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

typing.Any in Python 3.9 and PEP 585 - Type Hinting Generics In Standard Collections

我想知道是否还需要 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 导入这些集合:

  • 元组#typing.Tuple
  • 列表#typing.List
  • 字典#typing.Dict
  • 设置#typing.Set
  • 冻结集#typing.FrozenSet
  • 类型#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.Mapping查看
  • collections.abc.KeysView
  • collections.abc.ItemsView
  • collections.abc.ValuesView
  • contextlib.AbstractContextManager#typing.ContextManager
  • contextlib.AbstractAsyncContextManager#typing.AsyncContextManager
  • re.Pattern # typing.Pattern, 打字.re.Pattern
  • re.Match # typing.Match, 键入。re.Match