关于复杂类型 List[Tuple[...] 的 mypy 错误?
mypy error regarding complex type List[Tuple[...]?
我有 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
解析后,returns消息如下。
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 无法识别“字符串、整数和整数的 3 元素列表”的类型。它为 [tag, x, y]
计算的类型是 List[object]
,调用 tuple
会产生 Tuple[object, ...]
.
而不是 tuple([tag, x, y])
,只需使用元组文字:(tag, x, y)
.
或者完全跳过循环:return list(zip(S, X, Y))
我有 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
解析后,returns消息如下。
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 无法识别“字符串、整数和整数的 3 元素列表”的类型。它为 [tag, x, y]
计算的类型是 List[object]
,调用 tuple
会产生 Tuple[object, ...]
.
而不是 tuple([tag, x, y])
,只需使用元组文字:(tag, x, y)
.
或者完全跳过循环:return list(zip(S, X, Y))