如何解决关于复杂类型List [Tuple [...]的mypy错误?
我有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))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。