递归类型注解
Recursive type annotations
我正在尝试在适用的情况下向我的代码库中引入静态类型注释。一种情况是当读取 JSON 时,生成的对象将是一个以字符串为键的字典,具有以下类型之一的值:
bool
str
float
int
list
dict
然而,上面的 list
和 dict
可以包含相同类型的字典,从而导致递归定义。这在 Python3 的类型结构中可以表示吗?
从 mypy 0.641 开始,mypy 不支持您正在寻找的那种递归类型注释。自然语法:
from typing import Union, Dict, List
JSONVal = Union[None, bool, str, float, int, List['JSONVal'], Dict[str, 'JSONVal']]
d: JSONVal = {'a': ['b']}
生成错误报告缺少递归类型支持:
$ mypy asdf.py
asdf.py:3: error: Recursive types not fully supported yet, nested types replaced with "Any"
另请参阅 mypy issue tracker thread 递归类型支持。
目前,Dict[str, Any]
可能是可行的方法。
对于更新版本的 mypy,this comment 提到的 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"
我正在尝试在适用的情况下向我的代码库中引入静态类型注释。一种情况是当读取 JSON 时,生成的对象将是一个以字符串为键的字典,具有以下类型之一的值:
bool
str
float
int
list
dict
然而,上面的 list
和 dict
可以包含相同类型的字典,从而导致递归定义。这在 Python3 的类型结构中可以表示吗?
从 mypy 0.641 开始,mypy 不支持您正在寻找的那种递归类型注释。自然语法:
from typing import Union, Dict, List
JSONVal = Union[None, bool, str, float, int, List['JSONVal'], Dict[str, 'JSONVal']]
d: JSONVal = {'a': ['b']}
生成错误报告缺少递归类型支持:
$ mypy asdf.py
asdf.py:3: error: Recursive types not fully supported yet, nested types replaced with "Any"
另请参阅 mypy issue tracker thread 递归类型支持。
目前,Dict[str, Any]
可能是可行的方法。
对于更新版本的 mypy,this comment 提到的 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"