Python mypy可以处理列表理解吗?
在上述代码上运行Python mypy可以处理列表理解吗?,python,python-3.x,type-hinting,mypy,Python,Python 3.x,Type Hinting,Mypy,在上述代码上运行mypy时,我得到: from typing import Tuple def test_1(inp1: Tuple[int, int, int]) -> None: pass def test_2(inp2: Tuple[int, int, int]) -> None: test_tuple = tuple(e for e in inp2) reveal_type(test_tuple) test_1(test_tuple) te
mypy
时,我得到:
from typing import Tuple
def test_1(inp1: Tuple[int, int, int]) -> None:
pass
def test_2(inp2: Tuple[int, int, int]) -> None:
test_tuple = tuple(e for e in inp2)
reveal_type(test_tuple)
test_1(test_tuple)
test\u tuple
是否不保证有3个int
元素?mypy
是否不处理此类列表理解,或者这里是否有其他定义类型的方法?从0.600版开始,mypy
在此类情况下不会推断类型。这将很难实施,正如上所建议的那样
相反,我们可以使用cast
(请参阅):
从0.600版开始,
mypy
在这种情况下不会推断类型。这将很难实施,正如上所建议的那样
相反,我们可以使用cast
(请参阅):
error: Argument 1 to "test_1" has incompatible type "Tuple[int, ...]"; expected "Tuple[int, int, int]"
from typing import cast, Tuple
def test_1(inp1: Tuple[int, int, int]) -> None:
pass
def test_2(inp2: Tuple[int, int, int]) -> None:
test_tuple = cast(Tuple[int, int, int], tuple(e for e in inp2))
reveal_type(test_tuple)
test_1(test_tuple)