3.10.7
DateSeptember 23, 2022
EditorŁukasz Langa
This article explains the new features in Python 3.9, compared to 3.8. Python 3.9 was released on October 5, 2020.
For full details, see the changelog.
See also
PEP 596 - Python 3.9 Release Schedule
Summary – Release highlights¶
New syntax features:
PEP 584, union operators added to
dict
;PEP 585, type hinting generics in standard collections;
PEP 614, relaxed grammar restrictions on decorators.
New built-in features:
PEP 616, string methods to remove prefixes and suffixes.
New features in the standard library:
PEP 593, flexible function and variable annotations;
-
os.pidfd_open[]
added that allows process management without races and signals.
Interpreter improvements:
PEP 573, fast access to module state from methods of C extension types;
PEP 617, CPython now uses a new parser based on PEG;
a number of Python builtins [range, tuple, set, frozenset, list, dict] are now sped up using PEP 590 vectorcall;
garbage collection does not block on resurrected objects;
a number of Python modules [
_abc
,audioop
,_bz2
,_codecs
,_contextvars
,_crypt
,_functools
,_json
,_locale
,math
,operator
,resource
,time
,_weakref
] now use multiphase initialization as defined by PEP 489;a number of standard library modules [
audioop
,ast
,grp
,_hashlib
,pwd
,_posixsubprocess
,random
,select
,struct
,termios
,zlib
] are now using the stable ABI defined by PEP 384.
New library modules:
PEP 615, the IANA Time Zone Database is now present in the standard library in the
zoneinfo
module;an implementation of a topological sort of a graph is now provided in the new
graphlib
module.
Release process changes:
PEP 602, CPython adopts an annual release cycle.
You should check for DeprecationWarning in your code¶
When Python 2.7 was still supported, a lot of functionality in Python 3 was kept for backward compatibility with Python 2.7. With the end of Python 2 support, these backward compatibility layers have been removed, or will be removed soon. Most of them emitted a DeprecationWarning
warning for several years. For example, using collections.Mapping
instead of collections.abc.Mapping
emits a DeprecationWarning
since Python 3.3, released in 2012.
Test your application with the
-W
default
command-line option to see DeprecationWarning
and PendingDeprecationWarning
, or even with
-W
error
to treat them as errors. Warnings Filter can be used to ignore warnings from third-party code.
Python 3.9 is the last version providing those Python 2 backward compatibility layers, to give more time to Python projects maintainers to organize the removal of the Python 2 support and add support for Python 3.9.
Aliases to Abstract Base Classes in the collections
module, like collections.Mapping
alias to
collections.abc.Mapping
, are kept for one last release for backward compatibility. They will be removed from Python 3.10.
More generally, try to run your tests in the Python Development Mode which helps to prepare your code to make it compatible with the next Python version.
Note: a number of pre-existing deprecations were removed in this version of Python as well. Consult the Removed section.
New Features¶
Dictionary Merge & Update Operators¶
Merge [|
] and update [|=
] operators have been added to the built-in dict
class. Those complement the existing dict.update
and {**d1, **d2}
methods of merging dictionaries.
Example:
>>> x = {"key1": "value1 from x", "key2": "value2 from x"} >>> y = {"key2": "value2 from y", "key3": "value3 from y"} >>> x | y {'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'} >>> y | x {'key2': 'value2 from x', 'key3': 'value3 from y', 'key1': 'value1 from x'}
See PEP 584 for a full description. [Contributed by Brandt Bucher in bpo-36144.]
New String Methods to Remove Prefixes and Suffixes¶
str.removeprefix[prefix]
and str.removesuffix[suffix]
have been added to easily remove an unneeded prefix or a
suffix from a string. Corresponding bytes
, bytearray
, and collections.UserString
methods have also been added. See PEP 616 for a full description. [Contributed by Dennis Sweeney in bpo-39939.]
Type Hinting Generics in Standard Collections¶
In type annotations you can now use built-in collection types such as list
and dict
as generic types instead of importing the corresponding capitalized types [e.g. List
or Dict
] from typing
. Some other types in the standard library are also now generic, for example queue.Queue
.
Example:
def greet_all[names: list[str]] -> None: for name in names: print["Hello", name]
See PEP 585 for more details. [Contributed by Guido van Rossum, Ethan Smith, and Batuhan Taşkaya in bpo-39481.]
New Parser¶
Python 3.9 uses a new parser, based on PEG instead of LL[1]. The new parser’s performance is roughly comparable to that of the old parser, but the PEG formalism is more flexible than LL[1] when it comes to designing new language features. We’ll start using this flexibility in Python 3.10 and later.
The
ast
module uses the new parser and produces the same AST as the old parser.
In Python 3.10, the old parser will be deleted and so will all functionality that depends on it [primarily the parser
module, which has long been deprecated]. In Python 3.9 only, you can switch back to the LL[1] parser using a command line switch [-X
oldparser
]
or an environment variable [PYTHONOLDPARSER=1
].
See PEP 617 for more details. [Contributed by Guido van Rossum, Pablo Galindo and Lysandros Nikolaou in bpo-40334.]
Other Language Changes¶
__import__[]
now raisesImportError
instead ofValueError
, which used to occur when a relative import went past its top-level package. [Contributed by Ngalim Siregar in bpo-37444.]Python now gets the absolute path of the script filename specified on the command line [ex:
python3 script.py
]: the__file__
attribute of the__main__
module became an absolute path, rather than a relative path. These paths now remain valid after the current directory is changed byos.chdir[]
. As a side effect, the traceback also displays the absolute path for__main__
module frames in this case. [Contributed by Victor Stinner in bpo-20443.]In the Python Development Mode and in debug build, the encoding and errors arguments are now checked for string encoding and decoding operations. Examples:
open[]
,str.encode[]
andbytes.decode[]
.By default, for best performance, the errors argument is only checked at the first encoding/decoding error and the encoding argument is sometimes ignored for empty strings. [Contributed by Victor Stinner in bpo-37388.]
"".replace["", s, n]
now returnss
instead of an empty string for all non-zeron
. It is now consistent with"".replace["", s]
. There are similar changes forbytes
andbytearray
objects. [Contributed by Serhiy Storchaka in bpo-28029.]Any valid expression can now be used as a decorator. Previously, the grammar was much more restrictive. See PEP 614 for details. [Contributed by Brandt Bucher in bpo-39702.]
Improved help for the
typing
module. Docstrings are now shown for all special forms and special generic aliases [likeUnion
andList
]. Usinghelp[]
with generic alias likeList[int]
will show the help for the correspondent concrete type [list
in this case]. [Contributed by Serhiy Storchaka in bpo-40257.]Parallel running of
aclose[]
/asend[]
/athrow[]
is now prohibited, andag_running
now reflects the actual running status of the async generator. [Contributed by Yury Selivanov in bpo-30773.]Unexpected errors in calling the
__iter__
method are no longer masked byTypeError
in thein
operator and functionscontains[]
,indexOf[]
andcountOf[]
of theoperator
module. [Contributed by Serhiy Storchaka in bpo-40824.]Unparenthesized lambda expressions can no longer be the expression part in an
if
clause in comprehensions and generator expressions. See bpo-41848 and bpo-43755 for details.
New Modules¶
zoneinfo¶
The zoneinfo
module brings support for the
IANA time zone database to the standard library. It adds zoneinfo.ZoneInfo
, a concrete datetime.tzinfo
implementation backed by the system’s time zone data.
Example:
>>> from zoneinfo import ZoneInfo >>> from datetime import datetime, timedelta >>> # Daylight saving time >>> dt = datetime[2020, 10, 31, 12, tzinfo=ZoneInfo["America/Los_Angeles"]] >>> print[dt] 2020-10-31 12:00:00-07:00 >>> dt.tzname[] 'PDT' >>> # Standard time >>> dt += timedelta[days=7] >>> print[dt] 2020-11-07 12:00:00-08:00 >>> print[dt.tzname[]] PST
As a fall-back source of data for platforms that don’t ship the IANA database,
the tzdata
module was released as a first-party package – distributed via PyPI and maintained by the CPython core team.
See also
PEP 615 – Support for the IANA Time Zone Database in the Standard LibraryPEP written and implemented by Paul Ganssle
graphlib¶
A new module, graphlib
, was added that contains the
graphlib.TopologicalSorter
class to offer functionality to perform topological sorting of graphs. [Contributed by Pablo Galindo, Tim Peters and Larry Hastings in bpo-17005.]
Improved Modules¶
ast¶
Added the indent option to dump[]
which allows it to produce a multiline indented output. [Contributed
by Serhiy Storchaka in bpo-37995.]
Added ast.unparse[]
as a function in the ast
module that can be used to unparse an
ast.AST
object and produce a string with code that would produce an equivalent ast.AST
object when parsed. [Contributed by Pablo Galindo and Batuhan Taskaya in bpo-38870.]
Added docstrings to AST nodes that contains the ASDL signature used to construct that node. [Contributed by Batuhan Taskaya in bpo-39638.]
asyncio¶
Due to significant security concerns, the reuse_address parameter of
asyncio.loop.create_datagram_endpoint[]
is no longer supported. This is because of the behavior of the socket option SO_REUSEADDR
in UDP. For more details, see the documentation for loop.create_datagram_endpoint[]
. [Contributed by Kyle Stanley, Antoine Pitrou, and Yury Selivanov in bpo-37228.]
Added a new coroutine shutdown_default_executor[]
that schedules a shutdown for the default executor that waits on the
ThreadPoolExecutor
to finish closing. Also, asyncio.run[]
has been updated to use the new coroutine. [Contributed by Kyle Stanley
in bpo-34037.]
Added asyncio.PidfdChildWatcher
, a Linux-specific child watcher implementation that polls process file descriptors. [bpo-38692]
Added a new
coroutine asyncio.to_thread[]
. It is mainly used for running IO-bound functions in a separate thread to avoid blocking the event loop, and essentially works as a high-level version of
run_in_executor[]
that can directly take keyword arguments. [Contributed by Kyle Stanley and Yury Selivanov in bpo-32309.]
When cancelling the task due to a timeout,
asyncio.wait_for[]
will now wait until the cancellation is complete also in the case when timeout is = 0x03090000
// This was not needed before Python 3.9 [Python issue 35810 and 40217]
Py_VISIT[Py_TYPE[self]];
#endif
}
If your
traverse function delegates to tp_traverse
of its base class [or another type], ensure that Py_TYPE[self]
is visited only once. Note that only heap type are expected to visit the type in tp_traverse
.
For example, if your
tp_traverse
function includes:base->tp_traverse[self, visit, arg]then add:
#if PY_VERSION_HEX >= 0x03090000 // This was not needed before Python 3.9 [bpo-35810 and bpo-40217] if [base->tp_flags & Py_TPFLAGS_HEAPTYPE] { // a heap type's tp_traverse already visited Py_TYPE[self] } else { Py_VISIT[Py_TYPE[self]]; } #else
[See bpo-35810 and bpo-40217 for more information.]
The functions PyEval_CallObject
, PyEval_CallFunction
, PyEval_CallMethod
and PyEval_CallObjectWithKeywords
are deprecated. Use PyObject_Call[]
and its variants instead. [See more details in
bpo-29548.]
CPython bytecode changes¶
The
LOAD_ASSERTION_ERROR
opcode was added for handling theassert
statement. Previously, the assert statement would not work correctly if theAssertionError
exception was being shadowed. [Contributed by Zackery Spytz in bpo-34880.]The
COMPARE_OP
opcode was split into four distinct instructions:COMPARE_OP
for rich comparisonsIS_OP
for ‘is’ and ‘is not’ testsCONTAINS_OP
for ‘in’ and ‘not in’ testsJUMP_IF_NOT_EXC_MATCH
for checking exceptions in ‘try-except’ statements.
[Contributed by Mark Shannon in bpo-39156.]
Build Changes¶
Added
--with-platlibdir
option to theconfigure
script: name of the platform-specific library directory, stored in the newsys.platlibdir
attribute. Seesys.platlibdir
attribute for more information. [Contributed by Jan Matějek, Matěj Cepl, Charalampos Stratakis and Victor Stinner in bpo-1294959.]The
COUNT_ALLOCS
special build macro has been removed. [Contributed by Victor Stinner in bpo-39489.]On non-Windows platforms, the
setenv[]
andunsetenv[]
functions are now required to build Python. [Contributed by Victor Stinner in bpo-39395.]On non-Windows platforms, creating
bdist_wininst
installers is now officially unsupported. [See bpo-10945 for more details.]When building Python on macOS from source,
_tkinter
now links with non-system Tcl and Tk frameworks if they are installed in/Library/Frameworks
, as had been the case on older releases of macOS. If a macOS SDK is explicitly configured, by using--enable-universalsdk
or-isysroot
, only the SDK itself is searched. The default behavior can still be overridden with--with-tcltk-includes
and--with-tcltk-libs
. [Contributed by Ned Deily in bpo-34956.]Python can now be built for Windows 10 ARM64. [Contributed by Steve Dower in bpo-33125.]
Some individual tests are now skipped when
--pgo
is used. The tests in question increased the PGO task time significantly and likely didn’t help improve optimization of the final executable. This speeds up the task by a factor of about 15x. Running the full unit test suite is slow. This change may result in a slightly less optimized build since not as many code branches will be executed. If you are willing to wait for the much slower build, the old behavior can be restored using./configure [..] PROFILE_TASK="-m test --pgo-extended"
. We make no guarantees as to which PGO task set produces a faster build. Users who care should run their own relevant benchmarks as results can depend on the environment, workload, and compiler tool chain. [See bpo-36044 and bpo-37707 for more details.]
C API Changes¶
New Features¶
PEP 573: Added
PyType_FromModuleAndSpec[]
to associate a module with a class;PyType_GetModule[]
andPyType_GetModuleState[]
to retrieve the module and its state; andPyCMethod
andMETH_METHOD
to allow a method to access the class it was defined in. [Contributed by Marcel Plch and Petr Viktorin in bpo-38787.]Added
PyFrame_GetCode[]
function: get a frame code. AddedPyFrame_GetBack[]
function: get the frame next outer frame. [Contributed by Victor Stinner in bpo-40421.]Added
PyFrame_GetLineNumber[]
to the limited C API. [Contributed by Victor Stinner in bpo-40421.]Added
PyThreadState_GetInterpreter[]
andPyInterpreterState_Get[]
functions to get the interpreter. AddedPyThreadState_GetFrame[]
function to get the current frame of a Python thread state. AddedPyThreadState_GetID[]
function: get the unique identifier of a Python thread state. [Contributed by Victor Stinner in bpo-39947.]Added a new public
PyObject_CallNoArgs[]
function to the C API, which calls a callable Python object without any arguments. It is the most efficient way to call a callable Python object without any argument. [Contributed by Victor Stinner in bpo-37194.]Changes in the limited C API [if
Py_LIMITED_API
macro is defined]:Provide
Py_EnterRecursiveCall[]
andPy_LeaveRecursiveCall[]
as regular functions for the limited API. Previously, there were defined as macros, but these macros didn’t compile with the limited C API which cannot accessPyThreadState.recursion_depth
field [the structure is opaque in the limited C API].PyObject_INIT[]
andPyObject_INIT_VAR[]
become regular “opaque” function to hide implementation details.
[Contributed by Victor Stinner in bpo-38644 and bpo-39542.]
The
PyModule_AddType[]
function is added to help adding a type to a module. [Contributed by Dong-hee Na in bpo-40024.]Added the functions
PyObject_GC_IsTracked[]
andPyObject_GC_IsFinalized[]
to the public API to allow to query if Python objects are being currently tracked or have been already finalized by the garbage collector respectively. [Contributed by Pablo Galindo Salgado in bpo-40241.]Added
_PyObject_FunctionStr[]
to get a user-friendly string representation of a function-like object. [Patch by Jeroen Demeyer in bpo-37645.]Added
PyObject_CallOneArg[]
for calling an object with one positional argument [Patch by Jeroen Demeyer in bpo-37483.]
Porting to Python 3.9¶
PyInterpreterState.eval_frame
[PEP 523] now requires a new mandatory tstate parameter [PyThreadState*
]. [Contributed by Victor Stinner in bpo-38500.]Extension modules:
m_traverse
,m_clear
andm_free
functions ofPyModuleDef
are no longer called if the module state was requested but is not allocated yet. This is the case immediately after the module is created and before the module is executed [Py_mod_exec
function]. More precisely, these functions are not called ifm_size
is greater than 0 and the module state [as returned byPyModule_GetState[]
] isNULL
.Extension modules without module state [
m_size >> from typing import Literal >>> Literal[{0}] >>> Literal[{0}] == Literal[{False}] Traceback [most recent call last]: File "", line 1, in TypeError: unhashable type: 'set'
[Contributed by Yurii Karabas in bpo-42345.]
macOS 11.0 [Big Sur] and Apple Silicon Mac support¶
As of 3.9.1, Python now fully supports building and running on macOS 11.0 [Big Sur]
and on Apple Silicon Macs [based on the ARM64
architecture]. A new universal build variant, universal2
, is now available to natively support both ARM64
and Intel 64
in one set of executables. Binaries can also now be built on current versions of macOS to be deployed on a range of older macOS versions [tested to 10.9] while making some newer OS functions and options conditionally available based on the operating system version in use at runtime [“weaklinking”].
[Contributed by Ronald Oussoren and Lawrence D’Anna in bpo-41100.]
Notable changes in Python 3.9.2¶
collections.abc¶
collections.abc.Callable
generic now flattens type parameters, similar to what typing.Callable
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
can now be subclassed, and a subclass will be returned when subscripting the
collections.abc.Callable
type. Code which accesses the arguments via typing.get_args[]
or __args__
need to account for this change. A
DeprecationWarning
may be emitted for invalid forms of parameterizing collections.abc.Callable
which may have passed silently in Python 3.9.1. This
DeprecationWarning
will become a TypeError
in Python 3.10. [Contributed by Ken Jin in bpo-42195.]
urllib.parse¶
Earlier Python versions allowed using both ;
and &
as query parameter separators in urllib.parse.parse_qs[]
and
urllib.parse.parse_qsl[]
. 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[]
and
cgi.parse_multipart[]
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 bpo-42967.]
Which is the newest python?
When did Python 3.9 release?
What is the latest version of Python 2022?
How long will Python 3.9 be supported?
Release | Released | Security Support |
3.9 | 1 year and 11 months ago [05 Oct 2020] | Ends in 3 years [05 Oct 2025] |
3.8 | 2 years and 11 months ago [14 Oct 2019] | Ends in 2 years [14 Oct 2024] |
3.7 | 4 years ago [26 Jun 2018] | Ends in 9 months [27 Jun 2023] |
3.6 | 5 years and 9 months ago [22 Dec 2016] | Ended 9 months ago [23 Dec 2021] |