Python typing deprecation

Viewed 1179

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).

2 Answers

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.

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.

It means that

def foo(d: typing.Deque):
    ...

Should be changed to:

def foo(d: collections.deque):
    ...
Related