我定义了一个这样的函数
def map_json_to_item_dictionary(json: dict[str, dict]) -> dict[int, Any]:
result: dict[int, Any] = {}
return result
但有一个例外
'type' object is not subscriptable at function definition
PyCharm也提示异常
Class 'type' does not define '__getitem__', so the
[] operator cannot be used on its instances
看起来 python 尝试从
dict
访问某些内容。
我做错了什么?
您需要从
Dict
模块导入 Any
和 typing
:
from typing import Dict, Any
def map_json_to_item_dictionary(json: Dict[str, dict]) -> Dict[int, Any]:
result: Dict[int, Any] = {}
return result