Python typing deprecation

Question:

The latest typing docs has a lot of deprecation notices like the following:

class typing.Deque(deque, MutableSequence[T])
A generic version of collections.deque.

New in version 3.5.4.

New in version 3.6.1.

Deprecated since version 3.9: collections.deque now supports []. See PEP 585 and Generic Alias Type.

What does this means? Should we not use the generic type Deque (and several others) anymore? I’ve looked at the references and didn’t connect the dots (could be because I’m an intermediate Python user).

Asked By: Abhijit Sarkar

||

Answers:

See PEP585 and Generic Alias Type.

Changes in Python 3.9 remove the necessity for a parallel type hierarchy in the typing module, so that you can just use collections.deque directly when annotating a deque-like type.

For example, it means that annotating a deque of ints like

def foo(d: typing.Deque[int]):
    ...

Should be changed to:

def foo(d: collections.deque[int]):
    ...
Answered By: wim

It means that you should be transitioning to using built-in types / types from the standard library instead of the ones provided by typing. So for example collections.deque[int] instead of typing.Deque[int]. The same for list, tuple, etc. So tuple[int, str] is the preferred way.

Answered By: a_guest
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.