gpt4 book ai didi

python - Python中的泛型函数类型

转载 作者:行者123 更新时间:2023-12-03 13:45:01 25 4
gpt4 key购买 nike

我在 Eclipse 4.8 和 Pydev 下的 Linux Ubuntu 18.04 上的 Python 3.7 下运行。

声明:

args:Dict[str: Optional[Any]] = {}

位于从我的测试代码导入的模块中。并且它被标记为来自 typing.py 的以下错误消息:
TypeError: Parameters to generic types must be types. Got slice(<class 'str'>, typing.Union[typing.Any, NoneType], None). The stack trace follows: Finding files... done. Importing test modules ... Traceback (most recent call last):   File "/Data/WiseOldBird/Eclipse/pool/plugins/org.python.pydev.core_7.0.3.201811082356/pysrc/_pydev_runfiles/pydev_runfiles.py", line 468, in __get_module_from_str
mod = __import__(modname) File "/Data/WiseOldBird/Workspaces/WikimediaAccess/WikimediaLoader/Tests/wiseoldbird/loaders/TestWikimediaLoader.py", line 10, in <module>
from wiseoldbird.application_controller import main File "/Data/WiseOldBird/Workspaces/WikimediaAccess/WikimediaLoader/src/wiseoldbird/application_controller.py", line 36, in <module>
args:Dict[str: Optional[Any]] = {} File "/usr/local/lib/python3.7/typing.py", line 251, in inner
return func(*args, **kwds) File "/usr/local/lib/python3.7/typing.py", line 626, in __getitem__
params = tuple(_type_check(p, msg) for p in params) File "/usr/local/lib/python3.7/typing.py", line 626, in <genexpr>
params = tuple(_type_check(p, msg) for p in params) File "/usr/local/lib/python3.7/typing.py", line 139, in _type_check
raise TypeError(f"{msg} Got {arg!r:.100}.") TypeError: Parameters

这可以防止我的测试模块被导入。
我究竟做错了什么?

最佳答案

dict 类型的正确语法是

Dict[str, Optional[Any]]

当你写 [a: b] , Python 将其解释为一个切片,即使部分数组起作用的东西,如 a[1:10] .您可以在错误消息中看到这一点: Got slice(<class 'str'>, typing.Union[typing.Any, NoneType], None) .

关于python - Python中的泛型函数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53795514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com