Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 614
garyj Avatar asked Dec 03 '20 05:12

garyj


People also ask

How do you type hinting in Python?

Here's how you can add type hints to our function: Add a colon and a data type after each function parameter. Add an arrow ( -> ) and a data type after the function to specify the return data type.

Should I use type hinting in Python?

Type hints work best in modern Pythons. Annotations were introduced in Python 3.0, and it's possible to use type comments in Python 2.7. Still, improvements like variable annotations and postponed evaluation of type hints mean that you'll have a better experience doing type checks using Python 3.6 or even Python 3.7.

What is generic typing Python?

Generic TypesGenerics are not just used for function and method parameters. They can also be used to define classes that can contain, or work with, multiple types. These “generic types” allow us to state what type, or types, we want to work with for each instance when we instantiate the class.

Is generics data type supported in Python?

Actually now you can use generics in Python 3.5+. See PEP-484 and typing module documentation.


1 Answers

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
like image 150
alex_noname Avatar answered Sep 24 '22 12:09

alex_noname