Search code examples
pythonmypy

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


I am converting PyArrow types to their json-schema.org equivalent. The code works, all unit tests pass but MyPy gives the error

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

        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. How can I get instruct it that it is a dictionary? If not, can I get MyPy to ignore this problem?


Solution

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