“typing” is getting deprecated in Python

Indently
26 Aug 202407:19

Summary

TLDRIn this video, the creator expresses frustration over the deprecation of types in Python's 'typing' module since version 3.9, which was not well communicated. The video demonstrates the use of 'typing.Iterable' and 'typing.Callable', which are now recommended to be replaced with 'collections.abc' equivalents. The speaker criticizes the lack of clear deprecation warnings and the implicit handling of this change, which they only discovered in Python 3.12, and calls for more transparency in such updates.

Takeaways

  • 😕 The video discusses the deprecation of certain types in the `typing` module since Python 3.9.
  • 👨‍💻 The presenter expresses frustration over the lack of clear communication regarding these deprecations.
  • 🔍 The `typing.Iterable` and `typing.Callable` types are deprecated and should be replaced with `collections.abc.Iterable` and `collections.abc.Callable`.
  • 📚 The presenter found out about the deprecations only recently, despite using Python 3.2 daily and keeping up with documentation.
  • 💻 The video demonstrates how to use the deprecated types in a function example, which still works without warnings.
  • 🚫 The deprecation is not flagged by tools like `mypy` or PyCharm, which the presenter finds concerning.
  • 📝 The change is intended to minimize the runtime impact of the `typing` module, but the presenter questions the lack of deprecation warnings.
  • 🔄 The deprecated types are simply aliases to the `collections.abc` module, which should be used instead.
  • 🤔 The presenter is unsure why the change is being handled implicitly and wishes for clearer communication from Python developers.
  • 🗣️ The video ends with a call for feedback from viewers on their thoughts about the handling of these deprecations.

Q & A

  • What is the main topic of the video?

    -The main topic of the video is the deprecation of certain types in the 'typing' module in Python since version 3.9.

  • Why is the presenter upset in the video?

    -The presenter is upset because they found out about the deprecation of types in the 'typing' module only recently, despite using Python 3.2 daily, and they feel that the way this change was handled was sneaky and not well communicated.

  • What is the alternative to 'typing.Iterable' after its deprecation?

    -After the deprecation of 'typing.Iterable', the alternative is to use 'collections.ABC.Iterable' as recommended in the Python documentation.

  • What does the presenter demonstrate with the 'say_hello' function?

    -The presenter demonstrates that the 'say_hello' function, which takes an iterable of names, works correctly with the deprecated 'typing.Iterable' type, but should instead use 'collections.ABC.Iterable'.

  • What is the purpose of the 'repeat' function shown in the video?

    -The 'repeat' function is used to execute another function a specified number of times, and it uses the deprecated 'typing.Callable' type, which should be replaced with 'collections.ABC.Callable'.

  • What is the issue with the deprecation process as described by the presenter?

    -The issue is that the deprecation of types in the 'typing' module is not well communicated, and the presenter only found out about it after several Python versions had been released.

  • Why did the Python developers deprecate certain types from the 'typing' module?

    -The Python developers deprecated certain types to minimize the runtime impact of typing and to streamline the type annotations by directing users to use 'collections.ABC' instead.

  • How does the presenter feel about the lack of deprecation warnings for the 'typing' module types?

    -The presenter is confused and frustrated by the lack of deprecation warnings, as they believe it should have been communicated more clearly to avoid confusion among developers.

  • What is the presenter's opinion on the future of deprecated types in the 'typing' module?

    -The presenter wishes for more concrete information about whether the deprecated types will be removed from the 'typing' module in the future or not.

  • What does the presenter encourage viewers to do after watching the video?

    -The presenter encourages viewers to share their thoughts on the deprecation of types in the 'typing' module and whether they were aware of the change or not in the comment section.

Outlines

00:00

😕 Python's Deprecated Typing Module

The speaker expresses frustration over the deprecation of types in Python's 'typing' module since version 3.9. They discuss the lack of visibility regarding this change, which they only discovered in Python 3.12. The video introduces a function 'say_hello' that uses the deprecated 'Iterable' type from the 'typing' module, which should now be replaced with 'collections.ABC'. The speaker emphasizes the importance of checking documentation directly for such updates, as their usual tools did not flag the deprecation.

05:01

😤 The Silent Deprecation of Python's Typing Features

Continuing the discussion on Python's 'typing' module, the speaker criticizes the implicit handling of deprecation, which they feel should have been more explicitly communicated. They mention the lack of deprecation warnings as a missed opportunity to inform developers of the need to update their code. The speaker also touches on the potential future removal of these deprecated features, expressing a desire for clearer communication from the Python development team. They conclude by inviting viewers to share their thoughts on the matter in the comments section.

Mindmap

Keywords

💡typing module

The typing module in Python is a standard library that supports type hints, which are used to indicate the expected data types of function arguments and return values. In the video, the typing module is discussed in the context of its deprecation of certain types since Python 3.9, which has caused confusion among developers who were not aware of these changes. The script mentions importing 'typing as T' and using 'Iterable' from it, which is then contrasted with the recommended use of 'collections.ABC'.

💡collections.ABC

collections.ABC stands for Abstract Base Classes, which is a module in Python that provides a set of base classes that can be used to create abstract classes. In the video, the presenter discusses how types from the typing module that were deprecated should now be accessed through collections.ABC instead. For example, 'Iterable' should now be imported as 'C.Iterable' from collections.ABC.

💡deprecation

Deprecation in software development refers to the process of declaring a piece of software, or part of it, as outdated and no longer recommended for use. In the video, the concept is used to describe how certain types in the typing module have been deprecated since Python 3.9, which means they are no longer the preferred way to annotate types and may be removed in the future.

💡type hints

Type hints in Python are optional annotations that indicate the expected data type of a function's parameters and return values. They are used to improve code readability and to enable static type checkers to catch type-related errors. The video discusses how type hints using the typing module have been affected by the deprecation of certain types.

💡Iterable

In Python, an Iterable is an object that can be iterated over, meaning it can return its members one at a time. The video script uses 'Iterable' as an example of a type that was available in the typing module but has been deprecated, and developers are now encouraged to use 'collections.ABC.Iterable' instead.

💡Callable

A Callable in Python is any object that can be called like a function. In the video, 'Callable' is mentioned as another type from the typing module that has been deprecated and should now be accessed as 'collections.ABC.Callable'. The script provides an example of a function that takes another function (a callable) as an argument.

💡mypy

Mypy is a static type checker for Python that helps catch type errors before the code runs. The video script mentions mypy in the context of checking the code for type errors, even when using deprecated types from the typing module. The presenter expresses frustration that mypy did not alert them to the deprecations.

💡backwards compatibility

Backwards compatibility refers to the ability of a system, product, or service to work with older versions without issues. In the video, the presenter speculates that the deprecation of types in the typing module might be handled implicitly to maintain some level of backwards compatibility, even though it causes confusion among developers.

💡runtime impact

Runtime impact refers to the effect that a piece of code or a change in a library has on the performance or behavior of a program when it is running. The video discusses the intention behind the deprecation of certain types in the typing module, which is to minimize the runtime impact of typing, although the presenter questions the significance of this impact.

💡documentation

Documentation in programming refers to the written instructions and descriptions of a program or its components. The video script highlights the importance of reading documentation to stay updated with changes in a programming language or library. The presenter expresses frustration that they learned about the deprecation of types in the typing module from the documentation, which they had read but apparently missed the relevant information.

Highlights

The video discusses the deprecation of certain types in the Python 'typing' module since version 3.9.

The creator expresses frustration over the lack of visibility regarding the deprecation.

Importing 'typing' as T and 'collections.ABC' as C is demonstrated.

A function 'say_hello' is created to illustrate the use of the 'iterable' type from the 'typing' module.

The video shows that using 'iterable' works fine in practice, even though it's deprecated.

The creator emphasizes the need to check the documentation for deprecation notices.

It is recommended to use 'collections.ABC' instead of 'typing' for collection types.

The video explains that 'typing' types are aliases to 'collections', making them equivalent.

A second function 'repeat' is introduced to demonstrate the use of the 'callable' type.

The 'callable' type is also deprecated and should be replaced with 'collections.ABC.Callable'.

The video criticizes the implicit handling of the deprecation, making it difficult to discover.

The creator discusses the intention behind deprecation to minimize runtime impact.

Deprecation warnings are not generated for these changes, which is a point of confusion.

The possibility of deprecated functionalities being removed in the future is mentioned.

The video ends with a call for community feedback on the handling of deprecations.

The creator invites viewers to share their thoughts on the deprecation process in the comments.

Transcripts

play00:00

this video was brought to you by IND

play00:02

dentle IO learning python Made Simple

play00:05

how's it going everyone in today's video

play00:07

we're going to be going over the typing

play00:09

module and how most of the types inside

play00:12

it are actually deprecated since python

play00:15

3.9 I only recently found out about this

play00:18

and that's why I'm kind of upset because

play00:21

in my opinion the way they're handling

play00:22

this is kind of sneaky anyway to dive

play00:25

into what I want to talk about today I'm

play00:27

going to import typing as T and then I'm

play00:30

also going to import collections. ABC as

play00:34

C next I'm going to create a function

play00:36

that says say hello and that's going to

play00:39

take some names to actually say hello to

play00:43

and names should be of type itable so

play00:45

here I'm going to refer to our typing

play00:47

module and use the iterable

play00:50

type and this function will return none

play00:53

because we are only executing code but

play00:55

next we'll type in forame in names print

play00:59

name names or name and at a first glance

play01:03

this should look quite fine I mean if

play01:05

you type in say hello you add some names

play01:08

such as

play01:10

Bob and Luigi the program is going to

play01:13

work perfectly fine because we passed in

play01:16

a type which was of type iterable if we

play01:19

were to pass in a number that would not

play01:21

work because an integer is not of type

play01:24

iterable our type has to be iterable for

play01:27

this function to actually work correctly

play01:29

irly and even if we were to run mypi so

play01:32

we type in mypi main.py which is my

play01:34

current file we would have no issues

play01:37

found in this source

play01:40

file now what I absolutely hate about

play01:42

this is that typing. iterable is

play01:46

deprecated since python 3.9 which is

play01:49

ridiculous that it took me until python

play01:51

3.12 to find out about and maybe other

play01:54

code editors or other type Checkers are

play01:57

more strict about this but my pi and

play01:59

High charm show no signs of life when it

play02:02

comes to this deprecation and to

play02:04

actually see that this is deprecated you

play02:05

need to go to the docs directly it's

play02:08

written in the docs that everything's

play02:10

deprecated since python

play02:13

3.9 such as this itable it's going to

play02:16

tell you to use collections. ABC instead

play02:19

and this is since python 3.9 which again

play02:21

is insane cuz I'm using python 3.2 I use

play02:24

Python nearly every day and I only found

play02:27

out about it recently because this

play02:29

deprecation is incredibly implicit so

play02:32

what you're supposed to do instead for

play02:34

these collection types is use

play02:36

collections. APC which I gave an alias

play02:39

of C so I can just quickly do this and

play02:42

this would be the correct and modern way

play02:44

to annotate your types we're just

play02:47

importing from collections. ABC now

play02:49

instead of typing personally I hate this

play02:51

change because I thought typing is just

play02:53

such a straightforward module it has one

play02:55

place that contains all the types you

play02:57

need so it's just easy to refer to that

play02:59

module if you want to use any type you

play03:02

want now I'm complaining of course but I

play03:04

will get used to this I get used to all

play03:06

the changes that's what we do as

play03:08

developers I just hate how implicit this

play03:11

change is being brought about I mean

play03:12

again it took me three python versions

play03:14

to find out that I've been using a

play03:16

deprecated module all this time but

play03:18

let's go back to typing. iterable and go

play03:20

to the

play03:22

Declaration what you're going to notice

play03:24

is that all the types or all of the

play03:26

types that are getting deprecated from

play03:28

the typing module are actually just

play03:31

aliases to the collections module now

play03:33

which just means that if you refer to

play03:35

something from the typing module it just

play03:37

points to the one that's in the

play03:38

collections module t. iterable and c.

play03:42

iterable are exactly the same thing but

play03:45

let's take a look at another example and

play03:48

here I'm going to create a function

play03:49

called

play03:50

repeat which is going to take a function

play03:53

and repeat that function and I'm going

play03:55

to use the callable type

play03:57

here and we want to provide the amount

play03:59

of time s we want to repeat that

play04:01

function that will return none cuz we're

play04:04

only executing code and then for I in

play04:07

range times execute that function then

play04:11

let's create a simple function that says

play04:13

hello returns

play04:16

nothing and we will print hello just so

play04:19

we can test out our repeat function here

play04:22

we can type in repeat Hello without the

play04:26

parenthesis and times will be set to

play04:28

three so that when we run this we should

play04:30

get that function repeated three times

play04:33

once again everything's running

play04:34

perfectly even if we open up mypie and

play04:37

run that we're going to find no issues

play04:40

in this source file even if we're using

play04:43

a deprecated type except this time if we

play04:46

actually go to the

play04:49

Declaration the colable doc will have a

play04:51

message that says this is a deprecated

play04:54

Alias to collections. abc. colable and

play04:57

if we actually go to the docs once again

play05:00

it's going to be deprecated since python

play05:02

3.9 and it's going to redirect you to

play05:05

collections. ABC where you will find the

play05:08

appropriate type which then means you

play05:10

can change it to c. colable which

play05:13

actually uses the correct module for

play05:15

this type but again this change isn't

play05:17

really the worst thing in the world what

play05:20

I hate the most about it is how

play05:21

implicitly it's being handled the fact

play05:23

that I had to learn this from one of you

play05:25

guys in the comment section three

play05:27

versions later is what the disturbs me I

play05:30

read the updates I read the

play05:31

documentation all the time but still I

play05:34

had no idea until python 3.12 that this

play05:36

was actually a thing and they write that

play05:38

the intention is to minimize the runtime

play05:41

impact of typing I personally don't know

play05:43

how big that impact actually is but they

play05:46

State very clearly that this deprecation

play05:48

will not generate deprecation warnings

play05:51

and I don't understand that I don't

play05:53

understand why they wouldn't generate

play05:54

any deprecation warnings for something

play05:56

like this and they continue to write

play05:58

that the deprecated function

play05:59

functionality may eventually be removed

play06:02

from the typing module they wrote May

play06:04

here I don't understand why they write

play06:05

may I don't know if they're still

play06:07

thinking about this change or not but I

play06:09

wish it was a bit more concrete it's

play06:11

really like telling somebody hey maybe

play06:14

in the future you'll be able to use this

play06:16

maybe you won't we're still deciding or

play06:19

maybe it's for backwards compatibility I

play06:21

know that it's not an easy decision or

play06:23

it's not as easy as saying you should

play06:25

just remove it or you should just keep

play06:27

it I only wish they would put something

play06:29

a bit more concrete out for all of us so

play06:31

that we know that something's going to

play06:33

change or that something's not going to

play06:35

change instead of having us find out in

play06:37

October

play06:39

20202 when they actually deprecated if

play06:42

they deprecated but yeah that's actually

play06:45

all I wanted to cover in today's video

play06:47

do let me know what you think about this

play06:48

change in the comment section down below

play06:51

whether you already knew about it or

play06:52

whether this is the first time you hear

play06:54

about it I would love to hear about that

play06:55

in the comment section down below also

play06:57

your takes on the matter I mean of

play06:59

course course I can get very angry about

play07:00

this but I would love to hear whether

play07:02

you think this actually makes sense

play07:04

whether it's good that they pass it

play07:06

implicitly or whether it's completely my

play07:08

fault that I didn't read the

play07:09

documentation close enough when python

play07:11

3.9 was released so yeah with all that

play07:15

being said as always thanks for watching

play07:17

and I'll see you in the next video

Rate This

5.0 / 5 (0 votes)

相关标签
PythonTyping ModuleDeprecationDevelopersType HintingIterableCallableCollections.ABCCode EditingDocumentationBackward Compatibility
您是否需要英文摘要?