MyPy error: "object" has no attribute "get"

MyPy error: "object" has no attribute "get"

我正在将 PyArrow 类型转换为它们的 json-schema.org 等价物。代码有效,所有单元测试都通过但 MyPy 给出错误

error: "object" has no attribute "get"

我有一个字典如下

from pyarrow import bool_, date32, float64, int64, string

DATATYPE_MAPPING = {
    string(): {"json_type": "string", "example_value": "Words"},
    bool_(): {"json_type": "bool", "example_value": True},
    int64(): {"json_type": "integer", "example_value": 32},
    float64(): {"json_type": "number", "example_value": 3.14},
    date32(): {
        "json_type": "string",
        "example_value": "2020-02-01",
        "format": "date",
    },
}

这用于从 PyArrow.csv.read_csv 的结果中转换数据类型,如下所示

        arrow_reader = csv.read_csv(
            self.input_file,
            read_options=csv_read_options,
            parse_options=csv_parse_options,
        )

        self.column_headers = arrow_reader.column_names

        # Build the list converting PyArrow types to json-schema.org types
        data_type_list = [
            DATATYPE_MAPPING[datatype].get("example_value", "string")
            for datatype in arrow_reader.schema.types
        ]

MyPy 不理解 DATATYPE_MAPPING[数据类型] 是字典。 我怎样才能告诉它它是一本字典? 如果没有,我可以让 MyPy 忽略这个问题吗?

感谢乔尔。

from pyarrow import DataType, bool_, date32, float64, int64, string

DATATYPE_MAPPING: dict[DataType, dict[str, object]] = {
    string(): {"json_type": "string", "example_value": "Words"},
    bool_(): {"json_type": "bool", "example_value": True},
    int64(): {"json_type": "integer", "example_value": 32},
    float64(): {"json_type": "number", "example_value": 3.14},
    date32(): {
        "json_type": "string",
        "example_value": "2020-02-01",
        "format": "date",
    },
}