Loading video player…

Even More Improved Error Messages

00:00 In the previous lesson, I talked about leaving the GIL and using a JIT. In this lesson, I’ll show you the improvements to error messages in the latest release.

00:09 The last few versions of Python have improved the error messages that you see, hopefully making it easier to understand when something goes wrong. This trend continues with 3.13.

00:19 First off is colorization. Now by default, tracebacks are colorized in your terminal, making it simpler to distinguish between the stack and the error itself.

00:30 Python has so many modules that it’s easy to accidentally name your script the same as something in the standard library without realizing it. This can cause unexpected behavior.

00:40 Python lets you shadow the library, which is fine in theory, but in practice, if you then go to use something in the library, you’re going to get import errors.

00:49 To help you realize this problem, Python had already included the source file name in certain import errors, so you could realize where things were being looked for.

00:58 But this release has added some more hint text to the error as well. If you’ve shadowed a standard library, it will explicitly point this out for you,

01:07 and if you’ve just mucked up an import, it reminds you that one possible cause of this problem would be shadowing a library you might have installed.

01:16 In addition to this, the “did you mean” feature added a few releases back, has now been wired into function keyword arguments. Let’s go make some errors so you can see this in practice.

01:27 Is it still a mistake if I meant to do it?

01:31 On screen here, you can see my unfortunately named random.py file, which shadows the standard library module. Inside here, if I actually go to use the randint() function from the real module, I’ll get an import error as this file is what’s getting loaded rather than the standard library, and there’s no randint() in this file.

01:56 Let me run this. Then there’s two things to note here. First, the whole thing is colorized. And second, there is this error message. Python recognizes that I’ve shadowed a standard-library file and warns me that that might be the source of my error.

02:14 There’s a similar hint when you get an import error for a file that isn’t shadowing the standard library. On the screen here is numpy.py, which shadows the third-party module NumPy.

02:25 This is similar to my random.py example, but this time what’s being shadowed isn’t part of the standard library.

02:37 Once again with the color, and this time the hint is slightly different. I like the warning for when I shadow a standard-library module, but I’m not sure if I like this one.

02:46 Python doesn’t have some master list of what is and is not a third-party library, so this message pops up anytime you have this kind of attribute error. If you rename numpy.py to foo.py and import foo instead, you’ll get the same hint.

03:01 I’m not sure whether this is more or less confusing than without the error.

03:07 And finally, I’m back in the REPL to show the last improvement. Let me just write some problematic code.

03:21 And notice on the end here you’ve got a, “did you mean” based on the fact that max_split and maxsplit are almost the same? In this particular case, I think it should also tell you that you know, you’ve done it the right way and that the standard library’s naming policy is inconsistent.

03:37 But I digress. Pretty much every release adds features to the typing system, and 3.13 is no different. That’s up next.

Become a Member to join the conversation.