小编use*_*766的帖子

mypy:正确的类型注释多类型列表的方法

我有一个列表列表,每个列表都包含一个文件对象和一个字符串列表:

sample = [ [fileobject1, ['hello', 'world']], [fileobject2, ['something', 'else']] ]

我输入这样注释sample

List[List[Union[IO, List[str]]]]

进一步在我的代码中,我在内部列表的第一个 (0) 和第二个 (1) 条目上调用了一些方法。

例如像这样清除最内部的列表:

entry[1].clear()

代码运行良好,但 mypy 正确地抱怨:

Item "IO[Any]" of "Union[IO[Any], List[str]]" has no attribute "clear"

我将如何正确键入注释?也许一起使用不同的数据结构?

python type-hinting mypy

7
推荐指数
1
解决办法
3462
查看次数

标签 统计

mypy ×1

python ×1

type-hinting ×1