Python mypy:Collection[str]替换嵌套结构类型?
我有一个Python mypy:Collection[str]替换嵌套结构类型?,python,mypy,Python,Mypy,我有一个数据变量,如果输入正确,应该如下所示: data: List[Dict[str, Union[str, Dict[str, List[Dict[str, str]]]]]] = [] 但当我尝试使用以下代码附加到此列表时: info = {"spaghetti": [{"value": "foo", "test": "bar"}]} my_data = { "Name": name, "_info": info, } data.append(my
数据
变量,如果输入正确,应该如下所示:
data: List[Dict[str, Union[str, Dict[str, List[Dict[str, str]]]]]] = []
但当我尝试使用以下代码附加到此列表时:
info = {"spaghetti": [{"value": "foo", "test": "bar"}]}
my_data = {
"Name": name,
"_info": info,
}
data.append(my_data)
Mypy告诉我:
Argument 1 to "append" of "list" has incompatible type "Dict[str, Collection[str]]"; expected "Dict[str, Union[str, Dict[str, List[Dict[str, str]]]]]"
现在,怎么可能
Union[str,Dict[str,List[Dict[str,str]]]]
等同于Collection[str]
?注意Dict[str,Any]
是Collection[str]
的子集,因为Dict
实现了Collection
接口。将类型注释(或显式的cast
)添加到info
)有帮助吗?注释info
没有帮助,但如果我注释my_数据
,则没有更多警告。