Improving Error Messages
00:00 In the previous lesson, I showed you the new features of the REPL. In this lesson, I’ll highlight some of the improvements to error messages. Five years ago in Python 3.9, a new parser got implemented.
00:12 A new parser has allowed the core developers to improve on error messages that you might come across. I say you because I never see them. Nope, never. Not once.
00:21 Something something, a river in Egypt. Python 3.14 has continued these improvements and has changed both the output of some syntax errors as well as some of the runtime exceptions.
00:32 Let’s head back to the REPL to look at some intentionally broken code. I’ve got several different scenarios to show you. In the top window, I’ll show you what each scenario looked like in Python 3.13, and then in the bottom window, I’ll show you the improvements.
00:48
First off, errors caused by mistyping a keyword: forr. I don’t think that’s what they mean by code golf. In Python 3.13, it tells you that you have a syntax error, and unhelpfully points at the variable.
01:06
In 3.14, it highlights the keyword and makes a suggestion. In the top window, let me build a little if block here.
01:31
Yep. You aren’t allowed to put elif after else. At least this time it pointed at the right thing. Let’s try 3.14.
01:52 Hmm. That’s nice and very specific. When you alias an import to something else, that something else is known as an import target. The rules for that target are very specific.
02:04 Up top, the 3.13 version. It has to be a valid identifier, which of course doesn’t include subscripts. On the bottom, 3.14, and it’s a little more specific, recognizing the list object and telling you that you can’t do that.
02:26
Python uses prefixes to strings to change their behavior. The f-string allows you to format its contents. The b string specifies binary information.
02:40
There are prefixes that you can mix, but b and f aren’t two that can be used together. Down below in 3.14,
02:51 and it’s explicit about what caused the problem. If you get down into the weeds, the Python language consists of expressions and statements. An expression is something that evaluates to a value.
03:04 A statement is more general than that and includes things like keywords. Statements can also be expressions, but let’s skip right past that for the moment.
03:16 When using the ternary operator here, the end part needs to be an expression. It has to evaluate to something. Hence, the syntax error. In Python 3.14,
03:31 it gives you a little more information. Not sure how helpful this one is as I don’t know how many people outside of the compiler writers know the difference between expressions and statements, but it’s a bit better than the generic “invalid syntax”.
03:46 Python supports unpacking, meaning you can take a collection of things on the right-hand side of an assignment and put them into a series of variables on the left side of that assignment.
04:03 For unpacking to work, the number of things on the left and right need to match. Here, I’ve tried to unpack three pieces of fruit into two variables and I get an error. In the lower window,
04:21 Python 3.14 adds a little more information, this time telling you not just what you were trying to unpack into, but also what you were trying to unpack from.
04:31 In the REPL like this, it doesn’t make that much of a difference, but in your code, the right side might be a variable containing a list, and so this kind of error can be helpful in that situation.
04:41 Dictionaries are very useful, but they have one restriction: their keys must be hashable. Lists aren’t hashable, as this error is telling you. 3.14, in this case, it’s a bit more explicit as to the cause.
05:01 Rather than just telling you that you can’t hash a list, it also tells you why it was trying to hash something. Let’s try to violate the laws of the universe.
05:15 And if you’re not sure what a math domain error is, well, 3.14’s got your back.
05:25
You get a lot more help. No negative square roots for you. You’ll have to wait for a later lesson to get into complex numbers. A context manager is the kind of object that gets used by a with statement.
05:37 Not everything can be used that way.
05:44
str, for example, is not a context manager. In 3.14,
05:54
you get a little more information. The with block calls a __enter__() and __exit__() method on the object given to it.
06:02
So here, the message is telling you that no __exit__() was found. There are also async with blocks. Let me show you just one last error.
06:20
In Python 3.13, this gives you the same error as above, but in 3.14, not only do you get the better error, but it also recognizes that TaskGroup supports the async with protocol, and suggests that you may have forgotten your async keyword. Enough errors.
06:38 Next, I’ll show you some of the new syntax added to the language.
Become a Member to join the conversation.
