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

Improved Error Messages

00:00 In the previous lesson, I talked about the improvements to f-strings. In this lesson, I’ll show you better arrows. Did you mean errors? I mentioned the addition of the PEG parser In Python 3.9.

00:14 PEG has allowed more descriptive error messages, including did-you-mean semantics. For example, if you misspell something, it tries to guess what you meant and tell you about it.

00:26 Python 3.12 has added more error improvements. The contents of the standard library have now been added to the source of guesses for the did-you-mean semantics, as well as the members of a class.

00:40 Evidently, a common error during importation is to get the from module import name syntax in the wrong order. Rather than a plain SyntaxError, you now get a hint as to the correct way around.

00:55 And the contents of a module themselves are now in the guess list. So if you spell something in the imported module incorrectly, it will show in the did-you-mean error.

01:06 Let’s go write some broken code and see the errors. I’m in a Python 3.12 REPL here. Let me start by showing you something that worked before 3.12. Built-ins like print() were in the guess list for the did-you-mean code.

01:26 Compare pint with davis. davis isn’t close to anything, so it doesn’t get a did-you-mean to go with it, whereas pint is close to print and so it does.

01:38 The ability to look for these near matches in the builtins module was added in Python 3.10. Let me show you the built-ins.

01:53 There’s a lot there,

01:58 and you can see that print is inside of it. In addition to looking in builtins, Python 3.12 now looks in the standard library. As I haven’t imported math yet but it is part of the standard library, I get the did-you-mean message.

02:18 If I try that with miles, I just get a plain-style error instead. It isn’t close enough to anything in the guess list to get a did-you-mean.

02:29 Like with builtins, ou can see everything in the standard library. This time, instead of using a module directly though, you use the .stdlib_module_names member of the sys module,

02:47 which of course it finds math inside. Just for giggles, let’s look at all of them.

02:57 That’s even worse than builtins. Similar guess list stuff has been added to the inside of classes, with the members of the class now working with, did-you-mean style errors.

03:28 Consider my Talker class. Inside of the .say() method, I use message, which doesn’t actually exist. I probably mean the class member self.message, and now the error gives me that hint.

03:51 Python’s 3.10 guess list included everything in the local scope but didn’t work with self inside of a class. This actually created a bit of corner case.

04:01 When there was something in the local scope with a similar name to something in a class, it would did-you-mean on the global variable, possibly pointing you at the wrong thing.

04:11 Python 3.12 has fixed this by adding class context, and the class scope is checked first, fixing this potentially misleading error caused by the error message.

04:24 As I mentioned in the intro, a common mistake is to get the order of the from import syntax wrong. This is particularly easy for multilanguage coders, as other programming languages do this differently.

04:40 Python 3.12 explicitly looks for this kind of problem, and instead of giving a generic SyntaxError, it now gives you a bit of a hint. The did-you-mean feature has been added to the underlying import code as well.

04:59 If I try to import something from a module that isn’t there, it checks the module’s contents for near-named things and gives you a hint. Of cos I meant cosine.

05:10 Sorry. I’ll leave now. Okay, maybe I won’t leave. I’ll just head to the next lesson, which is about a few small additions to three different standard library modules.

Become a Member to join the conversation.