mypy错误:“对象”没有属性“ get”

发布于 2025-01-28 02:44:22 字数 1236 浏览 2 评论 0原文

我正在将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.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 [datatype]是词典。 我如何获得指示它是字典? 如果没有,我可以让Mypy忽略这个问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城旧梦 2025-02-04 02:44:22

感谢乔尔。

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

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",
    },
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文