python - Python 3.9 和 PEP 585 中的 Typing.Any - 标准集合中的类型提示泛型

标签 python type-hinting python-typing python-3.9

我想了解是否 typing还需要包吗?
如果在 Python 3.8 中我这样做:

from typing import Any, Dict
my_dict = Dict[str, Any]
现在通过 PEP 585 在 Python 3.9 中,现在首选使用内置类型进行集合,因此:
from typing import Any
my_dict = dict[str, Any]
我还需要使用typing.Any吗?或者是否有内置类型来替换它而我找不到?

最佳答案

Any的使用保持原样。 PEP 585仅适用于标准集合。

This PEP proposes to enable support for the generics syntax in all standard collections currently available in the typing module.


从 Python 开始 3.9 ,以下收藏变成generic并从 typing 导入那些已弃用:
  • 元组#打字。元组
  • 列表#打字。列表
  • dict #typing.Dict
  • 设置#打字。设置
  • frozenset #typing.FrozenSet
  • 输入#打字。输入
  • collections.deque
  • collections.defaultdict
  • collections.OrderedDict
  • collections.Counter
  • collections.ChainMap
  • collections.abc.Awaitable
  • collections.abc.Coroutine
  • collections.abc.AsyncIterable
  • collections.abc.AsyncIterator
  • collections.abc.AsyncGenerator
  • collections.abc.Iterable
  • collections.abc.Iterator
  • collections.abc.Generator
  • collections.abc.Reversible
  • collections.abc.Container
  • collections.abc.Collection
  • collections.abc.Callable
  • collections.abc.Set #typing.AbstractSet
  • collections.abc.MutableSet
  • collections.abc.Mapping
  • collections.abc.MutableMapping
  • collections.abc.Sequence
  • collections.abc.MutableSequence
  • collections.abc.ByteString
  • collections.abc.MappingView
  • collections.abc.KeysView
  • collections.abc.ItemsView
  • collections.abc.ValuesView
  • contextlib.AbstractContextManager # typing.ContextManager
  • contextlib.AbstractAsyncContextManager # typing.AsyncContextManager
  • re.Pattern # Typing.Pattern, Typing.re.Pattern
  • re.Match #typing.Match, Typing.re.Match
  • 关于python - Python 3.9 和 PEP 585 中的 Typing.Any - 标准集合中的类型提示泛型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65120501/

    相关文章:

    python - 将列表作为 url 值传递给 urlopen

    python - 在包级别使用类型注释

    python 输入 : result depending on arguments

    python - mypy 泛型可以表示将返回序列类型作为参数传递吗?

    python - 哪种方法可以更好地限制函数的执行时间?

    python - 如何在从elasticsearch导出的CSV文件中打印列名?

    Python ttk.Button -命令,无需按下按钮即可运行

    python - 返回非实例化类类型的类型提示

    python - mypy 的条件类型

    python - 如何将 sqlite3 模块添加到 Python?