简体   繁体   English

MyPy 错误:“object”没有属性“get”

[英]MyPy error: "object" has no attribute "get"

I am converting PyArrow types to their json-schema.org equivalent.我正在将 PyArrow 类型转换为它们的 json-schema.org 等价物。 The code works, all unit tests pass but MyPy gives the error代码有效,所有单元测试都通过,但 MyPy 给出错误

error: "object" has no attribute "get"

I have a dictionary as follows我有一本字典如下

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",
    },
}

This is used to translate data types from the results of PyArrow.csv.read_csv as follows这用于从 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 does not understand that a DATATYPE_MAPPING[datatype] is a dictionary. MyPy 不理解 DATATYPE_MAPPING[datatype] 是字典。 How can I get instruct it that it is a dictionary?我怎样才能知道它是一本字典? If not, can I get MyPy to ignore this problem?如果没有,我可以让 MyPy 忽略这个问题吗?

Thanks to Joel for this.感谢乔尔。

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",
    },
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM