PEP 484 typing -- 如何注解 Callable withOut 返回类型?
PEP484 typing -- how to annotate Callable with NoReturn type?
是否可以使用 NoReturn
类型注释 Callable
?
我希望这样做的方式会产生错误:
from sys import exit
from typing import Callable, NoReturn
f: Callable[..., NoReturn] = exit
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/typing.py", line 755, in __getitem__
return self.__getitem_inner__(params)
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/typing.py", line 251, in inner
return func(*args, **kwds)
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/typing.py", line 774, in __getitem_inner__
result = _type_check(result, msg)
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/typing.py", line 135, in _type_check
raise TypeError(f"Plain {arg} is not valid as type argument")
TypeError: Plain typing.NoReturn is not valid as type argument
编辑:对于以后遇到此问题的任何人,此问题是 Python 3.7.0 中的错误,升级到 Python 3.7.2 可缓解此问题。
对我来说(使用 Python 3.7.2)它可以正常工作:
>>> from sys import exit
>>> from typing import Callable, NoReturn
>>> f: Callable[..., NoReturn] = exit
>>>
是否可以使用 NoReturn
类型注释 Callable
?
我希望这样做的方式会产生错误:
from sys import exit
from typing import Callable, NoReturn
f: Callable[..., NoReturn] = exit
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/typing.py", line 755, in __getitem__ return self.__getitem_inner__(params) File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/typing.py", line 251, in inner return func(*args, **kwds) File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/typing.py", line 774, in __getitem_inner__ result = _type_check(result, msg) File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/typing.py", line 135, in _type_check raise TypeError(f"Plain {arg} is not valid as type argument") TypeError: Plain typing.NoReturn is not valid as type argument
编辑:对于以后遇到此问题的任何人,此问题是 Python 3.7.0 中的错误,升级到 Python 3.7.2 可缓解此问题。
对我来说(使用 Python 3.7.2)它可以正常工作:
>>> from sys import exit
>>> from typing import Callable, NoReturn
>>> f: Callable[..., NoReturn] = exit
>>>