6

我想了解是否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还是有一个我找不到的内置类型来替换它?

4

1 回答 1

3

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

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

从 Python 开始3.9,以下集合成为generic并且不推荐从中导入这些集合typing

  • 元组#打字。元组
  • 列表#打字。列表
  • dict #打字。字典
  • 设置#打字。设置
  • freezeset # typing.FrozenSet
  • 类型#打字。类型
  • 集合.deque
  • 集合.defaultdict
  • collections.OrderedDict
  • 集合.计数器
  • 集合.ChainMap
  • 集合.abc.Awaitable
  • collections.abc.Coroutine
  • collections.abc.AsyncIterable
  • 集合.abc.AsyncIterator
  • collections.abc.AsyncGenerator
  • 集合.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
  • 集合.abc.MutableSet
  • 集合.abc.映射
  • collections.abc.MutableMapping
  • collections.abc.Sequence
  • 集合.abc.MutableSequence
  • 集合.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
于 2020-12-03T07:11:10.280 回答