“type”对象在函数定义中不可下标[重复]

问题描述 投票:0回答:1

我定义了一个这样的函数

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
访问某些内容。

我做错了什么?

python python-typing
1个回答
2
投票

您需要从

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

© www.soinside.com 2019 - 2024. All rights reserved.