首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >递归类型注解

递归类型注解
EN

Stack Overflow用户
提问于 2018-12-05 18:55:16
回答 3查看 5.1K关注 0票数 8

在适用的情况下,我试图在我的代码库中引入静态类型注释。一种情况是在读取JSON时,生成的对象将是一个由字符串键组成的字典,其值为以下类型之一:

  • bool
  • str
  • float
  • int
  • list
  • dict

但是,上面的listdict可以包含相同类型的字典,从而产生递归定义。这在Python3 3的类型结构中是有代表性的吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-12-05 19:12:22

从mypy 0.990开始,mypy最终使用自然语法支持递归类型注释:

代码语言:javascript
复制
from typing import Union, Dict, List

JSONVal = Union[None, bool, str, float, int, List['JSONVal'], Dict[str, 'JSONVal']]

d: JSONVal = {'a': ['b']}

类型产出:

代码语言:javascript
复制
Success: no issues found in 1 source file

在0.990之前,这将产生一个错误,报告缺乏递归类型支持:

代码语言:javascript
复制
$ mypy asdf.py
asdf.py:3: error: Recursive types not fully supported yet, nested types replaced with "Any"

在这样的版本中,Dict[str, Any]将是最好的选择。

您现在还可以使用相互递归的类型别名,这样您就可以执行以下操作

代码语言:javascript
复制
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']}
票数 11
EN

Stack Overflow用户

发布于 2022-10-26 11:55:08

对递归类型的支持现在在Mypy中。

截至2022年10月,这一实施是暂时的。您可以通过将enable_recursive_aliases = true标志添加到pyproject.toml来启用它。

从0.990版本开始,默认情况下将启用此功能。来源

票数 4
EN

Stack Overflow用户

发布于 2022-03-29 15:33:02

对于最近版本的mypy,这句话到提到的MyPy问题跟踪器提出了一种使用协议部分工作(但有点费解)的方法,只要不需要使用TypeVar

代码语言:javascript
复制
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"
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53638973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档