Python 关于复杂类型列表[元组[…]的mypy错误?
我用Python 关于复杂类型列表[元组[…]的mypy错误?,python,debugging,mypy,python-typing,Python,Debugging,Mypy,Python Typing,我用mypy解析休闲代码: PointsList = List[Tuple[str, int, int]] def Input(S: str, X: List[int], Y: List[int]) -> PoinstList: inp = list() for tag, x, y in zip(S, X, Y): inp.append(tuple([tag, x, y])) return inp 解析后,
mypy
解析休闲代码:
PointsList = List[Tuple[str, int, int]]
def Input(S: str, X: List[int], Y: List[int]) -> PoinstList:
inp = list()
for tag, x, y in zip(S, X, Y):
inp.append(tuple([tag, x, y]))
return inp
解析后,返回下面的消息
a.py:28: error: Incompatible return value type (got "List[Tuple[object, ...]]", expected "List[Tuple[str, int, int]]")
Found 1 error in 1 file (checked 1 source file)
那么定义有什么问题?为什么mypy看到返回对象的类型像
List[Tuple[object,…]
而不是List[Tuple[str,int,int]]
应该是什么?。提前谢谢。问题是[tag,x,y]
。mypy无法识别返回对象的类型“字符串、int和int的三元素列表”。它为[tag,x,y]
计算的类型是list[object]
,并调用tuple
,生成tuple[object,…]
不要使用元组([tag,x,y])
,只需使用一个元组文本:(tag,x,y)
或者完全跳过循环:
返回列表(zip(S,X,Y))
问题是[tag,X,Y]
。Mypy不识别“字符串、int和int的三元素列表”的类型。它为[tag,X,Y]
计算的类型是列表[object]
,并调用tuple
,生成tuple[object,]
不要使用元组([tag,x,y])
,只需使用一个元组文本:(tag,x,y)
或者完全跳过循环:返回列表(zip(S,X,Y))