如何向用户界提供类型的暗示?
我想定义一个userDict
,该读取JSON的值并存储给定密钥的位置。 JSON文件看起来像这样:
{
"pages": [
{
"areas": [
{
"name": "My_Name",
"x": 179.95495495495493,
"y": 117.92792792792793,
"height": 15.315315315315303,
"width": 125.58558558558553
},
...
]
}
]
}
我想指出键入linter(例如mypy),该字典是字符串,值为position
。
我当前的代码如下:
import json
from collections import UserDict
from dataclasses import dataclass, field
from pathlib import Path
from typing import Dict, List, Optional, Union
from typing_extensions import Literal
JsonPosition = Dict[str, Union[str, float]]
JsonPage = Optional[Dict[Literal["areas"], List[JsonPosition]]]
@dataclass
class Position:
"""Information for a position"""
name: str
x: float
y: float
width: float
height: float
@classmethod
def from_json(cls, dict_values: JsonPosition):
return cls(**dict_values) # type: ignore # dynamic typing
class Page(UserDict):
"""Information about positions on a page"""
@classmethod
def from_json(cls, page: JsonPage):
"""Get positions from JSON Dictionary"""
if page is None:
return cls()
return cls({cast(str, p["name"]): Position.from_json(p) for p in page["areas"]})
JSON = Path("my_positions.json").read_text()
positions = json.loads(JSON)
page_1 = Page.from_json(positions["pages"][0])
我希望Mypy(或塔属或我使用的任何类型的Hinter)自动识别page_1 [“ my_name”]
作为position
。
我可以改变什么?
I want to define a UserDict
that reads values from JSON and stores a position for a given key. The JSON file looks like this:
{
"pages": [
{
"areas": [
{
"name": "My_Name",
"x": 179.95495495495493,
"y": 117.92792792792793,
"height": 15.315315315315303,
"width": 125.58558558558553
},
...
]
}
]
}
I would like to indicate to type linters (e.g. MyPy) that this dictionary as a key being a string and the values being a Position
.
My current code is the following:
import json
from collections import UserDict
from dataclasses import dataclass, field
from pathlib import Path
from typing import Dict, List, Optional, Union
from typing_extensions import Literal
JsonPosition = Dict[str, Union[str, float]]
JsonPage = Optional[Dict[Literal["areas"], List[JsonPosition]]]
@dataclass
class Position:
"""Information for a position"""
name: str
x: float
y: float
width: float
height: float
@classmethod
def from_json(cls, dict_values: JsonPosition):
return cls(**dict_values) # type: ignore # dynamic typing
class Page(UserDict):
"""Information about positions on a page"""
@classmethod
def from_json(cls, page: JsonPage):
"""Get positions from JSON Dictionary"""
if page is None:
return cls()
return cls({cast(str, p["name"]): Position.from_json(p) for p in page["areas"]})
JSON = Path("my_positions.json").read_text()
positions = json.loads(JSON)
page_1 = Page.from_json(positions["pages"][0])
I would like MyPy (or Pylance or whatever type hinter I use), to automatically recognize page_1["My_Name"]
as being a Position
.
What could I change?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,您可以将类型直接提供给
userDict
带有方括号([...]
)的类型strong> python 3.6 或更早,这将行不通。
对于 python> = 3.7和< 3.9 ,您需要以下 to inscript
collections.userdict
并将其放在特定于键入检查的单独块中(使用常数type_checking
):for python 3.9+,无需其他进口或技巧。
Actually, you can directly provide the type to
UserDict
with square brackets ([...]
) like you would with aDict
:For Python 3.6 or earlier, this will not work.
For Python >=3.7 and <3.9, you need the following to subscript
collections.UserDict
and put it in a separate block specific to type checking (with constantTYPE_CHECKING
):For Python 3.9+, no additional import or trick is necessary.