10

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?

alex_noname
  • 26,459
  • 5
  • 69
  • 86
garyj
  • 1,302
  • 2
  • 13
  • 22

1 Answers1

12

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
alex_noname
  • 26,459
  • 5
  • 69
  • 86
  • I thought that was the case, but just thought I would double check :) – garyj Dec 10 '20 at 12:46
  • 6
    `NoReturn`, `Union`, `Optional`, `Literal`, `ClassVar`, `Final`, `Annotated`, `Generic`, `TypeVar`, `AnyStr`, `Protocol`, `NewType`, `TypedDict`, `IO`, `TextIO`, `BinaryIO`, `Text`, `SupportsAbs`, `SupportsBytes `, `SupportsComplex`, `SupportsFloat`, `SupportsIndex`, `SupportsInt`, `SupportsRound` are other types that are also needed to be imported from the `typing` module besides `Any` in Python 3.9. – Rockallite Jan 06 '21 at 07:06