所以,我有一些如下所示的代码:
def get_tree(args):
sensor_ids = argToList(args.get("sensor_ids"))
process_ids = argToList(args.get("process_ids"))
all_results = {}
for sensor_id in sensor_ids:
sensor_id_str = str(sensor_id)
for process_id in process_ids:
process_id_str = str(process_id)
main_process = query_process(sensor_id_str, process_id_str)
results = {}
json_response = main_process.json()
for vertex in json_response["resources"]:
if vertex["vertex_type"] == "process":
results["main_process"] = {"sensor_id": sensor_id_str,
"process_id": process_id_str,
"properties": vertex["properties"]}
for edge in vertex["edges"]:
if edge == "child_process":
results["child_processes"] = []
MyPy 正在创建此错误,我不知道如何修复,flake 很高兴:
赋值中的类型不兼容(表达式具有类型 “List[]”,目标类型为“Dict[str, Any]”)[赋值] 结果[“子进程”] = [] ^
对于 that 原始代码,
mypy
(使用 0.950 和 0.961 进行测试)应该 告诉你
需要“结果”的类型注释(提示:“
”)results: Dict[<type>, <type>] = ...
这样它就可以判断你是否在做正确的事情。
使用增强代码,如果您调用
reveal_type(results)
,您会看到
a.py:27: note: Revealed type is "builtins.dict[builtins.str, builtins.dict[builtins.str, Any]]"
即根据你对
results
所做的第一个作业,mypy 推断出你有一个字典的字典,因此,在那里放一个列表是行不通的。
因此,如果您想将
results
注释为带有字符串键和任意值的基本“包”,
results: Dict[str, Any] = {}
TypedDict
:
class Results(TypedDict):
child_processes: Optional[List[Any]] # or be more specific...
results: Results = {}