我想了解是否 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