Python 3.9 和 PEP 585 中的typing.Any - 标准集合中的类型提示泛型
我想了解是否仍然需要 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Any
的使用保持不变。 PEP 585 仅适用于标准集合。从 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.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
)