Improved Error Messages in Python
Python 3.12 brings several improvements to error messages, making them more precise and informative.
Standard Library Suggestions: Modules from the standard library are now potentially suggested as part of error messages when a NameError is raised to the top level.
>>> sys.version_info
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sys' is not defined. Did you forget to import 'sys'?
Attribute Suggestions for Instances: When a NameError is raised in a method and the instance has an attribute that matches the name in the exception, the suggestion will include self.<NAME> instead of the closest match in the method scope.
class A:
def __init__(self):
self.blech = 1
def foo(self):
somethin = blech
A().foo()
Traceback(most recent call last):
File "<stdin>", line 1
somethin = blech
^ ^ ^ ^ ^
NameError: name 'blech' is not defined. Did you mean: 'self.blech'?
SyntaxError Improvement: Syntax error messages are improved when the user types import x from y instead of from y import x.
>>> import a.y.z from b.y.z
Traceback (most recent call last):
File "<stdin>", line 1
import a.y.z from b.y.z
^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Did you mean to use 'from ... import ...' instead?
ImportError Suggestions: ImportError exceptions raised from failed from <module> import <name> statements now include suggestions for the value of <name> based on the available names in <module>.
>>> from collections import chainmap
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'chainmap' from 'collections'. Did you mean: 'ChainMap'?
Python 3.12 – What’s New and How to Download?
Python releases a new version almost every year. The latest version, that is, Python 3.12 was released on 2 October, 2023. This version introduced many new features and improvements. In this article, we will see some of the newly added features to Python 3.12.
Table of Content
- Download and Install Python
- Improved Error Messages in Python
- More Flexibility in Python F-String
- Type Parameter Syntax
- Improvement in Modules
- Syntactic Formalization of f-strings
- Per-Interpreter GIL
- Low Impact Monitoring for CPython
- Conclusion