我正在Eclipse 4.8和Pydev下的Linux Ubuntu 18.04上的Python 3.7下运行。
声明:
args:Dict[str: Optional[Any]] = {}
位于从我的测试代码导入的模块中。并用来自type.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
这将阻止导入我的测试模块。 我在做什么错了?
答案 0 :(得分:4)
字典类型的正确语法是
Dict[str, Optional[Any]]
在编写[a: b]
时,Python会将其解释为切片,即使部分数组起作用的事物,例如a[1:10]
。您可以在错误消息Got slice(<class 'str'>, typing.Union[typing.Any, NoneType], None)
中看到它。