Improved Error Messages
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. 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. And the contents of a module themselves are now in the guess list.
davis isn’t close to anything, so it doesn’t get a did-you-mean to go with it, whereas
pint is close to
builtins module was added in Python 3.10. Let me show you the built-ins.
and you can see that
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.
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. 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.
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.
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.
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. 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.