问题描述
我有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,y]))
return inp
解析后,将消息返回到下面。
a.py:28: error: Incompatible return value type (got "List[Tuple[object,...]]",expected "List[Tuple[str,int]]")
Found 1 error in 1 file (checked 1 source file)
那么定义出了什么问题?为什么mypy看到返回对象的类型像List[Tuple[object,...]
而不是应有的List[Tuple[str,int]]
?。预先谢谢你。
解决方法
问题是[tag,x,y]
。 Mypy无法识别“ string,int和int的3元素列表”的类型。它为[tag,y]
计算的类型为List[object]
,然后调用tuple
生成Tuple[object,...]
。
代替tuple([tag,y])
,只需使用元组文字:(tag,y)
。
或完全跳过循环:return list(zip(S,X,Y))