Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Typing Improvements

00:00 In the previous lesson, I showed you three new additions to the standard library. In this lesson, I’ll go over two new items in Python typing.

00:10 I’ll explain the first addition by demonstrating a problem for you. Consider this object-oriented code where I have a Cat class and a Tiger class that inherits from it. The Cat class has a method called .noise(), which the Tiger class overrides.

00:28 What would happen if I decide to rename the .noise() method in the Cat class,

00:34 changing it to .make_noise()? Most of the time when you do this, the intent is to make the same sort of changes to any inheriting classes as well. Now, if I have an instance of Tiger, if I call .make_noise(), I’m going to get "meow".

00:49 This probably isn’t what I wanted. Before Python 3.12, you could only catch this kind of bug at runtime.

00:57 Enter the @override decorator. This is a new addition to the static typing tool set, and it acts as an indicator that a method is one which overrides another.

01:08 By having this kind of marker, the type checker can now flag the previous problem case. If I rename .noise() inside of the Cat class, mypy or tools like it can now point out that the .noise() method in Tiger is no longer overriding anything.

01:25 This is actually an idea stolen from other languages. In fact, the PEP that suggested it listed a half dozen others that can do the same thing. The only downside here is it makes the code a bit more verbose, but typing tends to do that.

01:41 The other addition to typing in Python 3.12 is a simplified syntax for typing generics. The old, and still supported, way of typing a generic is to use the TypeVar concept from the typing module.

01:55 The function definition here says it takes two arguments of some type. Whatever type it is, they have to be the same, and the result of the function is that it returns the same type.

02:07 The syntax here is a bit clunky, and the PEP proposing the change commented that a significant percentage of code that used typing uses this feature. Python 3.12 cleans this up significantly.

02:20 The use of the square brackets before the parentheses here indicate that the function uses a generic type. Whatever you put in the square brackets becomes your placeholder, which you then use for the type of the arguments and return value of the function. Not only is this cleaner syntax, but you no longer need to import anything from the typing module.

02:41 This is kind of similar to some of the improvements in 3.11, where you could use the built-in types as type indicators, saving you from having to import capital-L List from the typing module, allowing you to just use list, the built-in, as a type specifier.

02:55 These features go a little deeper than this. They can can apply to classes in some other situations as well. If typing’s your thing and you want to learn all about the changes, go take a look at this tutorial.

03:11 A whole lot is going on inside the interpreter itself this time around. I’ll tell you about some of those changes next.

Become a Member to join the conversation.