Spaces:
Running on Zero
A newer version of the Gradio SDK is available: 6.22.0
What's New In Python 3.10
Editor : Pablo Galindo Salgado
* Anyone can add text to this document. Do not spend very much time on the wording of your changes, because your text will probably get rewritten to some degree.
* The maintainer will go through Misc/NEWS periodically and add changes; it's therefore more important to add your changes to Misc/NEWS than to this file.
* This is not a complete list of every single change; completeness is the purpose of Misc/NEWS. Some changes I consider too small or esoteric to include. If such a change is added to the text, I'll just remove it. (This is another reason you shouldn't spend too much time on writing your addition.)
* If you want to draw your new text to the attention of the maintainer, add 'XXX' to the beginning of the paragraph or section.
* It's OK to just add a fragmentary note about a change. For example: "XXX Describe the transmogrify() function added to the socket module." The maintainer will research the change and write the necessary text.
* You can comment out your additions if you like, but it's not necessary (especially when a final release is some months away).
* Credit the author of a patch or bugfix. Just the name is sufficient; the e-mail address isn't necessary.
- It's helpful to add the bug/patch number as a comment:
XXX Describe the transmogrify() function added to the socket module. (Contributed by P.Y. Developer in
12345{.interpreted-text role="issue"}.)This saves the maintainer the effort of going through the git log when researching a change.
This article explains the new features in Python 3.10, compared to 3.9. Python 3.10 was released on October 4, 2021. For full details, see the changelog <changelog>{.interpreted-text role="ref"}.
Summary -- Release highlights
New syntax features:
634{.interpreted-text role="pep"}, Structural Pattern Matching: Specification635{.interpreted-text role="pep"}, Structural Pattern Matching: Motivation and Rationale636{.interpreted-text role="pep"}, Structural Pattern Matching: Tutorial12782{.interpreted-text role="issue"}, Parenthesized context managers are now officially allowed.
New features in the standard library:
618{.interpreted-text role="pep"}, Add Optional Length-Checking To zip.
Interpreter improvements:
626{.interpreted-text role="pep"}, Precise line numbers for debugging and other tools.
New typing features:
604{.interpreted-text role="pep"}, Allow writing union types as X | Y612{.interpreted-text role="pep"}, Parameter Specification Variables613{.interpreted-text role="pep"}, Explicit Type Aliases647{.interpreted-text role="pep"}, User-Defined Type Guards
Important deprecations, removals or restrictions:
644{.interpreted-text role="pep"}, Require OpenSSL 1.1.1 or newer632{.interpreted-text role="pep"}, Deprecate distutils module.623{.interpreted-text role="pep"}, Deprecate and prepare for the removal of the wstr member in PyUnicodeObject.624{.interpreted-text role="pep"}, Remove Py_UNICODE encoder APIs597{.interpreted-text role="pep"}, Add optional EncodingWarning
New Features
Parenthesized context managers {#whatsnew310-pep563}
Using enclosing parentheses for continuation across multiple lines in context managers is now supported. This allows formatting a long collection of context managers in multiple lines in a similar way as it was previously possible with import statements. For instance, all these examples are now valid:
with (CtxManager() as example):
...
with (
CtxManager1(),
CtxManager2()
):
...
with (CtxManager1() as example,
CtxManager2()):
...
with (CtxManager1(),
CtxManager2() as example):
...
with (
CtxManager1() as example1,
CtxManager2() as example2
):
...
it is also possible to use a trailing comma at the end of the enclosed group:
with (
CtxManager1() as example1,
CtxManager2() as example2,
CtxManager3() as example3,
):
...
This new syntax uses the non LL(1) capacities of the new parser. Check 617{.interpreted-text role="pep"} for more details.
(Contributed by Guido van Rossum, Pablo Galindo and Lysandros Nikolaou in 12782{.interpreted-text role="issue"} and 40334{.interpreted-text role="issue"}.)
Better error messages
SyntaxErrors
When parsing code that contains unclosed parentheses or brackets the interpreter now includes the location of the unclosed bracket of parentheses instead of displaying SyntaxError: unexpected EOF while parsing or pointing to some incorrect location. For instance, consider the following code (notice the unclosed '{'):
expected = {9: 1, 18: 2, 19: 2, 27: 3, 28: 3, 29: 3, 36: 4, 37: 4,
38: 4, 39: 4, 45: 5, 46: 5, 47: 5, 48: 5, 49: 5, 54: 6,
some_other_code = foo()
Previous versions of the interpreter reported confusing places as the location of the syntax error:
File "example.py", line 3
some_other_code = foo()
^
SyntaxError: invalid syntax
but in Python 3.10 a more informative error is emitted:
File "example.py", line 1
expected = {9: 1, 18: 2, 19: 2, 27: 3, 28: 3, 29: 3, 36: 4, 37: 4,
^
SyntaxError: '{' was never closed
In a similar way, errors involving unclosed string literals (single and triple quoted) now point to the start of the string instead of reporting EOF/EOL.
These improvements are inspired by previous work in the PyPy interpreter.
(Contributed by Pablo Galindo in 42864{.interpreted-text role="issue"} and Batuhan Taskaya in 40176{.interpreted-text role="issue"}.)
SyntaxError{.interpreted-text role="exc"} exceptions raised by the interpreter will now highlight the full error range of the expression that constitutes the syntax error itself, instead of just where the problem is detected. In this way, instead of displaying (before Python 3.10):
>>> foo(x, z for z in range(10), t, w)
File "<stdin>", line 1
foo(x, z for z in range(10), t, w)
^
SyntaxError: Generator expression must be parenthesized
now Python 3.10 will display the exception as:
>>> foo(x, z for z in range(10), t, w)
File "<stdin>", line 1
foo(x, z for z in range(10), t, w)
^^^^^^^^^^^^^^^^^^^^
SyntaxError: Generator expression must be parenthesized
This improvement was contributed by Pablo Galindo in 43914{.interpreted-text role="issue"}.
A considerable amount of new specialized messages for SyntaxError{.interpreted-text role="exc"} exceptions have been incorporated. Some of the most notable ones are as follows:
Missing
:before blocks:>>> if rocket.position > event_horizon File "<stdin>", line 1 if rocket.position > event_horizon ^ SyntaxError: expected ':'(Contributed by Pablo Galindo in
42997{.interpreted-text role="issue"}.)Unparenthesised tuples in comprehensions targets:
>>> {x,y for x,y in zip('abcd', '1234')} File "<stdin>", line 1 {x,y for x,y in zip('abcd', '1234')} ^ SyntaxError: did you forget parentheses around the comprehension target?(Contributed by Pablo Galindo in
43017{.interpreted-text role="issue"}.)Missing commas in collection literals and between expressions:
>>> items = { ... x: 1, ... y: 2 ... z: 3, File "<stdin>", line 3 y: 2 ^ SyntaxError: invalid syntax. Perhaps you forgot a comma?(Contributed by Pablo Galindo in
43822{.interpreted-text role="issue"}.)Multiple Exception types without parentheses:
>>> try: ... build_dyson_sphere() ... except NotEnoughScienceError, NotEnoughResourcesError: File "<stdin>", line 3 except NotEnoughScienceError, NotEnoughResourcesError: ^ SyntaxError: multiple exception types must be parenthesized(Contributed by Pablo Galindo in
43149{.interpreted-text role="issue"}.)Missing
:and values in dictionary literals:>>> values = { ... x: 1, ... y: 2, ... z: ... } File "<stdin>", line 4 z: ^ SyntaxError: expression expected after dictionary key and ':' >>> values = {x:1, y:2, z w:3} File "<stdin>", line 1 values = {x:1, y:2, z w:3} ^ SyntaxError: ':' expected after dictionary key(Contributed by Pablo Galindo in
43823{.interpreted-text role="issue"}.)tryblocks withoutexceptorfinallyblocks:>>> try: ... x = 2 ... something = 3 File "<stdin>", line 3 something = 3 ^^^^^^^^^ SyntaxError: expected 'except' or 'finally' block(Contributed by Pablo Galindo in
44305{.interpreted-text role="issue"}.)Usage of
=instead of==in comparisons:>>> if rocket.position = event_horizon: File "<stdin>", line 1 if rocket.position = event_horizon: ^ SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?(Contributed by Pablo Galindo in
43797{.interpreted-text role="issue"}.)Usage of
*in f-strings:>>> f"Black holes {*all_black_holes} and revelations" File "<stdin>", line 1 (*all_black_holes) ^ SyntaxError: f-string: cannot use starred expression here(Contributed by Pablo Galindo in
41064{.interpreted-text role="issue"}.)
IndentationErrors
Many IndentationError{.interpreted-text role="exc"} exceptions now have more context regarding what kind of block was expecting an indentation, including the location of the statement:
>>> def foo():
... if lel:
... x = 2
File "<stdin>", line 3
x = 2
^
IndentationError: expected an indented block after 'if' statement in line 2
AttributeErrors
When printing AttributeError{.interpreted-text role="exc"}, !PyErr_Display{.interpreted-text role="c:func"} will offer suggestions of similar attribute names in the object that the exception was raised from:
>>> collections.namedtoplo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'collections' has no attribute 'namedtoplo'. Did you mean: namedtuple?
(Contributed by Pablo Galindo in 38530{.interpreted-text role="issue"}.)
:::: warning ::: title Warning :::
Notice this won't work if !PyErr_Display{.interpreted-text role="c:func"} is not called to display the error which can happen if some other custom error display function is used. This is a common scenario in some REPLs like IPython.
::::
NameErrors
When printing NameError{.interpreted-text role="exc"} raised by the interpreter, !PyErr_Display{.interpreted-text role="c:func"} will offer suggestions of similar variable names in the function that the exception was raised from:
>>> schwarzschild_black_hole = None
>>> schwarschild_black_hole
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'schwarschild_black_hole' is not defined. Did you mean: schwarzschild_black_hole?
(Contributed by Pablo Galindo in 38530{.interpreted-text role="issue"}.)
:::: warning ::: title Warning :::
Notice this won't work if !PyErr_Display{.interpreted-text role="c:func"} is not called to display the error, which can happen if some other custom error display function is used. This is a common scenario in some REPLs like IPython.
::::
PEP 626: Precise line numbers for debugging and other tools
PEP 626 brings more precise and reliable line numbers for debugging, profiling and coverage tools. Tracing events, with the correct line number, are generated for all lines of code executed and only for lines of code that are executed.
The ~frame.f_lineno{.interpreted-text role="attr"} attribute of frame objects will always contain the expected line number.
The ~codeobject.co_lnotab{.interpreted-text role="attr"} attribute of code objects <code-objects>{.interpreted-text role="ref"} is deprecated and will be removed in 3.12. Code that needs to convert from offset to line number should use the new ~codeobject.co_lines{.interpreted-text role="meth"} method instead.
PEP 634: Structural Pattern Matching
Structural pattern matching has been added in the form of a match statement and case statements of patterns with associated actions. Patterns consist of sequences, mappings, primitive data types as well as class instances. Pattern matching enables programs to extract information from complex data types, branch on the structure of data, and apply specific actions based on different forms of data.
Syntax and operations
The generic syntax of pattern matching is:
match subject:
case <pattern_1>:
<action_1>
case <pattern_2>:
<action_2>
case <pattern_3>:
<action_3>
case _:
<action_wildcard>
A match statement takes an expression and compares its value to successive patterns given as one or more case blocks. Specifically, pattern matching operates by:
- using data with type and shape (the
subject) - evaluating the
subjectin thematchstatement - comparing the subject with each pattern in a
casestatement from top to bottom until a match is confirmed. - executing the action associated with the pattern of the confirmed match
- If an exact match is not confirmed, the last case, a wildcard
_, if provided, will be used as the matching case. If an exact match is not confirmed and a wildcard case does not exist, the entire match block is a no-op.
Declarative approach
Readers may be aware of pattern matching through the simple example of matching a subject (data object) to a literal (pattern) with the switch statement found in C, Java or JavaScript (and many other languages). Often the switch statement is used for comparison of an object/expression with case statements containing literals.
More powerful examples of pattern matching can be found in languages such as Scala and Elixir. With structural pattern matching, the approach is "declarative" and explicitly states the conditions (the patterns) for data to match.
While an "imperative" series of instructions using nested "if" statements could be used to accomplish something similar to structural pattern matching, it is less clear than the "declarative" approach. Instead the "declarative" approach states the conditions to meet for a match and is more readable through its explicit patterns. While structural pattern matching can be used in its simplest form comparing a variable to a literal in a case statement, its true value for Python lies in its handling of the subject's type and shape.
Simple pattern: match to a literal
Let's look at this example as pattern matching in its simplest form: a value, the subject, being matched to several literals, the patterns. In the example below, status is the subject of the match statement. The patterns are each of the case statements, where literals represent request status codes. The associated action to the case is executed after a match:
def http_error(status):
match status:
case 400:
return "Bad request"
case 404:
return "Not found"
case 418:
return "I'm a teapot"
case _:
return "Something's wrong with the internet"
If the above function is passed a status of 418, "I'm a teapot" is returned. If the above function is passed a status of 500, the case statement with _ will match as a wildcard, and "Something's wrong with the internet" is returned. Note the last block: the variable name, _, acts as a wildcard and insures the subject will always match. The use of _ is optional.
You can combine several literals in a single pattern using | ("or"):
case 401 | 403 | 404:
return "Not allowed"
Behavior without the wildcard
If we modify the above example by removing the last case block, the example becomes:
def http_error(status):
match status:
case 400:
return "Bad request"
case 404:
return "Not found"
case 418:
return "I'm a teapot"
Without the use of _ in a case statement, a match may not exist. If no match exists, the behavior is a no-op. For example, if status of 500 is passed, a no-op occurs.
Patterns with a literal and variable
Patterns can look like unpacking assignments, and a pattern may be used to bind variables. In this example, a data point can be unpacked to its x-coordinate and y-coordinate:
# point is an (x, y) tuple
match point:
case (0, 0):
print("Origin")
case (0, y):
print(f"Y={y}")
case (x, 0):
print(f"X={x}")
case (x, y):
print(f"X={x}, Y={y}")
case _:
raise ValueError("Not a point")
The first pattern has two literals, (0, 0), and may be thought of as an extension of the literal pattern shown above. The next two patterns combine a literal and a variable, and the variable binds a value from the subject (point). The fourth pattern captures two values, which makes it conceptually similar to the unpacking assignment (x, y) = point.
Patterns and classes
If you are using classes to structure your data, you can use as a pattern the class name followed by an argument list resembling a constructor. This pattern has the ability to capture instance attributes into variables:
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def location(point):
match point:
case Point(x=0, y=0):
print("Origin is the point's location.")
case Point(x=0, y=y):
print(f"Y={y} and the point is on the y-axis.")
case Point(x=x, y=0):
print(f"X={x} and the point is on the x-axis.")
case Point():
print("The point is located somewhere else on the plane.")
case _:
print("Not a point")
Patterns with positional parameters
You can use positional parameters with some builtin classes that provide an ordering for their attributes (e.g. dataclasses). You can also define a specific position for attributes in patterns by setting the __match_args__ special attribute in your classes. If it's set to ("x", "y"), the following patterns are all equivalent (and all bind the y attribute to the var variable):
Point(1, var)
Point(1, y=var)
Point(x=1, y=var)
Point(y=var, x=1)
Nested patterns
Patterns can be arbitrarily nested. For example, if our data is a short list of points, it could be matched like this:
match points:
case []:
print("No points in the list.")
case [Point(0, 0)]:
print("The origin is the only point in the list.")
case [Point(x, y)]:
print(f"A single point {x}, {y} is in the list.")
case [Point(0, y1), Point(0, y2)]:
print(f"Two points on the Y axis at {y1}, {y2} are in the list.")
case _:
print("Something else is found in the list.")
Complex patterns and the wildcard
To this point, the examples have used _ alone in the last case statement. A wildcard can be used in more complex patterns, such as ('error', code, _). For example:
match test_variable:
case ('warning', code, 40):
print("A warning has been received.")
case ('error', code, _):
print(f"An error {code} occurred.")
In the above case, test_variable will match for ('error', code, 100) and ('error', code, 800).
Guard
We can add an if clause to a pattern, known as a "guard". If the guard is false, match goes on to try the next case block. Note that value capture happens before the guard is evaluated:
match point:
case Point(x, y) if x == y:
print(f"The point is located on the diagonal Y=X at {x}.")
case Point(x, y):
print(f"Point is not on the diagonal.")
Other Key Features
Several other key features:
Like unpacking assignments, tuple and list patterns have exactly the same meaning and actually match arbitrary sequences. Technically, the subject must be a sequence. Therefore, an important exception is that patterns don't match iterators. Also, to prevent a common mistake, sequence patterns don't match strings.
Sequence patterns support wildcards:
[x, y, *rest]and(x, y, *rest)work similar to wildcards in unpacking assignments. The name after*may also be_, so(x, y, *_)matches a sequence of at least two items without binding the remaining items.Mapping patterns:
{"bandwidth": b, "latency": l}captures the"bandwidth"and"latency"values from a dict. Unlike sequence patterns, extra keys are ignored. A wildcard**restis also supported. (But**_would be redundant, so is not allowed.)Subpatterns may be captured using the
askeyword:case (Point(x1, y1), Point(x2, y2) as p2): ...This binds x1, y1, x2, y2 like you would expect without the
asclause, and p2 to the entire second item of the subject.Most literals are compared by equality. However, the singletons
True,FalseandNoneare compared by identity.Named constants may be used in patterns. These named constants must be dotted names to prevent the constant from being interpreted as a capture variable:
from enum import Enum class Color(Enum): RED = 0 GREEN = 1 BLUE = 2 color = Color.GREEN match color: case Color.RED: print("I see red!") case Color.GREEN: print("Grass is green") case Color.BLUE: print("I'm feeling the blues :(")
For the full specification see 634{.interpreted-text role="pep"}. Motivation and rationale are in 635{.interpreted-text role="pep"}, and a longer tutorial is in 636{.interpreted-text role="pep"}.
Optional EncodingWarning and encoding="locale" option {#whatsnew310-pep597}
The default encoding of ~io.TextIOWrapper{.interpreted-text role="class"} and open{.interpreted-text role="func"} is platform and locale dependent. Since UTF-8 is used on most Unix platforms, omitting encoding option when opening UTF-8 files (e.g. JSON, YAML, TOML, Markdown) is a very common bug. For example:
# BUG: "rb" mode or encoding="utf-8" should be used.
with open("data.json") as f:
data = json.load(f)
To find this type of bug, an optional EncodingWarning is added. It is emitted when sys.flags.warn_default_encoding <sys.flags>{.interpreted-text role="data"} is true and locale-specific default encoding is used.
-X warn_default_encoding option and PYTHONWARNDEFAULTENCODING{.interpreted-text role="envvar"} are added to enable the warning.
See io-text-encoding{.interpreted-text role="ref"} for more information.
New Features Related to Type Hints {#new-feat-related-type-hints}
This section covers major changes affecting 484{.interpreted-text role="pep"} type hints and the typing{.interpreted-text role="mod"} module.
PEP 604: New Type Union Operator
A new type union operator was introduced which enables the syntax X | Y. This provides a cleaner way of expressing 'either type X or type Y' instead of using typing.Union{.interpreted-text role="class"}, especially in type hints.
In previous versions of Python, to apply a type hint for functions accepting arguments of multiple types, typing.Union{.interpreted-text role="class"} was used:
def square(number: Union[int, float]) -> Union[int, float]:
return number ** 2
Type hints can now be written in a more succinct manner:
def square(number: int | float) -> int | float:
return number ** 2
This new syntax is also accepted as the second argument to isinstance{.interpreted-text role="func"} and issubclass{.interpreted-text role="func"}:
>>> isinstance(1, int | str)
True
See types-union{.interpreted-text role="ref"} and 604{.interpreted-text role="pep"} for more details.
(Contributed by Maggie Moss and Philippe Prados in 41428{.interpreted-text role="issue"}, with additions by Yurii Karabas and Serhiy Storchaka in 44490{.interpreted-text role="issue"}.)
PEP 612: Parameter Specification Variables
Two new options to improve the information provided to static type checkers for 484{.interpreted-text role="pep"}'s Callable have been added to the typing{.interpreted-text role="mod"} module.
The first is the parameter specification variable. They are used to forward the parameter types of one callable to another callable -- a pattern commonly found in higher order functions and decorators. Examples of usage can be found in typing.ParamSpec{.interpreted-text role="class"}. Previously, there was no easy way to type annotate dependency of parameter types in such a precise manner.
The second option is the new Concatenate operator. It's used in conjunction with parameter specification variables to type annotate a higher order callable which adds or removes parameters of another callable. Examples of usage can be found in typing.Concatenate{.interpreted-text role="class"}.
See typing.Callable{.interpreted-text role="class"}, typing.ParamSpec{.interpreted-text role="class"}, typing.Concatenate{.interpreted-text role="class"}, typing.ParamSpecArgs{.interpreted-text role="class"}, typing.ParamSpecKwargs{.interpreted-text role="class"}, and 612{.interpreted-text role="pep"} for more details.
(Contributed by Ken Jin in 41559{.interpreted-text role="issue"}, with minor enhancements by Jelle Zijlstra in 43783{.interpreted-text role="issue"}. PEP written by Mark Mendoza.)
PEP 613: TypeAlias
484{.interpreted-text role="pep"} introduced the concept of type aliases, only requiring them to be top-level unannotated assignments. This simplicity sometimes made it difficult for type checkers to distinguish between type aliases and ordinary assignments, especially when forward references or invalid types were involved. Compare:
StrCache = 'Cache[str]' # a type alias
LOG_PREFIX = 'LOG[DEBUG]' # a module constant
Now the typing{.interpreted-text role="mod"} module has a special value ~typing.TypeAlias{.interpreted-text role="data"} which lets you declare type aliases more explicitly:
StrCache: TypeAlias = 'Cache[str]' # a type alias
LOG_PREFIX = 'LOG[DEBUG]' # a module constant
See 613{.interpreted-text role="pep"} for more details.
(Contributed by Mikhail Golubev in 41923{.interpreted-text role="issue"}.)
PEP 647: User-Defined Type Guards
~typing.TypeGuard{.interpreted-text role="data"} has been added to the typing{.interpreted-text role="mod"} module to annotate type guard functions and improve information provided to static type checkers during type narrowing. For more information, please see ~typing.TypeGuard{.interpreted-text role="data"}'s documentation, and 647{.interpreted-text role="pep"}.
(Contributed by Ken Jin and Guido van Rossum in 43766{.interpreted-text role="issue"}. PEP written by Eric Traut.)
Other Language Changes
- The
int{.interpreted-text role="class"} type has a new methodint.bit_count{.interpreted-text role="meth"}, returning the number of ones in the binary expansion of a given integer, also known as the population count. (Contributed by Niklas Fiekas in29882{.interpreted-text role="issue"}.) - The views returned by
dict.keys{.interpreted-text role="meth"},dict.values{.interpreted-text role="meth"} anddict.items{.interpreted-text role="meth"} now all have amappingattribute that gives atypes.MappingProxyType{.interpreted-text role="class"} object wrapping the original dictionary. (Contributed by Dennis Sweeney in40890{.interpreted-text role="issue"}.) 618{.interpreted-text role="pep"}: Thezip{.interpreted-text role="func"} function now has an optionalstrictflag, used to require that all the iterables have an equal length.- Builtin and extension functions that take integer arguments no longer accept
~decimal.Decimal{.interpreted-text role="class"}s,~fractions.Fraction{.interpreted-text role="class"}s and other objects that can be converted to integers only with a loss (e.g. that have the~object.__int__{.interpreted-text role="meth"} method but do not have the~object.__index__{.interpreted-text role="meth"} method). (Contributed by Serhiy Storchaka in37999{.interpreted-text role="issue"}.) - If
object.__ipow__{.interpreted-text role="func"} returnsNotImplemented{.interpreted-text role="data"}, the operator will correctly fall back toobject.__pow__{.interpreted-text role="func"} andobject.__rpow__{.interpreted-text role="func"} as expected. (Contributed by Alex Shkop in38302{.interpreted-text role="issue"}.) - Assignment expressions can now be used unparenthesized within set literals and set comprehensions, as well as in sequence indexes (but not slices).
- Functions have a new
__builtins__attribute which is used to look for builtin symbols when a function is executed, instead of looking into__globals__['__builtins__']. The attribute is initialized from__globals__["__builtins__"]if it exists, else from the current builtins. (Contributed by Mark Shannon in42990{.interpreted-text role="issue"}.) - Two new builtin functions --
aiter{.interpreted-text role="func"} andanext{.interpreted-text role="func"} have been added to provide asynchronous counterparts toiter{.interpreted-text role="func"} andnext{.interpreted-text role="func"}, respectively. (Contributed by Joshua Bronson, Daniel Pope, and Justin Wang in31861{.interpreted-text role="issue"}.) - Static methods (
staticmethod{.interpreted-text role="deco"}) and class methods (classmethod{.interpreted-text role="deco"}) now inherit the method attributes (__module__,__name__,__qualname__,__doc__,__annotations__) and have a new__wrapped__attribute. Moreover, static methods are now callable as regular functions. (Contributed by Victor Stinner in43682{.interpreted-text role="issue"}.) - Annotations for complex targets (everything beside
simple nametargets defined by526{.interpreted-text role="pep"}) no longer cause any runtime effects withfrom __future__ import annotations. (Contributed by Batuhan Taskaya in42737{.interpreted-text role="issue"}.) - Class and module objects now lazy-create empty annotations dicts on demand. The annotations dicts are stored in the object's
__dict__for backwards compatibility. This improves the best practices for working with__annotations__; for more information, please seeannotations-howto{.interpreted-text role="ref"}. (Contributed by Larry Hastings in43901{.interpreted-text role="issue"}.) - Annotations consist of
yield,yield from,awaitor named expressions are now forbidden underfrom __future__ import annotationsdue to their side effects. (Contributed by Batuhan Taskaya in42725{.interpreted-text role="issue"}.) - Usage of unbound variables,
super()and other expressions that might alter the processing of symbol table as annotations are now rendered effectless underfrom __future__ import annotations. (Contributed by Batuhan Taskaya in42725{.interpreted-text role="issue"}.) - Hashes of NaN values of both
float{.interpreted-text role="class"} type anddecimal.Decimal{.interpreted-text role="class"} type now depend on object identity. Formerly, they always hashed to0even though NaN values are not equal to one another. This caused potentially quadratic runtime behavior due to excessive hash collisions when creating dictionaries and sets containing multiple NaNs. (Contributed by Raymond Hettinger in43475{.interpreted-text role="issue"}.) - A
SyntaxError{.interpreted-text role="exc"} (instead of aNameError{.interpreted-text role="exc"}) will be raised when deleting the__debug__{.interpreted-text role="const"} constant. (Contributed by Donghee Na in45000{.interpreted-text role="issue"}.) SyntaxError{.interpreted-text role="exc"} exceptions now haveend_linenoandend_offsetattributes. They will beNoneif not determined. (Contributed by Pablo Galindo in43914{.interpreted-text role="issue"}.)
New Modules
- None.
Improved Modules
asyncio
Add missing ~asyncio.loop.connect_accepted_socket{.interpreted-text role="meth"} method. (Contributed by Alex Grönholm in 41332{.interpreted-text role="issue"}.)
argparse
Misleading phrase "optional arguments" was replaced with "options" in argparse help. Some tests might require adaptation if they rely on exact output match. (Contributed by Raymond Hettinger in 9694{.interpreted-text role="issue"}.)
array
The ~array.array.index{.interpreted-text role="meth"} method of array.array{.interpreted-text role="class"} now has optional start and stop parameters. (Contributed by Anders Lorentsen and Zackery Spytz in 31956{.interpreted-text role="issue"}.)
asynchat, asyncore, smtpd
These modules have been marked as deprecated in their module documentation since Python 3.6. An import-time DeprecationWarning{.interpreted-text role="class"} has now been added to all three of these modules.
base64
Add base64.b32hexencode{.interpreted-text role="func"} and base64.b32hexdecode{.interpreted-text role="func"} to support the Base32 Encoding with Extended Hex Alphabet.
bdb
Add !clearBreakpoints{.interpreted-text role="meth"} to reset all set breakpoints. (Contributed by Irit Katriel in 24160{.interpreted-text role="issue"}.)
bisect
Added the possibility of providing a key function to the APIs in the bisect{.interpreted-text role="mod"} module. (Contributed by Raymond Hettinger in 4356{.interpreted-text role="issue"}.)
codecs
Add a codecs.unregister{.interpreted-text role="func"} function to unregister a codec search function. (Contributed by Hai Shi in 41842{.interpreted-text role="issue"}.)
collections.abc
The __args__ of the parameterized generic <types-genericalias>{.interpreted-text role="ref"} for collections.abc.Callable{.interpreted-text role="class"} are now consistent with typing.Callable{.interpreted-text role="data"}. collections.abc.Callable{.interpreted-text role="class"} generic now flattens type parameters, similar to what typing.Callable{.interpreted-text role="data"} currently does. This means that collections.abc.Callable[[int, str], str] will have __args__ of (int, str, str); previously this was ([int, str], str). To allow this change, types.GenericAlias{.interpreted-text role="class"} can now be subclassed, and a subclass will be returned when subscripting the collections.abc.Callable{.interpreted-text role="class"} type. Note that a TypeError{.interpreted-text role="exc"} may be raised for invalid forms of parameterizing collections.abc.Callable{.interpreted-text role="class"} which may have passed silently in Python 3.9. (Contributed by Ken Jin in 42195{.interpreted-text role="issue"}.)
contextlib
Add a contextlib.aclosing{.interpreted-text role="func"} context manager to safely close async generators and objects representing asynchronously released resources. (Contributed by Joongi Kim and John Belmonte in 41229{.interpreted-text role="issue"}.)
Add asynchronous context manager support to contextlib.nullcontext{.interpreted-text role="func"}. (Contributed by Tom Gringauz in 41543{.interpreted-text role="issue"}.)
Add ~contextlib.AsyncContextDecorator{.interpreted-text role="class"}, for supporting usage of async context managers as decorators.
curses
The extended color functions added in ncurses 6.1 will be used transparently by curses.color_content{.interpreted-text role="func"}, curses.init_color{.interpreted-text role="func"}, curses.init_pair{.interpreted-text role="func"}, and curses.pair_content{.interpreted-text role="func"}. A new function, curses.has_extended_color_support{.interpreted-text role="func"}, indicates whether extended color support is provided by the underlying ncurses library. (Contributed by Jeffrey Kintscher and Hans Petter Jansson in 36982{.interpreted-text role="issue"}.)
The BUTTON5_* constants are now exposed in the curses{.interpreted-text role="mod"} module if they are provided by the underlying curses library. (Contributed by Zackery Spytz in 39273{.interpreted-text role="issue"}.)
dataclasses
__slots__
Added slots parameter in dataclasses.dataclass{.interpreted-text role="func"} decorator. (Contributed by Yurii Karabas in 42269{.interpreted-text role="issue"})
Keyword-only fields
dataclasses now supports fields that are keyword-only in the generated __init__ method. There are a number of ways of specifying keyword-only fields.
You can say that every field is keyword-only:
from dataclasses import dataclass
@dataclass(kw_only=True)
class Birthday:
name: str
birthday: datetime.date
Both name and birthday are keyword-only parameters to the generated __init__ method.
You can specify keyword-only on a per-field basis:
from dataclasses import dataclass, field
@dataclass
class Birthday:
name: str
birthday: datetime.date = field(kw_only=True)
Here only birthday is keyword-only. If you set kw_only on individual fields, be aware that there are rules about re-ordering fields due to keyword-only fields needing to follow non-keyword-only fields. See the full dataclasses documentation for details.
You can also specify that all fields following a KW_ONLY marker are keyword-only. This will probably be the most common usage:
from dataclasses import dataclass, KW_ONLY
@dataclass
class Point:
x: float
y: float
_: KW_ONLY
z: float = 0.0
t: float = 0.0
Here, z and t are keyword-only parameters, while x and y are not. (Contributed by Eric V. Smith in 43532{.interpreted-text role="issue"}.)
distutils {#distutils-deprecated}
The entire distutils package is deprecated, to be removed in Python 3.12. Its functionality for specifying package builds has already been completely replaced by third-party packages setuptools and packaging, and most other commonly used APIs are available elsewhere in the standard library (such as platform{.interpreted-text role="mod"}, shutil{.interpreted-text role="mod"}, subprocess{.interpreted-text role="mod"} or sysconfig{.interpreted-text role="mod"}). There are no plans to migrate any other functionality from distutils, and applications that are using other functions should plan to make private copies of the code. Refer to 632{.interpreted-text role="pep"} for discussion.
The bdist_wininst command deprecated in Python 3.8 has been removed. The bdist_wheel command is now recommended to distribute binary packages on Windows. (Contributed by Victor Stinner in 42802{.interpreted-text role="issue"}.)
doctest
When a module does not define __loader__, fall back to __spec__.loader. (Contributed by Brett Cannon in 42133{.interpreted-text role="issue"}.)
encodings
encodings.normalize_encoding{.interpreted-text role="func"} now ignores non-ASCII characters. (Contributed by Hai Shi in 39337{.interpreted-text role="issue"}.)
enum
~enum.Enum{.interpreted-text role="class"} ~object.__repr__{.interpreted-text role="func"} now returns enum_name.member_name and ~object.__str__{.interpreted-text role="func"} now returns member_name. Stdlib enums available as module constants have a repr{.interpreted-text role="func"} of module_name.member_name. (Contributed by Ethan Furman in 40066{.interpreted-text role="issue"}.)
Add enum.StrEnum{.interpreted-text role="class"} for enums where all members are strings. (Contributed by Ethan Furman in 41816{.interpreted-text role="issue"}.)
fileinput
Add encoding and errors parameters in fileinput.input{.interpreted-text role="func"} and fileinput.FileInput{.interpreted-text role="class"}. (Contributed by Inada Naoki in 43712{.interpreted-text role="issue"}.)
fileinput.hook_compressed{.interpreted-text role="func"} now returns ~io.TextIOWrapper{.interpreted-text role="class"} object when mode is "r" and file is compressed, like uncompressed files. (Contributed by Inada Naoki in 5758{.interpreted-text role="issue"}.)
faulthandler
The faulthandler{.interpreted-text role="mod"} module now detects if a fatal error occurs during a garbage collector collection. (Contributed by Victor Stinner in 44466{.interpreted-text role="issue"}.)
gc
Add audit hooks for gc.get_objects{.interpreted-text role="func"}, gc.get_referrers{.interpreted-text role="func"} and gc.get_referents{.interpreted-text role="func"}. (Contributed by Pablo Galindo in 43439{.interpreted-text role="issue"}.)
glob
Add the root_dir and dir_fd parameters in ~glob.glob{.interpreted-text role="func"} and ~glob.iglob{.interpreted-text role="func"} which allow to specify the root directory for searching. (Contributed by Serhiy Storchaka in 38144{.interpreted-text role="issue"}.)
hashlib
The hashlib module requires OpenSSL 1.1.1 or newer. (Contributed by Christian Heimes in 644{.interpreted-text role="pep"} and 43669{.interpreted-text role="issue"}.)
The hashlib module has preliminary support for OpenSSL 3.0.0. (Contributed by Christian Heimes in 38820{.interpreted-text role="issue"} and other issues.)
The pure-Python fallback of ~hashlib.pbkdf2_hmac{.interpreted-text role="func"} is deprecated. In the future PBKDF2-HMAC will only be available when Python has been built with OpenSSL support. (Contributed by Christian Heimes in 43880{.interpreted-text role="issue"}.)
hmac
The hmac module now uses OpenSSL's HMAC implementation internally. (Contributed by Christian Heimes in 40645{.interpreted-text role="issue"}.)
IDLE and idlelib
Make IDLE invoke sys.excepthook{.interpreted-text role="func"} (when started without '-n'). User hooks were previously ignored. (Contributed by Ken Hilton in 43008{.interpreted-text role="issue"}.)
Rearrange the settings dialog. Split the General tab into Windows and Shell/Ed tabs. Move help sources, which extend the Help menu, to the Extensions tab. Make space for new options and shorten the dialog. The latter makes the dialog better fit small screens. (Contributed by Terry Jan Reedy in 40468{.interpreted-text role="issue"}.) Move the indent space setting from the Font tab to the new Windows tab. (Contributed by Mark Roseman and Terry Jan Reedy in 33962{.interpreted-text role="issue"}.)
The changes above were backported to a 3.9 maintenance release.
Add a Shell sidebar. Move the primary prompt ('>>>') to the sidebar. Add secondary prompts ('...') to the sidebar. Left click and optional drag selects one or more lines of text, as with the editor line number sidebar. Right click after selecting text lines displays a context menu with 'copy with prompts'. This zips together prompts from the sidebar with lines from the selected text. This option also appears on the context menu for the text. (Contributed by Tal Einat in 37903{.interpreted-text role="issue"}.)
Use spaces instead of tabs to indent interactive code. This makes interactive code entries 'look right'. Making this feasible was a major motivation for adding the shell sidebar. (Contributed by Terry Jan Reedy in 37892{.interpreted-text role="issue"}.)
Highlight the new soft keywords <soft-keywords>{.interpreted-text role="ref"} match{.interpreted-text role="keyword"}, case <match>{.interpreted-text role="keyword"}, and _ <wildcard-patterns>{.interpreted-text role="keyword"} in pattern-matching statements. However, this highlighting is not perfect and will be incorrect in some rare cases, including some _-s in case patterns. (Contributed by Tal Einat in 44010{.interpreted-text role="issue"}.)
New in 3.10 maintenance releases.
Apply syntax highlighting to .pyi files. (Contributed by Alex Waygood and Terry Jan Reedy in 45447{.interpreted-text role="issue"}.)
Include prompts when saving Shell with inputs and outputs. (Contributed by Terry Jan Reedy in 95191{.interpreted-text role="gh"}.)
importlib.metadata
Feature parity with importlib_metadata 4.6 (history).
importlib.metadata entry points <entry-points>{.interpreted-text role="ref"} now provide a nicer experience for selecting entry points by group and name through a new importlib.metadata.EntryPoints <entry-points>{.interpreted-text role="ref"} class. See the Compatibility Note in the docs for more info on the deprecation and usage.
Added importlib.metadata.packages_distributions() <package-distributions>{.interpreted-text role="ref"} for resolving top-level Python modules and packages to their importlib.metadata.Distribution <distributions>{.interpreted-text role="ref"}.
inspect
When a module does not define __loader__, fall back to __spec__.loader. (Contributed by Brett Cannon in 42133{.interpreted-text role="issue"}.)
Add inspect.get_annotations{.interpreted-text role="func"}, which safely computes the annotations defined on an object. It works around the quirks of accessing the annotations on various types of objects, and makes very few assumptions about the object it examines. inspect.get_annotations{.interpreted-text role="func"} can also correctly un-stringize stringized annotations. inspect.get_annotations{.interpreted-text role="func"} is now considered best practice for accessing the annotations dict defined on any Python object; for more information on best practices for working with annotations, please see annotations-howto{.interpreted-text role="ref"}. Relatedly, inspect.signature{.interpreted-text role="func"}, inspect.Signature.from_callable{.interpreted-text role="func"}, and !inspect.Signature.from_function{.interpreted-text role="func"} now call inspect.get_annotations{.interpreted-text role="func"} to retrieve annotations. This means inspect.signature{.interpreted-text role="func"} and inspect.Signature.from_callable{.interpreted-text role="func"} can also now un-stringize stringized annotations. (Contributed by Larry Hastings in 43817{.interpreted-text role="issue"}.)
itertools
Add itertools.pairwise{.interpreted-text role="func"}. (Contributed by Raymond Hettinger in 38200{.interpreted-text role="issue"}.)
linecache
When a module does not define __loader__, fall back to __spec__.loader. (Contributed by Brett Cannon in 42133{.interpreted-text role="issue"}.)
os
Add os.cpu_count{.interpreted-text role="func"} support for VxWorks RTOS. (Contributed by Peixing Xin in 41440{.interpreted-text role="issue"}.)
Add a new function os.eventfd{.interpreted-text role="func"} and related helpers to wrap the eventfd2 syscall on Linux. (Contributed by Christian Heimes in 41001{.interpreted-text role="issue"}.)
Add os.splice{.interpreted-text role="func"} that allows to move data between two file descriptors without copying between kernel address space and user address space, where one of the file descriptors must refer to a pipe. (Contributed by Pablo Galindo in 41625{.interpreted-text role="issue"}.)
Add ~os.O_EVTONLY{.interpreted-text role="const"}, ~os.O_FSYNC{.interpreted-text role="const"}, ~os.O_SYMLINK{.interpreted-text role="const"} and ~os.O_NOFOLLOW_ANY{.interpreted-text role="const"} for macOS. (Contributed by Donghee Na in 43106{.interpreted-text role="issue"}.)
os.path
os.path.realpath{.interpreted-text role="func"} now accepts a strict keyword-only argument. When set to True, OSError{.interpreted-text role="exc"} is raised if a path doesn't exist or a symlink loop is encountered. (Contributed by Barney Gale in 43757{.interpreted-text role="issue"}.)
pathlib
Add slice support to PurePath.parents <pathlib.PurePath.parents>{.interpreted-text role="attr"}. (Contributed by Joshua Cannon in 35498{.interpreted-text role="issue"}.)
Add negative indexing support to PurePath.parents <pathlib.PurePath.parents>{.interpreted-text role="attr"}. (Contributed by Yaroslav Pankovych in 21041{.interpreted-text role="issue"}.)
Add Path.hardlink_to <pathlib.Path.hardlink_to>{.interpreted-text role="meth"} method that supersedes !link_to{.interpreted-text role="meth"}. The new method has the same argument order as ~pathlib.Path.symlink_to{.interpreted-text role="meth"}. (Contributed by Barney Gale in 39950{.interpreted-text role="issue"}.)
pathlib.Path.stat{.interpreted-text role="meth"} and ~pathlib.Path.chmod{.interpreted-text role="meth"} now accept a follow_symlinks keyword-only argument for consistency with corresponding functions in the os{.interpreted-text role="mod"} module. (Contributed by Barney Gale in 39906{.interpreted-text role="issue"}.)
platform
Add platform.freedesktop_os_release{.interpreted-text role="func"} to retrieve operation system identification from freedesktop.org os-release standard file. (Contributed by Christian Heimes in 28468{.interpreted-text role="issue"}.)
pprint
pprint.pprint{.interpreted-text role="func"} now accepts a new underscore_numbers keyword argument. (Contributed by sblondon in 42914{.interpreted-text role="issue"}.)
pprint{.interpreted-text role="mod"} can now pretty-print dataclasses.dataclass{.interpreted-text role="class"} instances. (Contributed by Lewis Gaul in 43080{.interpreted-text role="issue"}.)
py_compile
Add --quiet option to command-line interface of py_compile{.interpreted-text role="mod"}. (Contributed by Gregory Schevchenko in 38731{.interpreted-text role="issue"}.)
pyclbr
Add an end_lineno attribute to the Function and Class objects in the tree returned by pyclbr.readmodule{.interpreted-text role="func"} and pyclbr.readmodule_ex{.interpreted-text role="func"}. It matches the existing (start) lineno. (Contributed by Aviral Srivastava in 38307{.interpreted-text role="issue"}.)
shelve
The shelve{.interpreted-text role="mod"} module now uses pickle.DEFAULT_PROTOCOL{.interpreted-text role="const"} by default instead of pickle{.interpreted-text role="mod"} protocol 3 when creating shelves. (Contributed by Zackery Spytz in 34204{.interpreted-text role="issue"}.)
statistics
Add ~statistics.covariance{.interpreted-text role="func"}, Pearson's ~statistics.correlation{.interpreted-text role="func"}, and simple ~statistics.linear_regression{.interpreted-text role="func"} functions. (Contributed by Tymoteusz Wołodźko in 38490{.interpreted-text role="issue"}.)
site
When a module does not define __loader__, fall back to __spec__.loader. (Contributed by Brett Cannon in 42133{.interpreted-text role="issue"}.)
socket
The exception socket.timeout{.interpreted-text role="exc"} is now an alias of TimeoutError{.interpreted-text role="exc"}. (Contributed by Christian Heimes in 42413{.interpreted-text role="issue"}.)
Add option to create MPTCP sockets with IPPROTO_MPTCP (Contributed by Rui Cunha in 43571{.interpreted-text role="issue"}.)
Add IP_RECVTOS option to receive the type of service (ToS) or DSCP/ECN fields (Contributed by Georg Sauthoff in 44077{.interpreted-text role="issue"}.)
ssl
The ssl module requires OpenSSL 1.1.1 or newer. (Contributed by Christian Heimes in 644{.interpreted-text role="pep"} and 43669{.interpreted-text role="issue"}.)
The ssl module has preliminary support for OpenSSL 3.0.0 and new option ~ssl.OP_IGNORE_UNEXPECTED_EOF{.interpreted-text role="const"}. (Contributed by Christian Heimes in 38820{.interpreted-text role="issue"}, 43794{.interpreted-text role="issue"}, 43788{.interpreted-text role="issue"}, 43791{.interpreted-text role="issue"}, 43799{.interpreted-text role="issue"}, 43920{.interpreted-text role="issue"}, 43789{.interpreted-text role="issue"}, and 43811{.interpreted-text role="issue"}.)
Deprecated function and use of deprecated constants now result in a DeprecationWarning{.interpreted-text role="exc"}. ssl.SSLContext.options{.interpreted-text role="attr"} has ~ssl.OP_NO_SSLv2{.interpreted-text role="data"} and ~ssl.OP_NO_SSLv3{.interpreted-text role="data"} set by default and therefore cannot warn about setting the flag again. The deprecation section <whatsnew310-deprecated>{.interpreted-text role="ref"} has a list of deprecated features. (Contributed by Christian Heimes in 43880{.interpreted-text role="issue"}.)
The ssl module now has more secure default settings. Ciphers without forward secrecy or SHA-1 MAC are disabled by default. Security level 2 prohibits weak RSA, DH, and ECC keys with less than 112 bits of security. ~ssl.SSLContext{.interpreted-text role="class"} defaults to minimum protocol version TLS 1.2. Settings are based on Hynek Schlawack's research. (Contributed by Christian Heimes in 43998{.interpreted-text role="issue"}.)
The deprecated protocols SSL 3.0, TLS 1.0, and TLS 1.1 are no longer officially supported. Python does not block them actively. However OpenSSL build options, distro configurations, vendor patches, and cipher suites may prevent a successful handshake.
Add a timeout parameter to the ssl.get_server_certificate{.interpreted-text role="func"} function. (Contributed by Zackery Spytz in 31870{.interpreted-text role="issue"}.)
The ssl module uses heap-types and multi-phase initialization. (Contributed by Christian Heimes in 42333{.interpreted-text role="issue"}.)
A new verify flag ~ssl.VERIFY_X509_PARTIAL_CHAIN{.interpreted-text role="const"} has been added. (Contributed by l0x in 40849{.interpreted-text role="issue"}.)
sqlite3
Add audit events for ~sqlite3.connect{.interpreted-text role="func"}, ~sqlite3.Connection.enable_load_extension{.interpreted-text role="meth"}, and ~sqlite3.Connection.load_extension{.interpreted-text role="meth"}. (Contributed by Erlend E. Aasland in 43762{.interpreted-text role="issue"}.)
sys
Add sys.orig_argv{.interpreted-text role="data"} attribute: the list of the original command line arguments passed to the Python executable. (Contributed by Victor Stinner in 23427{.interpreted-text role="issue"}.)
Add sys.stdlib_module_names{.interpreted-text role="data"}, containing the list of the standard library module names. (Contributed by Victor Stinner in 42955{.interpreted-text role="issue"}.)
[thread]{#thread}
_thread.interrupt_main{.interpreted-text role="func"} now takes an optional signal number to simulate (the default is still signal.SIGINT{.interpreted-text role="const"}). (Contributed by Antoine Pitrou in 43356{.interpreted-text role="issue"}.)
threading
Add threading.gettrace{.interpreted-text role="func"} and threading.getprofile{.interpreted-text role="func"} to retrieve the functions set by threading.settrace{.interpreted-text role="func"} and threading.setprofile{.interpreted-text role="func"} respectively. (Contributed by Mario Corchero in 42251{.interpreted-text role="issue"}.)
Add threading.__excepthook__{.interpreted-text role="data"} to allow retrieving the original value of threading.excepthook{.interpreted-text role="func"} in case it is set to a broken or a different value. (Contributed by Mario Corchero in 42308{.interpreted-text role="issue"}.)
traceback
The ~traceback.format_exception{.interpreted-text role="func"}, ~traceback.format_exception_only{.interpreted-text role="func"}, and ~traceback.print_exception{.interpreted-text role="func"} functions can now take an exception object as a positional-only argument. (Contributed by Zackery Spytz and Matthias Bussonnier in 26389{.interpreted-text role="issue"}.)
types
Reintroduce the types.EllipsisType{.interpreted-text role="data"}, types.NoneType{.interpreted-text role="data"} and types.NotImplementedType{.interpreted-text role="data"} classes, providing a new set of types readily interpretable by type checkers. (Contributed by Bas van Beek in 41810{.interpreted-text role="issue"}.)
typing
For major changes, see new-feat-related-type-hints{.interpreted-text role="ref"}.
The behavior of typing.Literal{.interpreted-text role="class"} was changed to conform with 586{.interpreted-text role="pep"} and to match the behavior of static type checkers specified in the PEP.
Literalnow de-duplicates parameters.Equality comparisons between
Literalobjects are now order independent.Literalcomparisons now respect types. For example,Literal[0] == Literal[False]previously evaluated toTrue. It is nowFalse. To support this change, the internally used type cache now supports differentiating types.Literalobjects will now raise aTypeError{.interpreted-text role="exc"} exception during equality comparisons if any of their parameters are nothashable{.interpreted-text role="term"}. Note that declaringLiteralwith unhashable parameters will not throw an error:>>> from typing import Literal >>> Literal[{0}] >>> Literal[{0}] == Literal[{False}] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unhashable type: 'set'
(Contributed by Yurii Karabas in 42345{.interpreted-text role="issue"}.)
Add new function typing.is_typeddict{.interpreted-text role="func"} to introspect if an annotation is a typing.TypedDict{.interpreted-text role="class"}. (Contributed by Patrick Reader in 41792{.interpreted-text role="issue"}.)
Subclasses of typing.Protocol which only have data variables declared will now raise a TypeError when checked with isinstance unless they are decorated with ~typing.runtime_checkable{.interpreted-text role="func"}. Previously, these checks passed silently. Users should decorate their subclasses with the !runtime_checkable{.interpreted-text role="func"} decorator if they want runtime protocols. (Contributed by Yurii Karabas in 38908{.interpreted-text role="issue"}.)
Importing from the typing.io and typing.re submodules will now emit DeprecationWarning{.interpreted-text role="exc"}. These submodules have been deprecated since Python 3.8 and will be removed in a future version of Python. Anything belonging to those submodules should be imported directly from typing{.interpreted-text role="mod"} instead. (Contributed by Sebastian Rittau in 38291{.interpreted-text role="issue"}.)
unittest
Add new method ~unittest.TestCase.assertNoLogs{.interpreted-text role="meth"} to complement the existing ~unittest.TestCase.assertLogs{.interpreted-text role="meth"}. (Contributed by Kit Yan Choi in 39385{.interpreted-text role="issue"}.)
urllib.parse
Python versions earlier than Python 3.10 allowed using both ; and & as query parameter separators in urllib.parse.parse_qs{.interpreted-text role="func"} and urllib.parse.parse_qsl{.interpreted-text role="func"}. Due to security concerns, and to conform with newer W3C recommendations, this has been changed to allow only a single separator key, with & as the default. This change also affects !cgi.parse{.interpreted-text role="func"} and !cgi.parse_multipart{.interpreted-text role="func"} as they use the affected functions internally. For more details, please see their respective documentation. (Contributed by Adam Goldschmidt, Senthil Kumaran and Ken Jin in 42967{.interpreted-text role="issue"}.)
The presence of newline or tab characters in parts of a URL allows for some forms of attacks. Following the WHATWG specification that updates 3986{.interpreted-text role="rfc"}, ASCII newline \n, \r and tab \t characters are stripped from the URL by the parser in urllib.parse{.interpreted-text role="mod"} preventing such attacks. The removal characters are controlled by a new module level variable urllib.parse._UNSAFE_URL_BYTES_TO_REMOVE. (See 88048{.interpreted-text role="gh"})
xml
Add a ~xml.sax.handler.LexicalHandler{.interpreted-text role="class"} class to the xml.sax.handler{.interpreted-text role="mod"} module. (Contributed by Jonathan Gossage and Zackery Spytz in 35018{.interpreted-text role="issue"}.)
zipimport
Add methods related to 451{.interpreted-text role="pep"}: ~zipimport.zipimporter.find_spec{.interpreted-text role="meth"}, zipimport.zipimporter.create_module{.interpreted-text role="meth"}, and zipimport.zipimporter.exec_module{.interpreted-text role="meth"}. (Contributed by Brett Cannon in 42131{.interpreted-text role="issue"}.)
Add ~zipimport.zipimporter.invalidate_caches{.interpreted-text role="meth"} method. (Contributed by Desmond Cheong in 14678{.interpreted-text role="issue"}.)
Optimizations
- Constructors
str{.interpreted-text role="func"},bytes{.interpreted-text role="func"} andbytearray{.interpreted-text role="func"} are now faster (around 30--40% for small objects). (Contributed by Serhiy Storchaka in41334{.interpreted-text role="issue"}.) - The
runpy{.interpreted-text role="mod"} module now imports fewer modules. Thepython3 -m module-namecommand startup time is 1.4x faster in average. On Linux,python3 -I -m module-nameimports 69 modules on Python 3.9, whereas it only imports 51 modules (-18) on Python 3.10. (Contributed by Victor Stinner in41006{.interpreted-text role="issue"} and41718{.interpreted-text role="issue"}.) - The
LOAD_ATTRinstruction now uses new "per opcode cache" mechanism. It is about 36% faster now for regular attributes and 44% faster for slots. (Contributed by Pablo Galindo and Yury Selivanov in42093{.interpreted-text role="issue"} and Guido van Rossum in42927{.interpreted-text role="issue"}, based on ideas implemented originally in PyPy and MicroPython.) - When building Python with
--enable-optimizations{.interpreted-text role="option"} now-fno-semantic-interpositionis added to both the compile and link line. This speeds builds of the Python interpreter created with--enable-shared{.interpreted-text role="option"} withgccby up to 30%. See this article for more details. (Contributed by Victor Stinner and Pablo Galindo in38980{.interpreted-text role="issue"}.) - Use a new output buffer management code for
bz2{.interpreted-text role="mod"} /lzma{.interpreted-text role="mod"} /zlib{.interpreted-text role="mod"} modules, and add.readall()function to_compression.DecompressReaderclass. bz2 decompression is now 1.09x ~ 1.17x faster, lzma decompression 1.20x ~ 1.32x faster,GzipFile.read(-1)1.11x ~ 1.18x faster. (Contributed by Ma Lin, reviewed by Gregory P. Smith, in41486{.interpreted-text role="issue"}) - When using stringized annotations, annotations dicts for functions are no longer created when the function is created. Instead, they are stored as a tuple of strings, and the function object lazily converts this into the annotations dict on demand. This optimization cuts the CPU time needed to define an annotated function by half. (Contributed by Yurii Karabas and Inada Naoki in
42202{.interpreted-text role="issue"}.) - Substring search functions such as
str1 in str2andstr2.find(str1)now sometimes use Crochemore & Perrin's "Two-Way" string searching algorithm to avoid quadratic behavior on long strings. (Contributed by Dennis Sweeney in41972{.interpreted-text role="issue"}) - Add micro-optimizations to
_PyType_Lookup()to improve type attribute cache lookup performance in the common case of cache hits. This makes the interpreter 1.04 times faster on average. (Contributed by Dino Viehland in43452{.interpreted-text role="issue"}.) - The following built-in functions now support the faster
590{.interpreted-text role="pep"} vectorcall calling convention:map{.interpreted-text role="func"},filter{.interpreted-text role="func"},reversed{.interpreted-text role="func"},bool{.interpreted-text role="func"} andfloat{.interpreted-text role="func"}. (Contributed by Donghee Na and Jeroen Demeyer in43575{.interpreted-text role="issue"},43287{.interpreted-text role="issue"},41922{.interpreted-text role="issue"},41873{.interpreted-text role="issue"} and41870{.interpreted-text role="issue"}.) ~bz2.BZ2File{.interpreted-text role="class"} performance is improved by removing internalRLock. This makes!BZ2File{.interpreted-text role="class"} thread unsafe in the face of multiple simultaneous readers or writers, just like its equivalent classes ingzip{.interpreted-text role="mod"} andlzma{.interpreted-text role="mod"} have always been. (Contributed by Inada Naoki in43785{.interpreted-text role="issue"}.)
Deprecated {#whatsnew310-deprecated}
Currently Python accepts numeric literals immediately followed by keywords, for example
0in x,1or x,0if 1else 2. It allows confusing and ambiguous expressions like[0x1for x in y](which can be interpreted as[0x1 for x in y]or[0x1f or x in y]). Starting in this release, a deprecation warning is raised if the numeric literal is immediately followed by one of keywordsand{.interpreted-text role="keyword"},else{.interpreted-text role="keyword"},for{.interpreted-text role="keyword"},if{.interpreted-text role="keyword"},in{.interpreted-text role="keyword"},is{.interpreted-text role="keyword"} andor{.interpreted-text role="keyword"}. In future releases it will be changed to syntax warning, and finally to syntax error. (Contributed by Serhiy Storchaka in43833{.interpreted-text role="issue"}.)Starting in this release, there will be a concerted effort to begin cleaning up old import semantics that were kept for Python 2.7 compatibility. Specifically,
!find_loader{.interpreted-text role="meth"}/!find_module{.interpreted-text role="meth"} (superseded by~importlib.abc.MetaPathFinder.find_spec{.interpreted-text role="meth"}),importlib.abc.Loader.load_module(superseded by~importlib.abc.Loader.exec_module{.interpreted-text role="meth"}),!module_repr{.interpreted-text role="meth"} (which the import system takes care of for you), the__package__attribute (superseded by__spec__.parent), the__loader__attribute (superseded by__spec__.loader), and the__cached__attribute (superseded by__spec__.cached) will slowly be removed (as well as other classes and methods inimportlib{.interpreted-text role="mod"}).ImportWarning{.interpreted-text role="exc"} and/orDeprecationWarning{.interpreted-text role="exc"} will be raised as appropriate to help identify code which needs updating during this transition.The entire
distutilsnamespace is deprecated, to be removed in Python 3.12. Refer to themodule changes <distutils-deprecated>{.interpreted-text role="ref"} section for more information.Non-integer arguments to
random.randrange{.interpreted-text role="func"} are deprecated. TheValueError{.interpreted-text role="exc"} is deprecated in favor of aTypeError{.interpreted-text role="exc"}. (Contributed by Serhiy Storchaka and Raymond Hettinger in37319{.interpreted-text role="issue"}.)The various
load_module()methods ofimportlib{.interpreted-text role="mod"} have been documented as deprecated since Python 3.6, but will now also trigger aDeprecationWarning{.interpreted-text role="exc"}. Use~importlib.abc.Loader.exec_module{.interpreted-text role="meth"} instead. (Contributed by Brett Cannon in26131{.interpreted-text role="issue"}.)!zimport.zipimporter.load_module{.interpreted-text role="meth"} has been deprecated in preference for~zipimport.zipimporter.exec_module{.interpreted-text role="meth"}. (Contributed by Brett Cannon in26131{.interpreted-text role="issue"}.)The use of
importlib.abc.Loader.load_moduleby the import system now triggers anImportWarning{.interpreted-text role="exc"} as~importlib.abc.Loader.exec_module{.interpreted-text role="meth"} is preferred. (Contributed by Brett Cannon in26131{.interpreted-text role="issue"}.)The use of
!importlib.abc.MetaPathFinder.find_module{.interpreted-text role="meth"} and!importlib.abc.PathEntryFinder.find_module{.interpreted-text role="meth"} by the import system now trigger anImportWarning{.interpreted-text role="exc"} asimportlib.abc.MetaPathFinder.find_spec{.interpreted-text role="meth"} andimportlib.abc.PathEntryFinder.find_spec{.interpreted-text role="meth"} are preferred, respectively. You can useimportlib.util.spec_from_loader{.interpreted-text role="func"} to help in porting. (Contributed by Brett Cannon in42134{.interpreted-text role="issue"}.)The use of
!importlib.abc.PathEntryFinder.find_loader{.interpreted-text role="meth"} by the import system now triggers anImportWarning{.interpreted-text role="exc"} asimportlib.abc.PathEntryFinder.find_spec{.interpreted-text role="meth"} is preferred. You can useimportlib.util.spec_from_loader{.interpreted-text role="func"} to help in porting. (Contributed by Brett Cannon in43672{.interpreted-text role="issue"}.)The various implementations of
!importlib.abc.MetaPathFinder.find_module{.interpreted-text role="meth"} (!importlib.machinery.BuiltinImporter.find_module{.interpreted-text role="meth"},!importlib.machinery.FrozenImporter.find_module{.interpreted-text role="meth"},!importlib.machinery.WindowsRegistryFinder.find_module{.interpreted-text role="meth"},!importlib.machinery.PathFinder.find_module{.interpreted-text role="meth"},!importlib.abc.MetaPathFinder.find_module{.interpreted-text role="meth"} ),!importlib.abc.PathEntryFinder.find_module{.interpreted-text role="meth"} (!importlib.machinery.FileFinder.find_module{.interpreted-text role="meth"} ), and!importlib.abc.PathEntryFinder.find_loader{.interpreted-text role="meth"} (!importlib.machinery.FileFinder.find_loader{.interpreted-text role="meth"} ) now raiseDeprecationWarning{.interpreted-text role="exc"} and are slated for removal in Python 3.12 (previously they were documented as deprecated in Python 3.4). (Contributed by Brett Cannon in42135{.interpreted-text role="issue"}.)!importlib.abc.Finder{.interpreted-text role="class"} is deprecated (including its sole method,!find_module{.interpreted-text role="meth"}). Bothimportlib.abc.MetaPathFinder{.interpreted-text role="class"} andimportlib.abc.PathEntryFinder{.interpreted-text role="class"} no longer inherit from the class. Users should inherit from one of these two classes as appropriate instead. (Contributed by Brett Cannon in42135{.interpreted-text role="issue"}.)The deprecations of
!imp{.interpreted-text role="mod"},!importlib.find_loader{.interpreted-text role="func"},!importlib.util.set_package_wrapper{.interpreted-text role="func"},!importlib.util.set_loader_wrapper{.interpreted-text role="func"},!importlib.util.module_for_loader{.interpreted-text role="func"},!pkgutil.ImpImporter{.interpreted-text role="class"}, and!pkgutil.ImpLoader{.interpreted-text role="class"} have all been updated to list Python 3.12 as the slated version of removal (they began raisingDeprecationWarning{.interpreted-text role="exc"} in previous versions of Python). (Contributed by Brett Cannon in43720{.interpreted-text role="issue"}.)The import system now uses the
__spec__attribute on modules before falling back on!module_repr{.interpreted-text role="meth"} for a module's__repr__()method. Removal of the use ofmodule_repr()is scheduled for Python 3.12. (Contributed by Brett Cannon in42137{.interpreted-text role="issue"}.)!importlib.abc.Loader.module_repr{.interpreted-text role="meth"},!importlib.machinery.FrozenLoader.module_repr{.interpreted-text role="meth"}, and!importlib.machinery.BuiltinLoader.module_repr{.interpreted-text role="meth"} are deprecated and slated for removal in Python 3.12. (Contributed by Brett Cannon in42136{.interpreted-text role="issue"}.)sqlite3.OptimizedUnicodehas been undocumented and obsolete since Python 3.3, when it was made an alias tostr{.interpreted-text role="class"}. It is now deprecated, scheduled for removal in Python 3.12. (Contributed by Erlend E. Aasland in42264{.interpreted-text role="issue"}.)The undocumented built-in function
sqlite3.enable_shared_cacheis now deprecated, scheduled for removal in Python 3.12. Its use is strongly discouraged by the SQLite3 documentation. See the SQLite3 docs for more details. If a shared cache must be used, open the database in URI mode using thecache=sharedquery parameter. (Contributed by Erlend E. Aasland in24464{.interpreted-text role="issue"}.)The following
threadingmethods are now deprecated:threading.currentThread=>threading.current_thread{.interpreted-text role="func"}threading.activeCount=>threading.active_count{.interpreted-text role="func"}threading.Condition.notifyAll=>threading.Condition.notify_all{.interpreted-text role="meth"}threading.Event.isSet=>threading.Event.is_set{.interpreted-text role="meth"}threading.Thread.setName=>threading.Thread.name{.interpreted-text role="attr"}threading.thread.getName=>threading.Thread.name{.interpreted-text role="attr"}threading.Thread.isDaemon=>threading.Thread.daemon{.interpreted-text role="attr"}threading.Thread.setDaemon=>threading.Thread.daemon{.interpreted-text role="attr"}
(Contributed by Jelle Zijlstra in
87889{.interpreted-text role="gh"}.)!pathlib.Path.link_to{.interpreted-text role="meth"} is deprecated and slated for removal in Python 3.12. Usepathlib.Path.hardlink_to{.interpreted-text role="meth"} instead. (Contributed by Barney Gale in39950{.interpreted-text role="issue"}.)cgi.log()is deprecated and slated for removal in Python 3.12. (Contributed by Inada Naoki in41139{.interpreted-text role="issue"}.)The following
ssl{.interpreted-text role="mod"} features have been deprecated since Python 3.6, Python 3.7, or OpenSSL 1.1.0 and will be removed in 3.11:!OP_NO_SSLv2{.interpreted-text role="data"},!OP_NO_SSLv3{.interpreted-text role="data"},!OP_NO_TLSv1{.interpreted-text role="data"},!OP_NO_TLSv1_1{.interpreted-text role="data"},!OP_NO_TLSv1_2{.interpreted-text role="data"}, and!OP_NO_TLSv1_3{.interpreted-text role="data"} are replaced by~ssl.SSLContext.minimum_version{.interpreted-text role="attr"} and~ssl.SSLContext.maximum_version{.interpreted-text role="attr"}.!PROTOCOL_SSLv2{.interpreted-text role="data"},!PROTOCOL_SSLv3{.interpreted-text role="data"},!PROTOCOL_SSLv23{.interpreted-text role="data"},!PROTOCOL_TLSv1{.interpreted-text role="data"},!PROTOCOL_TLSv1_1{.interpreted-text role="data"},!PROTOCOL_TLSv1_2{.interpreted-text role="data"}, and!PROTOCOL_TLS{.interpreted-text role="const"} are deprecated in favor of~ssl.PROTOCOL_TLS_CLIENT{.interpreted-text role="const"} and~ssl.PROTOCOL_TLS_SERVER{.interpreted-text role="const"}!wrap_socket{.interpreted-text role="func"} is replaced byssl.SSLContext.wrap_socket{.interpreted-text role="meth"}!match_hostname{.interpreted-text role="func"}!RAND_pseudo_bytes{.interpreted-text role="func"},!RAND_egd{.interpreted-text role="func"}- NPN features like
ssl.SSLSocket.selected_npn_protocol{.interpreted-text role="meth"} andssl.SSLContext.set_npn_protocols{.interpreted-text role="meth"} are replaced by ALPN.
The threading debug (
!PYTHONTHREADDEBUG{.interpreted-text role="envvar"} environment variable) is deprecated in Python 3.10 and will be removed in Python 3.12. This feature requires adebug build of Python <debug-build>{.interpreted-text role="ref"}. (Contributed by Victor Stinner in44584{.interpreted-text role="issue"}.)Importing from the
typing.ioandtyping.resubmodules will now emitDeprecationWarning{.interpreted-text role="exc"}. These submodules will be removed in a future version of Python. Anything belonging to these submodules should be imported directly fromtyping{.interpreted-text role="mod"} instead. (Contributed by Sebastian Rittau in38291{.interpreted-text role="issue"}.)
Removed {#whatsnew310-removed}
Removed special methods
__int__,__float__,__floordiv__,__mod__,__divmod__,__rfloordiv__,__rmod__and__rdivmod__of thecomplex{.interpreted-text role="class"} class. They always raised aTypeError{.interpreted-text role="exc"}. (Contributed by Serhiy Storchaka in41974{.interpreted-text role="issue"}.)The
ParserBase.error()method from the private and undocumented_markupbasemodule has been removed.html.parser.HTMLParser{.interpreted-text role="class"} is the only subclass ofParserBaseand itserror()implementation was already removed in Python 3.5. (Contributed by Berker Peksag in31844{.interpreted-text role="issue"}.)Removed the
unicodedata.ucnhash_CAPIattribute which was an internal PyCapsule object. The related private_PyUnicode_Name_CAPIstructure was moved to the internal C API. (Contributed by Victor Stinner in42157{.interpreted-text role="issue"}.)Removed the
parsermodule, which was deprecated in 3.9 due to the switch to the new PEG parser, as well as all the C source and header files that were only being used by the old parser, includingnode.h,parser.h,graminit.handgrammar.h.Removed the Public C API functions
PyParser_SimpleParseStringFlags,PyParser_SimpleParseStringFlagsFilename,PyParser_SimpleParseFileFlagsandPyNode_Compilethat were deprecated in 3.9 due to the switch to the new PEG parser.Removed the
formattermodule, which was deprecated in Python 3.4. It is somewhat obsolete, little used, and not tested. It was originally scheduled to be removed in Python 3.6, but such removals were delayed until after Python 2.7 EOL. Existing users should copy whatever classes they use into their code. (Contributed by Donghee Na and Terry J. Reedy in42299{.interpreted-text role="issue"}.)Removed the
!PyModule_GetWarningsModule{.interpreted-text role="c:func"} function that was useless now due to the!_warnings{.interpreted-text role="mod"} module was converted to a builtin module in 2.6. (Contributed by Hai Shi in42599{.interpreted-text role="issue"}.)Remove deprecated aliases to
collections-abstract-base-classes{.interpreted-text role="ref"} from thecollections{.interpreted-text role="mod"} module. (Contributed by Victor Stinner in37324{.interpreted-text role="issue"}.)The
loopparameter has been removed from most ofasyncio{.interpreted-text role="mod"}'shigh-level API <../library/asyncio-api-index>{.interpreted-text role="doc"} following deprecation in Python 3.8. The motivation behind this change is multifold:- This simplifies the high-level API.
- The functions in the high-level API have been implicitly getting the current thread's running event loop since Python 3.7. There isn't a need to pass the event loop to the API in most normal use cases.
- Event loop passing is error-prone especially when dealing with loops running in different threads.
Note that the low-level API will still accept
loop. Seechanges-python-api{.interpreted-text role="ref"} for examples of how to replace existing code.(Contributed by Yurii Karabas, Andrew Svetlov, Yury Selivanov and Kyle Stanley in
42392{.interpreted-text role="issue"}.)
Porting to Python 3.10
This section lists previously described changes and other bugfixes that may require changes to your code.
Changes in the Python syntax
- Deprecation warning is now emitted when compiling previously valid syntax if the numeric literal is immediately followed by a keyword (like in
0in x). In future releases it will be changed to syntax warning, and finally to a syntax error. To get rid of the warning and make the code compatible with future releases just add a space between the numeric literal and the following keyword. (Contributed by Serhiy Storchaka in43833{.interpreted-text role="issue"}.)
Changes in the Python API {#changes-python-api}
The etype parameters of the
~traceback.format_exception{.interpreted-text role="func"},~traceback.format_exception_only{.interpreted-text role="func"}, and~traceback.print_exception{.interpreted-text role="func"} functions in thetraceback{.interpreted-text role="mod"} module have been renamed to exc. (Contributed by Zackery Spytz and Matthias Bussonnier in26389{.interpreted-text role="issue"}.)atexit{.interpreted-text role="mod"}: At Python exit, if a callback registered withatexit.register{.interpreted-text role="func"} fails, its exception is now logged. Previously, only some exceptions were logged, and the last exception was always silently ignored. (Contributed by Victor Stinner in42639{.interpreted-text role="issue"}.)collections.abc.Callable{.interpreted-text role="class"} generic now flattens type parameters, similar to whattyping.Callable{.interpreted-text role="data"} currently does. This means thatcollections.abc.Callable[[int, str], str]will have__args__of(int, str, str); previously this was([int, str], str). Code which accesses the arguments viatyping.get_args{.interpreted-text role="func"} or__args__need to account for this change. Furthermore,TypeError{.interpreted-text role="exc"} may be raised for invalid forms of parameterizingcollections.abc.Callable{.interpreted-text role="class"} which may have passed silently in Python 3.9. (Contributed by Ken Jin in42195{.interpreted-text role="issue"}.)socket.htons{.interpreted-text role="meth"} andsocket.ntohs{.interpreted-text role="meth"} now raiseOverflowError{.interpreted-text role="exc"} instead ofDeprecationWarning{.interpreted-text role="exc"} if the given parameter will not fit in a 16-bit unsigned integer. (Contributed by Erlend E. Aasland in42393{.interpreted-text role="issue"}.)The
loopparameter has been removed from most ofasyncio{.interpreted-text role="mod"}'shigh-level API <../library/asyncio-api-index>{.interpreted-text role="doc"} following deprecation in Python 3.8.A coroutine that currently looks like this:
async def foo(loop): await asyncio.sleep(1, loop=loop)Should be replaced with this:
async def foo(): await asyncio.sleep(1)If
foo()was specifically designed not to run in the current thread's running event loop (e.g. running in another thread's event loop), consider usingasyncio.run_coroutine_threadsafe{.interpreted-text role="func"} instead.(Contributed by Yurii Karabas, Andrew Svetlov, Yury Selivanov and Kyle Stanley in
42392{.interpreted-text role="issue"}.)The
types.FunctionType{.interpreted-text role="data"} constructor now inherits the current builtins if the globals dictionary has no"__builtins__"key, rather than using{"None": None}as builtins: same behavior aseval{.interpreted-text role="func"} andexec{.interpreted-text role="func"} functions. Defining a function withdef function(...): ...in Python is not affected, globals cannot be overridden with this syntax: it also inherits the current builtins. (Contributed by Victor Stinner in42990{.interpreted-text role="issue"}.)
Changes in the C API
The C API functions
PyParser_SimpleParseStringFlags,PyParser_SimpleParseStringFlagsFilename,PyParser_SimpleParseFileFlags,PyNode_Compileand the type used by these functions,struct _node, were removed due to the switch to the new PEG parser.Source should be now be compiled directly to a code object using, for example,
Py_CompileString{.interpreted-text role="c:func"}. The resulting code object can then be evaluated using, for example,PyEval_EvalCode{.interpreted-text role="c:func"}.Specifically:
A call to
PyParser_SimpleParseStringFlagsfollowed byPyNode_Compilecan be replaced by callingPy_CompileString{.interpreted-text role="c:func"}.There is no direct replacement for
PyParser_SimpleParseFileFlags. To compile code from aFILE *argument, you will need to read the file in C and pass the resulting buffer toPy_CompileString{.interpreted-text role="c:func"}.To compile a file given a
char *filename, explicitly open the file, read it and compile the result. One way to do this is using theio{.interpreted-text role="py:mod"} module withPyImport_ImportModule{.interpreted-text role="c:func"},PyObject_CallMethod{.interpreted-text role="c:func"},PyBytes_AsString{.interpreted-text role="c:func"} andPy_CompileString{.interpreted-text role="c:func"}, as sketched below. (Declarations and error handling are omitted.) :io_module = Import_ImportModule("io"); fileobject = PyObject_CallMethod(io_module, "open", "ss", filename, "rb"); source_bytes_object = PyObject_CallMethod(fileobject, "read", ""); result = PyObject_CallMethod(fileobject, "close", ""); source_buf = PyBytes_AsString(source_bytes_object); code = Py_CompileString(source_buf, filename, Py_file_input);For
FrameObjectobjects, the~frame.f_lasti{.interpreted-text role="attr"} member now represents a wordcode offset instead of a simple offset into the bytecode string. This means that this number needs to be multiplied by 2 to be used with APIs that expect a byte offset instead (likePyCode_Addr2Line{.interpreted-text role="c:func"} for example). Notice as well that the!f_lasti{.interpreted-text role="attr"} member ofFrameObjectobjects is not considered stable: please usePyFrame_GetLineNumber{.interpreted-text role="c:func"} instead.
CPython bytecode changes
- The
MAKE_FUNCTIONinstruction now accepts either a dict or a tuple of strings as the function's annotations. (Contributed by Yurii Karabas and Inada Naoki in42202{.interpreted-text role="issue"}.)
Build Changes
644{.interpreted-text role="pep"}: Python now requires OpenSSL 1.1.1 or newer. OpenSSL 1.0.2 is no longer supported. (Contributed by Christian Heimes in43669{.interpreted-text role="issue"}.)The C99 functions
snprintf{.interpreted-text role="c:func"} andvsnprintf{.interpreted-text role="c:func"} are now required to build Python. (Contributed by Victor Stinner in36020{.interpreted-text role="issue"}.)sqlite3{.interpreted-text role="mod"} requires SQLite 3.7.15 or higher. (Contributed by Sergey Fedoseev and Erlend E. Aasland in40744{.interpreted-text role="issue"} and40810{.interpreted-text role="issue"}.)The
atexit{.interpreted-text role="mod"} module must now always be built as a built-in module. (Contributed by Victor Stinner in42639{.interpreted-text role="issue"}.)Add
--disable-test-modules{.interpreted-text role="option"} option to theconfigurescript: don't build nor install test modules. (Contributed by Xavier de Gaye, Thomas Petazzoni and Peixing Xin in27640{.interpreted-text role="issue"}.)Add
--with-wheel-pkg-dir=PATH option <--with-wheel-pkg-dir>{.interpreted-text role="option"} to the./configurescript. If specified, theensurepip{.interpreted-text role="mod"} module looks forsetuptoolsandpipwheel packages in this directory: if both are present, these wheel packages are used instead of ensurepip bundled wheel packages.Some Linux distribution packaging policies recommend against bundling dependencies. For example, Fedora installs wheel packages in the
/usr/share/python-wheels/directory and don't install theensurepip._bundledpackage.(Contributed by Victor Stinner in
42856{.interpreted-text role="issue"}.)Add a new
configure --without-static-libpython option <--without-static-libpython>{.interpreted-text role="option"} to not build thelibpythonMAJOR.MINOR.astatic library and not install thepython.oobject file.(Contributed by Victor Stinner in
43103{.interpreted-text role="issue"}.)The
configurescript now uses thepkg-configutility, if available, to detect the location of Tcl/Tk headers and libraries. As before, those locations can be explicitly specified with the--with-tcltk-includesand--with-tcltk-libsconfiguration options. (Contributed by Manolis Stamatogiannakis in42603{.interpreted-text role="issue"}.)Add
--with-openssl-rpath{.interpreted-text role="option"} option toconfigurescript. The option simplifies building Python with a custom OpenSSL installation, e.g../configure --with-openssl=/path/to/openssl --with-openssl-rpath=auto. (Contributed by Christian Heimes in43466{.interpreted-text role="issue"}.)
C API Changes
PEP 652: Maintaining the Stable ABI
The Stable ABI (Application Binary Interface) for extension modules or embedding Python is now explicitly defined. stable{.interpreted-text role="ref"} describes C API and ABI stability guarantees along with best practices for using the Stable ABI.
(Contributed by Petr Viktorin in 652{.interpreted-text role="pep"} and 43795{.interpreted-text role="issue"}.)
New Features
The result of
PyNumber_Index{.interpreted-text role="c:func"} now always has exact typeint{.interpreted-text role="class"}. Previously, the result could have been an instance of a subclass ofint. (Contributed by Serhiy Storchaka in40792{.interpreted-text role="issue"}.)Add a new
~PyConfig.orig_argv{.interpreted-text role="c:member"} member to thePyConfig{.interpreted-text role="c:type"} structure: the list of the original command line arguments passed to the Python executable. (Contributed by Victor Stinner in23427{.interpreted-text role="issue"}.)The
PyDateTime_DATE_GET_TZINFO{.interpreted-text role="c:func"} andPyDateTime_TIME_GET_TZINFO{.interpreted-text role="c:func"} macros have been added for accessing thetzinfoattributes ofdatetime.datetime{.interpreted-text role="class"} anddatetime.time{.interpreted-text role="class"} objects. (Contributed by Zackery Spytz in30155{.interpreted-text role="issue"}.)Add a
PyCodec_Unregister{.interpreted-text role="c:func"} function to unregister a codec search function. (Contributed by Hai Shi in41842{.interpreted-text role="issue"}.)The
PyIter_Send{.interpreted-text role="c:func"} function was added to allow sending value into iterator without raisingStopIterationexception. (Contributed by Vladimir Matveev in41756{.interpreted-text role="issue"}.)Add
PyUnicode_AsUTF8AndSize{.interpreted-text role="c:func"} to the limited C API. (Contributed by Alex Gaynor in41784{.interpreted-text role="issue"}.)Add
PyModule_AddObjectRef{.interpreted-text role="c:func"} function: similar toPyModule_AddObject{.interpreted-text role="c:func"} but don't steal a reference to the value on success. (Contributed by Victor Stinner in1635741{.interpreted-text role="issue"}.)Add
Py_NewRef{.interpreted-text role="c:func"} andPy_XNewRef{.interpreted-text role="c:func"} functions to increment the reference count of an object and return the object. (Contributed by Victor Stinner in42262{.interpreted-text role="issue"}.)The
PyType_FromSpecWithBases{.interpreted-text role="c:func"} andPyType_FromModuleAndSpec{.interpreted-text role="c:func"} functions now accept a single class as the bases argument. (Contributed by Serhiy Storchaka in42423{.interpreted-text role="issue"}.)The
PyType_FromModuleAndSpec{.interpreted-text role="c:func"} function now accepts NULLtp_docslot. (Contributed by Hai Shi in41832{.interpreted-text role="issue"}.)The
PyType_GetSlot{.interpreted-text role="c:func"} function can acceptstatic types <static-types>{.interpreted-text role="ref"}. (Contributed by Hai Shi and Petr Viktorin in41073{.interpreted-text role="issue"}.)Add a new
PySet_CheckExact{.interpreted-text role="c:func"} function to the C-API to check if an object is an instance ofset{.interpreted-text role="class"} but not an instance of a subtype. (Contributed by Pablo Galindo in43277{.interpreted-text role="issue"}.)Add
PyErr_SetInterruptEx{.interpreted-text role="c:func"} which allows passing a signal number to simulate. (Contributed by Antoine Pitrou in43356{.interpreted-text role="issue"}.)The limited C API is now supported if
Python is built in debug mode <debug-build>{.interpreted-text role="ref"} (if thePy_DEBUGmacro is defined). In the limited C API, thePy_INCREF{.interpreted-text role="c:func"} andPy_DECREF{.interpreted-text role="c:func"} functions are now implemented as opaque function calls, rather than accessing directly thePyObject.ob_refcnt{.interpreted-text role="c:member"} member, if Python is built in debug mode and thePy_LIMITED_APImacro targets Python 3.10 or newer. It became possible to support the limited C API in debug mode because thePyObject{.interpreted-text role="c:type"} structure is the same in release and debug mode since Python 3.8 (see36465{.interpreted-text role="issue"}).The limited C API is still not supported in the
--with-trace-refs{.interpreted-text role="option"} special build (Py_TRACE_REFSmacro). (Contributed by Victor Stinner in43688{.interpreted-text role="issue"}.)Add the
Py_Is(x, y) <Py_Is>{.interpreted-text role="c:func"} function to test if the x object is the y object, the same asx is yin Python. Add also thePy_IsNone{.interpreted-text role="c:func"},Py_IsTrue{.interpreted-text role="c:func"},Py_IsFalse{.interpreted-text role="c:func"} functions to test if an object is, respectively, theNonesingleton, theTruesingleton or theFalsesingleton. (Contributed by Victor Stinner in43753{.interpreted-text role="issue"}.)Add new functions to control the garbage collector from C code:
PyGC_Enable(){.interpreted-text role="c:func"},PyGC_Disable(){.interpreted-text role="c:func"},PyGC_IsEnabled(){.interpreted-text role="c:func"}. These functions allow to activate, deactivate and query the state of the garbage collector from C code without having to import thegc{.interpreted-text role="mod"} module.Add a new
Py_TPFLAGS_DISALLOW_INSTANTIATION{.interpreted-text role="c:macro"} type flag to disallow creating type instances. (Contributed by Victor Stinner in43916{.interpreted-text role="issue"}.)Add a new
Py_TPFLAGS_IMMUTABLETYPE{.interpreted-text role="c:macro"} type flag for creating immutable type objects: type attributes cannot be set nor deleted. (Contributed by Victor Stinner and Erlend E. Aasland in43908{.interpreted-text role="issue"}.)
Porting to Python 3.10
The
PY_SSIZE_T_CLEANmacro must now be defined to usePyArg_ParseTuple{.interpreted-text role="c:func"} andPy_BuildValue{.interpreted-text role="c:func"} formats which use#:es#,et#,s#,u#,y#,z#,U#andZ#. Seearg-parsing{.interpreted-text role="ref"} and353{.interpreted-text role="pep"}. (Contributed by Victor Stinner in40943{.interpreted-text role="issue"}.)Since
Py_REFCNT(){.interpreted-text role="c:func"} is changed to the inline static function,Py_REFCNT(obj) = new_refcntmust be replaced withPy_SET_REFCNT(obj, new_refcnt): seePy_SET_REFCNT(){.interpreted-text role="c:func"} (available since Python 3.9). For backward compatibility, this macro can be used:#if PY_VERSION_HEX < 0x030900A4 # define Py_SET_REFCNT(obj, refcnt) ((Py_REFCNT(obj) = (refcnt)), (void)0) #endif(Contributed by Victor Stinner in
39573{.interpreted-text role="issue"}.)Calling
PyDict_GetItem{.interpreted-text role="c:func"} withoutGIL{.interpreted-text role="term"} held had been allowed for historical reason. It is no longer allowed. (Contributed by Victor Stinner in40839{.interpreted-text role="issue"}.)PyUnicode_FromUnicode(NULL, size)andPyUnicode_FromStringAndSize(NULL, size)raiseDeprecationWarningnow. UsePyUnicode_New{.interpreted-text role="c:func"} to allocate Unicode object without initial data. (Contributed by Inada Naoki in36346{.interpreted-text role="issue"}.)The private
_PyUnicode_Name_CAPIstructure of the PyCapsule APIunicodedata.ucnhash_CAPIhas been moved to the internal C API. (Contributed by Victor Stinner in42157{.interpreted-text role="issue"}.)!Py_GetPath{.interpreted-text role="c:func"},!Py_GetPrefix{.interpreted-text role="c:func"},!Py_GetExecPrefix{.interpreted-text role="c:func"},!Py_GetProgramFullPath{.interpreted-text role="c:func"},!Py_GetPythonHome{.interpreted-text role="c:func"} and!Py_GetProgramName{.interpreted-text role="c:func"} functions now returnNULLif called beforePy_Initialize{.interpreted-text role="c:func"} (before Python is initialized). Use the newinit-config{.interpreted-text role="ref"} API to get theinit-path-config{.interpreted-text role="ref"}. (Contributed by Victor Stinner in42260{.interpreted-text role="issue"}.)PyList_SET_ITEM{.interpreted-text role="c:func"},PyTuple_SET_ITEM{.interpreted-text role="c:func"} andPyCell_SET{.interpreted-text role="c:func"} macros can no longer be used as l-value or r-value. For example,x = PyList_SET_ITEM(a, b, c)andPyList_SET_ITEM(a, b, c) = xnow fail with a compiler error. It prevents bugs likeif (PyList_SET_ITEM (a, b, c) < 0) ...test. (Contributed by Zackery Spytz and Victor Stinner in30459{.interpreted-text role="issue"}.)The non-limited API files
odictobject.h,parser_interface.h,picklebufobject.h,pyarena.h,pyctype.h,pydebug.h,pyfpe.h, andpytime.hhave been moved to theInclude/cpythondirectory. These files must not be included directly, as they are already included inPython.h; seeapi-includes{.interpreted-text role="ref"}. If they have been included directly, consider includingPython.hinstead. (Contributed by Nicholas Sim in35134{.interpreted-text role="issue"}.)Use the
Py_TPFLAGS_IMMUTABLETYPE{.interpreted-text role="c:macro"} type flag to create immutable type objects. Do not rely onPy_TPFLAGS_HEAPTYPE{.interpreted-text role="c:macro"} to decide if a type object is mutable or not; check ifPy_TPFLAGS_IMMUTABLETYPE{.interpreted-text role="c:macro"} is set instead. (Contributed by Victor Stinner and Erlend E. Aasland in43908{.interpreted-text role="issue"}.)The undocumented function
Py_FrozenMainhas been removed from the limited API. The function is mainly useful for custom builds of Python. (Contributed by Petr Viktorin in26241{.interpreted-text role="issue"}.)
Deprecated
- The
PyUnicode_InternImmortal()function is now deprecated and will be removed in Python 3.12: usePyUnicode_InternInPlace{.interpreted-text role="c:func"} instead. (Contributed by Victor Stinner in41692{.interpreted-text role="issue"}.)
Removed
Removed
Py_UNICODE_str*functions manipulatingPy_UNICODE*strings. (Contributed by Inada Naoki in41123{.interpreted-text role="issue"}.)Py_UNICODE_strlen: usePyUnicode_GetLength{.interpreted-text role="c:func"} orPyUnicode_GET_LENGTH{.interpreted-text role="c:macro"}Py_UNICODE_strcat: usePyUnicode_CopyCharacters{.interpreted-text role="c:func"} orPyUnicode_FromFormat{.interpreted-text role="c:func"}Py_UNICODE_strcpy,Py_UNICODE_strncpy: usePyUnicode_CopyCharacters{.interpreted-text role="c:func"} orPyUnicode_Substring{.interpreted-text role="c:func"}Py_UNICODE_strcmp: usePyUnicode_Compare{.interpreted-text role="c:func"}Py_UNICODE_strncmp: usePyUnicode_Tailmatch{.interpreted-text role="c:func"}Py_UNICODE_strchr,Py_UNICODE_strrchr: usePyUnicode_FindChar{.interpreted-text role="c:func"}
Removed
PyUnicode_GetMax(). Please migrate to new (393{.interpreted-text role="pep"}) APIs. (Contributed by Inada Naoki in41103{.interpreted-text role="issue"}.)Removed
PyLong_FromUnicode(). Please migrate toPyLong_FromUnicodeObject{.interpreted-text role="c:func"}. (Contributed by Inada Naoki in41103{.interpreted-text role="issue"}.)Removed
PyUnicode_AsUnicodeCopy(). Please usePyUnicode_AsUCS4Copy{.interpreted-text role="c:func"} orPyUnicode_AsWideCharString{.interpreted-text role="c:func"} (Contributed by Inada Naoki in41103{.interpreted-text role="issue"}.)Removed
_Py_CheckRecursionLimitvariable: it has been replaced byceval.recursion_limitof thePyInterpreterState{.interpreted-text role="c:type"} structure. (Contributed by Victor Stinner in41834{.interpreted-text role="issue"}.)Removed undocumented macros
Py_ALLOW_RECURSIONandPy_END_ALLOW_RECURSIONand therecursion_criticalfield of thePyInterpreterState{.interpreted-text role="c:type"} structure. (Contributed by Serhiy Storchaka in41936{.interpreted-text role="issue"}.)Removed the undocumented
PyOS_InitInterrupts()function. Initializing Python already implicitly installs signal handlers: seePyConfig.install_signal_handlers{.interpreted-text role="c:member"}. (Contributed by Victor Stinner in41713{.interpreted-text role="issue"}.)Remove the
PyAST_Validate()function. It is no longer possible to build a AST object (mod_tytype) with the public C API. The function was already excluded from the limited C API (384{.interpreted-text role="pep"}). (Contributed by Victor Stinner in43244{.interpreted-text role="issue"}.)Remove the
symtable.hheader file and the undocumented functions:PyST_GetScope()PySymtable_Build()PySymtable_BuildObject()PySymtable_Free()Py_SymtableString()Py_SymtableStringObject()
The
Py_SymtableString()function was part the stable ABI by mistake but it could not be used, because thesymtable.hheader file was excluded from the limited C API.Use Python
symtable{.interpreted-text role="mod"} module instead. (Contributed by Victor Stinner in43244{.interpreted-text role="issue"}.)Remove
PyOS_ReadlineFunctionPointer{.interpreted-text role="c:func"} from the limited C API headers and frompython3.dll, the library that provides the stable ABI on Windows. Since the function takes aFILE*argument, its ABI stability cannot be guaranteed. (Contributed by Petr Viktorin in43868{.interpreted-text role="issue"}.)Remove
ast.h,asdl.h, andPython-ast.hheader files. These functions were undocumented and excluded from the limited C API. Most names defined by these header files were not prefixed byPyand so could create names conflicts. For example,Python-ast.hdefined aYieldmacro which was conflict with theYieldname used by the Windows<winbase.h>header. Use the Pythonast{.interpreted-text role="mod"} module instead. (Contributed by Victor Stinner in43244{.interpreted-text role="issue"}.)Remove the compiler and parser functions using
struct _modtype, because the public AST C API was removed:PyAST_Compile()PyAST_CompileEx()PyAST_CompileObject()PyFuture_FromAST()PyFuture_FromASTObject()PyParser_ASTFromFile()PyParser_ASTFromFileObject()PyParser_ASTFromFilename()PyParser_ASTFromString()PyParser_ASTFromStringObject()
These functions were undocumented and excluded from the limited C API. (Contributed by Victor Stinner in
43244{.interpreted-text role="issue"}.)Remove the
pyarena.hheader file with functions:PyArena_New()PyArena_Free()PyArena_Malloc()PyArena_AddPyObject()
These functions were undocumented, excluded from the limited C API, and were only used internally by the compiler. (Contributed by Victor Stinner in
43244{.interpreted-text role="issue"}.)The
PyThreadState.use_tracingmember has been removed to optimize Python. (Contributed by Mark Shannon in43760{.interpreted-text role="issue"}.)
Notable security feature in 3.10.7
Converting between int{.interpreted-text role="class"} and str{.interpreted-text role="class"} in bases other than 2 (binary), 4, 8 (octal), 16 (hexadecimal), or 32 such as base 10 (decimal) now raises a ValueError{.interpreted-text role="exc"} if the number of digits in string form is above a limit to avoid potential denial of service attacks due to the algorithmic complexity. This is a mitigation for 2020-10735{.interpreted-text role="cve"}. This limit can be configured or disabled by environment variable, command line flag, or sys{.interpreted-text role="mod"} APIs. See the integer string conversion length limitation <int_max_str_digits>{.interpreted-text role="ref"} documentation. The default limit is 4300 digits in string form.
Notable security feature in 3.10.8
The deprecated !mailcap{.interpreted-text role="mod"} module now refuses to inject unsafe text (filenames, MIME types, parameters) into shell commands. Instead of using such text, it will warn and act as if a match was not found (or for test commands, as if the test failed). (Contributed by Petr Viktorin in 98966{.interpreted-text role="gh"}.)
Notable changes in 3.10.12
tarfile
- The extraction methods in
tarfile{.interpreted-text role="mod"}, andshutil.unpack_archive{.interpreted-text role="func"}, have a new a filter argument that allows limiting tar features than may be surprising or dangerous, such as creating files outside the destination directory. Seetarfile-extraction-filter{.interpreted-text role="ref"} for details. In Python 3.12, use without the filter argument will show aDeprecationWarning{.interpreted-text role="exc"}. In Python 3.14, the default will switch to'data'. (Contributed by Petr Viktorin in706{.interpreted-text role="pep"}.)