在适用的情况下,我试图在我的代码库中引入静态类型注释。一种情况是在读取JSON时,生成的对象将是一个由字符串键组成的字典,其值为以下类型之一:
boolstrfloatintlistdict但是,上面的list和dict可以包含相同类型的字典,从而产生递归定义。这在Python3 3的类型结构中是有代表性的吗?
发布于 2018-12-05 19:12:22
从mypy 0.990开始,mypy最终使用自然语法支持递归类型注释:
from typing import Union, Dict, List
JSONVal = Union[None, bool, str, float, int, List['JSONVal'], Dict[str, 'JSONVal']]
d: JSONVal = {'a': ['b']}类型产出:
Success: no issues found in 1 source file在0.990之前,这将产生一个错误,报告缺乏递归类型支持:
$ mypy asdf.py
asdf.py:3: error: Recursive types not fully supported yet, nested types replaced with "Any"在这样的版本中,Dict[str, Any]将是最好的选择。
您现在还可以使用相互递归的类型别名,这样您就可以执行以下操作
from typing import Union, Dict, List
JSONVal = Union[None, bool, str, float, int, 'JSONArray', 'JSONObject']
JSONArray = List[JSONVal]
JSONObject = Dict[str, JSONVal]
d: JSONObject = {'a': ['b']}发布于 2022-10-26 11:55:08
对递归类型的支持现在在Mypy中。
截至2022年10月,这一实施是暂时的。您可以通过将enable_recursive_aliases = true标志添加到pyproject.toml来启用它。
从0.990版本开始,默认情况下将启用此功能。来源。
发布于 2022-03-29 15:33:02
对于最近版本的mypy,这句话到提到的MyPy问题跟踪器提出了一种使用协议部分工作(但有点费解)的方法,只要不需要使用TypeVar:
from __future__ import annotations
from collections.abc import Iterator
from typing import TypeVar, Protocol, overload, Any, TYPE_CHECKING
_T_co = TypeVar("_T_co")
class _RecursiveSequence(Protocol[_T_co]):
def __len__(self) -> int: ...
@overload
def __getitem__(self, __index: int) -> _T_co | _RecursiveSequence[_T_co]: ...
@overload
def __getitem__(self, __index: slice) -> _RecursiveSequence[_T_co]: ...
def __contains__(self, __x: object) -> bool: ...
def __iter__(self) -> Iterator[_T_co | _RecursiveSequence[_T_co]]: ...
def __reversed__(self) -> Iterator[_T_co | _RecursiveSequence[_T_co]]: ...
def count(self, __value: Any) -> int: ...
def index(self, __value: Any, __start: int = ..., __stop: int = ...) -> int: ...
def func1(a: _RecursiveSequence[int]) -> int: ...
if TYPE_CHECKING:
reveal_type(func1([1])) # Revealed type is "builtins.int"
reveal_type(func1([[1]])) # Revealed type is "builtins.int"
reveal_type(func1([[[1]]])) # Revealed type is "builtins.int"
reveal_type(func1((1, 2, 3))) # Revealed type is "builtins.int"
reveal_type(func1([(1, 2, 3)])) # Revealed type is "builtins.int"
reveal_type(func1([True])) # Revealed type is "builtins.int"https://stackoverflow.com/questions/53638973
复制相似问题