Locked learning resources

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

Unlock This Lesson

Locked learning resources

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: Deprecations and Defaults

00:00 In the previous lesson, I showed you how error messages have improved. In this lesson, I’ll show you two of the new typing features.

00:08 The Python typing system allows you to annotate your code with additional information about the types of variables, arguments, and return values in methods and functions.

00:17 To use the typing system, you need a third-party tool. Code that fails the type check may still run. It’s the third-party tool that tells you something is fishy.

00:27 Most IDEs now do this for you, or you can install a command-line tool if you prefer. I’m going to use a third-party library called Pyright, which you can get through doing a pip install.

00:38 Once you’ve installed it, you run the pyright command, optionally, tell it what version of Python to check with, and give it one or more files that you want to check.

00:48 The first new typing feature isn’t even in the typing library oddly enough. The warnings module allows you to mark parts of your code with warnings for the user and future developers.

00:58 In this release, a deprecated decorator has been added. You decorate your deprecated code in order to warn coders interacting with it that it will change in the future.

01:09 Most warnings show when you use the code itself. This warning is part of the typing system instead and will only show up if you run a type checker. For Pyright, the default is not to even print deprecation warnings.

01:22 And since I want to demo deprecations, I’ve created a pyproject.toml file with some configuration to tell Pyright that I want it to squeal. In the top window here, I have a function called concatenate(), which concatenates two strings together.

01:38 Of course, Python has a way of doing this already, so I might want to remove this function and warn future coders that I’m going to be getting rid of it.

01:47 To do that, I can decorate the function with the deprecated decorator. You can even include a message about the deprecation, which in this case tells you just to use the string’s + operator instead.

01:59 Now, in the window below, I’ll run the type checker,

02:09 and you can see the warning here telling me that the function call on line eight of the file has been deprecated. Note that the line number is associated with the call, not the declaration.

02:21 Sometimes you want to determine what type gets used with something at runtime and to do this, Python supports typing templates. This is typically done with a container where you specify the type of the things in the container when you instantiate the container.

02:38 Python 3.13 has added a way of specifying a default type for your templates, saving you a little bit of code.

02:47 Say you wanted to create a queue that contained only one type of object. You could do this by writing your own class that wrapped the deque from the collections module.

02:57 To declare it as a template, you use square brackets. The capital T here is a placeholder allowing you to get at the type within your code. For example, here, when I instantiate the deque that’s being wrapped, I specify that it will contain type T, whatever that is.

03:13 Likewise, if I want to ensure the type checker validates types when I manipulate the queue, I add typing information in the push() and pop() methods.

03:22 In both these cases, I declare that the element being used must be of type T.

03:27 Then, when I create the queue, I have to use the same square bracket notation to specify what is allowed in the queue. In this case, it’s int.

03:37 I can call push() with an integer, and I can also call it with something that isn’t an integer because this is Python and Python doesn’t care.

03:45 But if I run a type checker on this, it will holler, possibly catching a bug.

03:51 Okay. That’s how typing templates work. Now, let’s look at the new feature in 3.13.

04:00 The concept is similar here, but now you can provide a default type for the template, in this case, an integer. The rest of the code in the Queue class is the same, but then down here, when I go to instantiate it, I don’t have to use the square brackets.

04:14 If I do, I can set it to something else, or if I don’t, it defaults to int.

04:20 And like before, if I try to push something besides the integer into it, it’ll work, but the type checker will holler.

04:29 Next up, even more improvements to the typing system.

Become a Member to join the conversation.