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

Better Errors & Colorization

00:00 In the previous lesson, I explained the new sentinel() function. In this lesson, I’ll show you a couple of things that might make your coding easier.

00:08 More improved error messages and colorization. Ever since the introduction of the PEG parser in Python 3.10, the core developers have been improving error messages for a variety of conditions.

00:20 Python 3.15 sees this work continue. This version adds “Did you mean” error messages for attempting to access an attribute on an object when you want to access it on an inner object, accidentally using common function names from other languages, and misspelling an attribute you’re trying to remove.

00:40 Let’s go write some faulty code. To demonstrate inner attribute references, I need a couple of objects.

00:54 I’ll start with a Customer object that has an email address.

01:05 And now I’ve added an Invoice object that contains an instance of a Customer. To reference the email from Invoice, I need self.customer.email, but it’s easy enough to forget the customer part.

01:21 And now the parser notices that kind of mistake and suggests the right value for you. Did you mean .customer.email? Why yes, I did. As someone who codes stuff for the web, I’m frequently switching back and forth between JavaScript, eww, on the front end and Python on the back end.

01:40 The result? I often write some weird hybrid between the two languages that works in neither place. Well, now Python has some hints for me.

01:52 This kind of mistake shows up on collections a lot. All languages have them, but there’s a variety of method names to accomplish the same thing. Instead of just saying what is push, the error now recognizes that from JavaScript’s array and tells me I might have meant .append() instead.

02:11 Likewise, for a variety of versions of toUpperCase,

02:21 it even recognizes put on a dictionary and tells you to use square bracket assignment instead. The “Did you mean” error message has been on attribute access for a while now.

02:34 In Python 3.15, it also works with attribute deletion. My generic object

02:45 with an attribute added,

02:50 and when I misspell the deletion, I get a “Did you mean” error. Python 3.14 added colorization to a variety of tools, including the REPL. Python 3.15 continues this work.

03:05 On the left is the newly colorized help message. On the top right is the built-in SQLite tool, which colorizes commands and table and column names. It also added tab completion as well.

03:18 And in the bottom right is the calendar module, which can be invoked from the command line to print out a calendar, now in color. Other modules got the color treatment as well.

03:28 The output from ast.dump() now has a colorized mode, along with command line invocations of the timeit and tokenize modules.

03:39 In every release, there are loads of smaller refinements. In the next lesson, I’ll run you through a few of them.

Become a Member to join the conversation.