我正在尝试在适用的情况下将静态类型注释引入我的代码库。一种情况是读取 JSON 时,生成的对象将是一个由字符串作为键的字典,其值具有以下类型之一:
bool
str
float
int
list
dict
然而,上面的
list
和 dict
可以包含相同类型的字典,从而导致递归定义。这可以在 Python3 的类型结构中表示吗?
从 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']}
mypy 输出:
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']}
Mypy 现在支持递归类型。
截至 2022 年 10 月,实施是暂时的。您可以通过将
enable_recursive_aliases = true
标志添加到 pyproject.toml
来启用它。
从 0.990 版本开始,默认启用此功能。 来源。
既然 PEP 695 已被接受,您可以在 Python 3.12 中执行此操作(无需从
typing
进行任何导入):
type JSONVal = None | bool | str | float | int | JSONArray | JSONObject
type JSONArray = list[JSONVal]
type JSONObject = dict[str, JSONVal]
如果您愿意,您甚至可以将其塞入一行中。请注意,您不再需要引号来指示前向引用。
您还可以使用泛型类型语法轻松限制原始类型:
type JSONVal[T] = T | JSONArray[T] | JSONObject[T]
type JSONArray[T] = list[JSONVal[T]]
type JSONObject[T] = dict[str, JSONVal[T]]
# JSON object that only allows for strings and integers:
str_and_int_object: JSONObject[str | int]
如果您使用选项 --enable-incomplete-feature=NewGenericSyntax
(该功能对我来说似乎很完整),则mypy 自版本 1.11 起接受所有这些。我想下个版本会完全支持。
对于 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"