关于复杂类型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))

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...