“typing” is getting deprecated in Python

preview_player
Показать описание
In today’s video we’re going to be talking about the typing module getting deprecated in Python, or to be more specific, how most of it is already deprecated since Python 3.9.

▶ Become job-ready with Python:

▶ Follow me on Instagram:
Рекомендации по теме
Комментарии
Автор

I think typing shouldn't be deprecated because I usually use my hands and a keyboard to type in my code.

kudorgyozo
Автор

This video is rather misleading. The `typing` module is not getting deprecated. Many aliases, like the ones shown in the video, are deprecated since Python 3.9. Types such as `Any`, `Self`, `Optional`, `Literal`, and more are still accessed through the `typing` module.

zarifatai
Автор

Now I have to type an extra dot in my imports, with everything going on in the world, I have to deal with too!?

eddie_dane
Автор

The documentation and the PEP (585) do state their reason for not annotating it as deprecated. Type checking is left to type checkers, not the interpreter. Quote, "It is expected that type checkers will flag the deprecated types when the checked program targets Python 3.9 or newer."

Removal is also documented:
"Removal will occur no sooner than Python 3.9’s end of life, scheduled for October 2025."

I think it's fair of Python to expect type checkers to do their job properly, but I also understand the confusion it might cause if the types are eventually just gone.

veni_vidi_victorian
Автор

To be fair: Your types are not evaluated during runtime, so python cannot give you a deprecation warning.
But I guess PyCharm could have added a rule to show you this deprecation warning. ruff does so.

stonemannerie
Автор

They've been deprecated since 3.9, but weirdly there are (currently) no concrete plans to actually remove these aliases. Even looking forward as far as 3.16 there's nothing. I think they might be "soft-deprecated" or something, as yeah Pylance doesn't mark them as deprecated either. I forget how I found out initially actually, I remember something flagged it to me, though I still use typing lmao.

Carberra
Автор

This actually makes sense when you take into account that the old capitalized types like List, Dict and Optional from typing have been replaced with more straightforward syntax. So now many programs won't need to use the typing module at all.

graemeholliday
Автор

I am just learning python at my job. This is my "go to" channel to learn python stuff. Focused and straight to the point videos. Awesome.

LV-
Автор

I just discovered it now! Thank you very much for the info. I absolutely agree on your opinion about typing, was great to have the typing module and I also not received any warning (vscode).

GiancarloMattia
Автор

Note that not everything in the typing module is being deprecated, for example 'Literal' and 'Optional' are staying (for now).

dirtcrusher
Автор

A lot of comments already talked about the fact that it is the checkers that should display a deprecation warning and not the interpreter.
However this video doesn't explain the change that occurred in Python 3.9 in itself. At least, not entirely.

Type hints before 3.9 were using at the same time standard built-in Python types (like int and str) and the typing module for more complex type hints. The best example I can think of right now is "List". To type hint a list of strings you had to do something like

from typing import List

def foo(bar: List[str]) -> None:

After 3.9, the [] notation is now available for native Python types. That's why you don't need typing anymore. So, our example above becames:

def foo(bar: list[str]) -> None:

Without any import and using the native list type. Same thing for collections.abc.Callable or Iterable which are the real fully-fledged Python types instead of some cluncky aliases that could only be use for type hints.

I do think this was a really cool update, it simplified my code and is way less confusing than having two types for the same thing

Sakiut-
Автор

Also built in types (list, tuple, dict) can be used for type annotation, instead of typing.List, etc.

nowster
Автор

I was aware of all the variations, but presumed that typing was the preferred option due to it being newer and more precise of a description in my opinion. I looked into it, and the reason for this change is obvious now. When generic variations of the types in abc were introduced, they could not be applied to the type itself because of limitations in Python. Since 3.9, it became possible to apply the generic typing to the classes itself. Therefore, the use of the typing module is now obsolete.

Looking into it it makes sense. But not a single editor has ever provided any indication of the deprecation. So I falsely made an assumption that the new version would be preferred. I think this is a completely reasonable assumption to make if you are not aware of the relevant context, so this should have been communicated more clearly.

damymetzke
Автор

We're obsessed with typing module 🥺❤

dipeshsamrawat
Автор

I'm surprised python developers didn't put a deprecated action on the actual code to let us all know.
I appreciate you bringing this to my attention. I'm erked too.

thisoldproperty
Автор

Thanks for the video! Most of the linters such as Ruff were warning about this deprecation long time ago

IManuI
Автор

Why's callable under collections???

yorailevi
Автор

I also had no idea. Honestly they should have made it more explicit, not just in documentation. If it was deprecated then i personally I would have liked an error or some sort of warning from pycharm. But oh well i guess we have to read the whole documentation, from start to end to find out.

laylaxoxo
Автор

As far as I am aware, a type such as _collections.abc.Iterable_ is an actual, valid type, checkable at runtime with _isinstance_ .
I am not sure if _typing.Iterable_ can be used to the same effect.
I will admit, I'm not a huge fan, but I guess it makes sense if they are checkable at runtime to move them into more relevant modules

aaronvegoda
Автор

Well, almost everything related to typing in python is quite chaotic and doesn't seem to have a single direction. The idea behind removing Iterable and things like that from typing is that they are not just for type hints, you could actually inherit from them and automatically receive implementation of some collection-related methods. That is runtime and not type checking. The decision to not generate DeprecationWarning is weird too.

BTW, that leaves a question: what the hell is Callable doing in collections.abs? :⁠-⁠D

evlezzz