02-28-2023, 10:25 PM
Release 3.11.2
Date February 28, 2023
Editor Pablo Galindo Salgado
This article explains the new features in Python 3.11, compared to 3.10.
Summary – Release highlights
PEP 657: Fine-grained error locations in tracebacks
When printing tracebacks, the interpreter will now point to the exact expression that caused the error, instead of just the line. For example:
Traceback (most recent call last):
File "distance.py", line 11, in <module>
print(manhattan_distance(p1, p2))
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "distance.py", line 6, in manhattan_distance
return abs(point_1.x - point_2.x) + abs(point_1.y - point_2.y)
AttributeError: 'NoneType' object has no attribute 'x'
Previous versions of the interpreter would point to just the line, making it ambiguous which object was None
. These enhanced errors can also be helpful when dealing with deeply nested dict
objects and multiple function calls:
Traceback (most recent call last):
File "query.py", line 37, in <module>
magic_arithmetic('foo')
File "query.py", line 18, in magic_arithmetic
return add_counts(x) / 25
^^^^^^^^^^^^^
File "query.py", line 24, in add_counts
return 25 + query_user(user1) + query_user(user2)
^^^^^^^^^^^^^^^^^
File "query.py", line 32, in query_user
return 1 + query_count(db, response['a']['b']['c']['user'], retry=True)
~~~~~~~~~~~~~~~~~~^^^^^
TypeError: 'NoneType' object is not subscriptable
As well as complex arithmetic expressions:
Traceback (most recent call last):
File "calculation.py", line 54, in <module>
result = (x / y / z) * (a / b / c)
~~~~~~^~~
ZeroDivisionError: division by zero
Additionally, the information used by the enhanced traceback feature is made available via a general API, that can be used to correlate bytecode instructions with source code location. This information can be retrieved using:
Date February 28, 2023
Editor Pablo Galindo Salgado
This article explains the new features in Python 3.11, compared to 3.10.
Summary – Release highlights
- Python 3.11 is between 10-60% faster than Python 3.10. On average, we measured a 1.25x speedup on the standard benchmark suite.
- PEP 657: Fine-grained error locations in tracebacks
- New -Pcommand line option and \PYTHONSAFEPATHenvironment variable to disable automatically
- prepending potentially unsafe paths to sys.path
- PEP 646: Variadic generics
- PEP 655: Marking individual TypedDict items as required or not-required
- PEP 673: Self type
- PEP 675: Arbitrary literal string type
- PEP 681: Data class transforms
- PEP 594: Many legacy standard library modules have been deprecated and will be removed in Python 3.13
- PEP 624: Py_UNICODE encoder APIs have been removed
- PEP 670: Macros converted to static inline functions
PEP 657: Fine-grained error locations in tracebacks
When printing tracebacks, the interpreter will now point to the exact expression that caused the error, instead of just the line. For example:
Traceback (most recent call last):
File "distance.py", line 11, in <module>
print(manhattan_distance(p1, p2))
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "distance.py", line 6, in manhattan_distance
return abs(point_1.x - point_2.x) + abs(point_1.y - point_2.y)
AttributeError: 'NoneType' object has no attribute 'x'
Previous versions of the interpreter would point to just the line, making it ambiguous which object was None
. These enhanced errors can also be helpful when dealing with deeply nested dict
objects and multiple function calls:
Traceback (most recent call last):
File "query.py", line 37, in <module>
magic_arithmetic('foo')
File "query.py", line 18, in magic_arithmetic
return add_counts(x) / 25
^^^^^^^^^^^^^
File "query.py", line 24, in add_counts
return 25 + query_user(user1) + query_user(user2)
^^^^^^^^^^^^^^^^^
File "query.py", line 32, in query_user
return 1 + query_count(db, response['a']['b']['c']['user'], retry=True)
~~~~~~~~~~~~~~~~~~^^^^^
TypeError: 'NoneType' object is not subscriptable
As well as complex arithmetic expressions:
Traceback (most recent call last):
File "calculation.py", line 54, in <module>
result = (x / y / z) * (a / b / c)
~~~~~~^~~
ZeroDivisionError: division by zero
Additionally, the information used by the enhanced traceback feature is made available via a general API, that can be used to correlate bytecode instructions with source code location. This information can be retrieved using:
- The codeobject.co_positions() method in Python.
- The PyCode_Addr2Location()function in the C API.