Python 串联元组的Mypy类型

Python 串联元组的Mypy类型,python,mypy,Python,Mypy,我有一个接受特定元组和连接的函数,我试图指定输出的类型,但mypy不同意我的说法 文件test.py: 将mypy 0.641作为mypy运行-忽略缺少的导入测试。我得到: 我想这是正确的,但更一般,因为我指定了我的输入。这是一个错误,但似乎没有时间线来允许mypy进行正确的类型推断。mypy目前不支持固定长度元组的串联。作为一种解决方法,您可以从单个元素构造元组: from typing import Tuple def test(a: Tuple[str, str], b: Tuple[i

我有一个接受特定元组和连接的函数,我试图指定输出的类型,但mypy不同意我的说法

文件test.py:

将mypy 0.641作为mypy运行-忽略缺少的导入测试。我得到:


我想这是正确的,但更一般,因为我指定了我的输入。

这是一个错误,但似乎没有时间线来允许mypy进行正确的类型推断。

mypy目前不支持固定长度元组的串联。作为一种解决方法,您可以从单个元素构造元组:

from typing import Tuple

def test(a: Tuple[str, str], b: Tuple[int, int]) -> Tuple[str, str, int, int]:
    return a[0], a[1], b[0], b[1]
或者使用Python 3.5+:

def test(a: Tuple[str, str], b: Tuple[int, int]) -> Tuple[str, str, int, int]:
    return (*a, *b)  # the parentheses are required here

下面是一个不太详细的python3.5+解决方案:

from typing import Tuple

def f(a: Tuple[str, str], b: Tuple[int, int]) -> Tuple[str, str, int, int]:
    return (*a, *b)
def test(a: Tuple[str, str], b: Tuple[int, int]) -> Tuple[str, str, int, int]:
    return (*a, *b)  # the parentheses are required here
from typing import Tuple

def f(a: Tuple[str, str], b: Tuple[int, int]) -> Tuple[str, str, int, int]:
    return (*a, *b)