Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python typing deprecation

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

like image 790
Abhijit Sarkar Avatar asked May 11 '26 09:05

Abhijit Sarkar


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.

like image 55
a_guest Avatar answered May 12 '26 23:05

a_guest


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]):
    ...
like image 43
wim Avatar answered May 12 '26 22:05

wim