mypy错误:“对象”没有属性“ 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.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢乔尔。
Thanks to Joel for this.