Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python mypy可以处理列表理解吗?_Python_Python 3.x_Type Hinting_Mypy - Fatal编程技术网

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)