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

发布于 2025-01-17 02:53:24 字数 414 浏览 1 评论 0原文

我想了解是否仍然需要 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 或者是否有内置类型输入以替换我找不到的内容?

I am trying to understand if the typing package is still needed?

If in Python 3.8 I do:

from typing import Any, Dict
my_dict = Dict[str, Any]

Now in Python 3.9 via PEP 585 it's now preferred to use the built in types for collections hence:

from typing import Any
my_dict = dict[str, Any]

Do I still need to use the typing.Any or is there a built in type to replace it which I can not find?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人疚 2025-01-24 02:53:24

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

此 PEP 建议在 typing 模块当前可用的所有标准集合中启用对泛型语法的支持。

从 Python 3.9 开始,以下集合变得通用,并且不推荐从 typing 导入这些集合:

  • tuple (typing.Tuple)
  • list< /code> (typing.List)
  • dict (typing.Dict)
  • set (typing.Set )
  • frozenset (typing.FrozenSet)
  • type (typing.Type)
  • 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)

The use of the Any remains the same. PEP 585 applies only to standard collections.

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

Starting with Python 3.9, the following collections become generic and importing those from typing is deprecated:

  • tuple (typing.Tuple)
  • list (typing.List)
  • dict (typing.Dict)
  • set (typing.Set)
  • frozenset (typing.FrozenSet)
  • type (typing.Type)
  • 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文