What’s New In Python 3.12¶
- Editor:
Adam Turner
This article explains the new features in Python 3.12, compared to 3.11. Python 3.12 was released on October 2, 2023. For full details, see the changelog.
See also
PEP 693 – Python 3.12 Release Schedule
Summary – Release highlights¶
Python 3.12 is a stable release of the Python programming language,
with a mix of changes to the language and the standard library.
The library changes focus on cleaning up deprecated APIs, usability, and correctness.
Of note, the distutils package has been removed from the standard library.
Filesystem support in os and pathlib has seen a number of improvements,
and several modules have better performance.
The language changes focus on usability,
as f-strings have had many limitations removed
and ‘Did you mean …’ suggestions continue to improve.
The new type parameter syntax
and type statement improve ergonomics for using generic types and type aliases with static type checkers.
This article doesn’t attempt to provide a complete specification of all new features, but instead gives a convenient overview. For full details, you should refer to the documentation, such as the Library Reference and Language Reference. If you want to understand the complete implementation and design rationale for a change, refer to the PEP for a particular new feature; but note that PEPs usually are not kept up-to-date once a feature has been fully implemented.
New syntax features:
New grammar features:
Interpreter improvements:
PEP 669, low impact monitoring
Improved ‘Did you mean …’ suggestions for
NameError,ImportError, andSyntaxErrorexceptions
Python data model improvements:
PEP 688, using the buffer protocol from Python
Significant improvements in the standard library:
The
pathlib.Pathclass now supports subclassingThe
osmodule received several improvements for Windows supportA command-line interface has been added to the
sqlite3moduleisinstance()checks againstruntime-checkable protocolsenjoy a speed up of between two and 20 timesThe
asynciopackage has had a number of performance improvements, with some benchmarks showing a 75% speed up.A command-line interface has been added to the
uuidmoduleDue to the changes in PEP 701, producing tokens via the
tokenizemodule is up to 64% faster.
Security improvements:
Replace the builtin
hashlibimplementations of SHA1, SHA3, SHA2-384, SHA2-512, and MD5 with formally verified code from the HACL* project. These builtin implementations remain as fallbacks that are only used when OpenSSL does not provide them.
C API improvements:
CPython implementation improvements:
PEP 709, comprehension inlining
CPython support for the Linux
perfprofilerImplement stack overflow protection on supported platforms
New typing features:
PEP 698,
typing.override()decorator
Important deprecations, removals or restrictions:
PEP 623: Remove
wstrfrom Unicode objects in Python’s C API, reducing the size of everystrobject by at least 8 bytes.PEP 632: Remove the
distutilspackage. See the migration guide for advice replacing the APIs it provided. The third-party Setuptools package continues to providedistutils, if you still require it in Python 3.12 and beyond.gh-95299: Do not pre-install
setuptoolsin virtual environments created withvenv. This means thatdistutils,setuptools,pkg_resources, andeasy_installwill no longer available by default; to access these runpip install setuptoolsin the activated virtual environment.The
asynchat,asyncore, andimpmodules have been removed, along with severalunittest.TestCasemethod aliases.
New Features¶
PEP 695: Type Parameter Syntax¶
Generic classes and functions under PEP 484 were declared using a verbose syntax that left the scope of type parameters unclear and required explicit declarations of variance.
PEP 695 introduces a new, more compact and explicit way to create generic classes and functions:
def max[T](args: Iterable[T]) -> T:
...
class list[T]:
def __getitem__(self, index: int, /) -> T:
...
def append(self, element: T) -> None:
...
In addition, the PEP introduces a new way to declare type aliases
using the type statement, which creates an instance of
TypeAliasType:
type Point = tuple[float, float]
Type aliases can also be generic:
type Point[T] = tuple[T, T]
The new syntax allows declaring TypeVarTuple
and ParamSpec parameters, as well as TypeVar
parameters with bounds or constraints:
type IntFunc[**P] = Callable[P, int] # ParamSpec
type LabeledTuple[*Ts] = tuple[str, *Ts] # TypeVarTuple
type HashableSequence[T: Hashable] = Sequence[T] # TypeVar with bound
type IntOrStrSequence[T: (int, str)] = Sequence[T] # TypeVar with constraints
The value of type aliases and the bound and constraints of type variables created through this syntax are evaluated only on demand (see lazy evaluation). This means type aliases are able to refer to other types defined later in the file.
Type parameters declared through a type parameter list are visible within the scope of the declaration and any nested scopes, but not in the outer scope. For example, they can be used in the type annotations for the methods of a generic class or in the class body. However, they cannot be used in the module scope after the class is defined. See Type parameter lists for a detailed description of the runtime semantics of type parameters.
In order to support these scoping semantics, a new kind of scope is introduced, the annotation scope. Annotation scopes behave for the most part like function scopes, but interact differently with enclosing class scopes. In Python 3.13, annotations will also be evaluated in annotation scopes.
See PEP 695 for more details.
(PEP written by Eric Traut. Implementation by Jelle Zijlstra, Eric Traut, and others in gh-103764.)
PEP 701: Syntactic formalization of f-strings¶
PEP 701 lifts some restrictions on the usage of f-strings. Expression components inside f-strings can now be any valid Python expression, including strings reusing the same quote as the containing f-string, multi-line expressions, comments, backslashes, and unicode escape sequences. Let’s cover these in detail:
Quote reuse: in Python 3.11, reusing the same quotes as the enclosing f-string raises a
SyntaxError, forcing the user to either use other available quotes (like using double quotes or triple quotes if the f-string uses single quotes). In Python 3.12, you can now do things like this:>>> songs = ['Take me back to Eden', 'Alkaline', 'Ascensionism'] >>> f"This is the playlist: {", ".join(songs)}" 'This is the playlist: Take me back to Eden, Alkaline, Ascensionism'
Note that before this change there was no explicit limit in how f-strings can be nested, but the fact that string quotes cannot be reused inside the expression component of f-strings made it impossible to nest f-strings arbitrarily. In fact, this is the most nested f-string that could be written:
>>> f"""{f'''{f'{f"{1+1}"}'}'''}""" '2'
As now f-strings can contain any valid Python expression inside expression components, it is now possible to nest f-strings arbitrarily:
>>> f"{f"{f"{f"{f"{f"{1+1}"}"}"}"}"}" '2'
Multi-line expressions and comments: In Python 3.11, f-string expressions must be defined in a single line, even if the expression within the f-string could normally span multiple lines (like literal lists being defined over multiple lines), making them harder to read. In Python 3.12 you can now define f-strings spanning multiple lines, and add inline comments:
>>> f"This is the playlist: {", ".join([ ... 'Take me back to Eden', # My, my, those eyes like fire ... 'Alkaline', # Not acid nor alkaline ... 'Ascensionism' # Take to the broken skies at last ... ])}" 'This is the playlist: Take me back to Eden, Alkaline, Ascensionism'Backslashes and unicode characters: before Python 3.12 f-string expressions couldn’t contain any
\character. This also affected unicode escape sequences (such as\N{snowman}) as these contain the\Npart that previously could not be part of expression components of f-strings. Now, you can define expressions like this:>>> print(f"This is the playlist: {"\n".join(songs)}") This is the playlist: Take me back to Eden Alkaline Ascensionism >>> print(f"This is the playlist: {"\N{BLACK HEART SUIT}".join(songs)}") This is the playlist: Take me back to Eden♥Alkaline♥Ascensionism
See PEP 701 for more details.
As a positive side-effect of how this feature has been implemented (by parsing f-strings
with the PEG parser), now error messages for f-strings are more precise
and include the exact location of the error. For example, in Python 3.11, the following
f-string raises a SyntaxError:
>>> my_string = f"{x z y}" + f"{1 + 1}"
File "<stdin>", line 1
(x z y)
^^^
SyntaxError: f-string: invalid syntax. Perhaps you forgot a comma?
but the error message doesn’t include the exact location of the error within the line and also has the expression artificially surrounded by parentheses. In Python 3.12, as f-strings are parsed with the PEG parser, error messages can be more precise and show the entire line:
>>> my_string = f"{x z y}" + f"{1 + 1}"
File "<stdin>", line 1
my_string = f"{x z y}" + f"{1 + 1}"
^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?
(Contributed by Pablo Galindo, Batuhan Taskaya, Lysandros Nikolaou, Cristián Maureira-Fredes and Marta Gómez in gh-102856. PEP written by Pablo Galindo, Batuhan Taskaya, Lysandros Nikolaou and Marta Gómez).
PEP 684: A Per-Interpreter GIL¶
PEP 684 introduces a per-interpreter GIL, so that sub-interpreters may now be created with a unique GIL per interpreter. This allows Python programs to take full advantage of multiple CPU cores. This is currently only available through the C-API, though a Python API is anticipated for 3.13.
Use the new Py_NewInterpreterFromConfig() function to
create an interpreter with its own GIL:
PyInterpreterConfig config = {
.check_multi_interp_extensions = 1,
.gil = PyInterpreterConfig_OWN_GIL,
};
PyThreadState *tstate = NULL;
PyStatus status = Py_NewInterpreterFromConfig(&tstate, &config);
if (PyStatus_Exception(status)) {
return -1;
}
/* The new interpreter is now active in the current thread. */
For further examples how to use the C-API for sub-interpreters with a
per-interpreter GIL, see Modules/_xxsubinterpretersmodule.c.
(Contributed by Eric Snow in gh-104210, etc.)
PEP 669: Low impact monitoring for CPython¶
PEP 669 defines a new API for profilers,
debuggers, and other tools to monitor events in CPython.
It covers a wide range of events, including calls,
returns, lines, exceptions, jumps, and more.
This means that you only pay for what you use, providing support
for near-zero overhead debuggers and coverage tools.
See sys.monitoring for details.
(Contributed by Mark Shannon in gh-103082.)
PEP 688: Making the buffer protocol accessible in Python¶
PEP 688 introduces a way to use the buffer protocol
from Python code. Classes that implement the __buffer__() method
are now usable as buffer types.
The new collections.abc.Buffer ABC provides a standard
way to represent buffer objects, for example in type annotations.
The new inspect.BufferFlags enum represents the flags that
can be used to customize buffer creation.
(Contributed by Jelle Zijlstra in gh-102500.)
PEP 709: Comprehension inlining¶
Dictionary, list, and set comprehensions are now inlined, rather than creating a new single-use function object for each execution of the comprehension. This speeds up execution of a comprehension by up to two times. See PEP 709 for further details.
Comprehension iteration variables remain isolated and don’t overwrite a variable of the same name in the outer scope, nor are they visible after the comprehension. Inlining does result in a few visible behavior changes:
There is no longer a separate frame for the comprehension in tracebacks, and tracing/profiling no longer shows the comprehension as a function call.
The
symtablemodule will no longer produce child symbol tables for each comprehension; instead, the comprehension’s locals will be included in the parent function’s symbol table.Calling
locals()inside a comprehension now includes variables from outside the comprehension, and no longer includes the synthetic.0variable for the comprehension “argument”.A comprehension iterating directly over
locals()(e.g.[k for k in locals()]) may see “RuntimeError: dictionary changed size during iteration” when run under tracing (e.g. code coverage measurement). This is the same behavior already seen in e.g.for k in locals():. To avoid the error, first create a list of keys to iterate over:keys = list(locals()); [k for k in keys].
(Contributed by Carl Meyer and Vladimir Matveev in PEP 709.)
Improved Error Messages¶
Modules from the standard library are now potentially suggested as part of the error messages displayed by the interpreter when a
NameErroris raised to the top level. (Contributed by Pablo Galindo in gh-98254.)>>> sys.version_info Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'sys' is not defined. Did you forget to import 'sys'?
Improve the error suggestion for
NameErrorexceptions for instances. Now if aNameErroris raised in a method and the instance has an attribute that’s exactly equal to the name in the exception, the suggestion will includeself.<NAME>instead of the closest match in the method scope. (Contributed by Pablo Galindo in gh-99139.)>>> class A: ... def __init__(self): ... self.blech = 1 ... ... def foo(self): ... somethin = blech ... >>> A().foo() Traceback (most recent call last): File "<stdin>", line 1 somethin = blech ^^^^^ NameError: name 'blech' is not defined. Did you mean: 'self.blech'?
Improve the
SyntaxErrorerror message when the user typesimport x from yinstead offrom y import x. (Contributed by Pablo Galindo in gh-98931.)>>> import a.y.z from b.y.z Traceback (most recent call last): File "<stdin>", line 1 import a.y.z from b.y.z ^^^^^^^^^^^^^^^^^^^^^^^ SyntaxError: Did you mean to use 'from ... import ...' instead?
ImportErrorexceptions raised from failedfrom <module> import <name>statements now include suggestions for the value of<name>based on the available names in<module>. (Contributed by Pablo Galindo in gh-91058.)>>> from collections import chainmap Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: cannot import name 'chainmap' from 'collections'. Did you mean: 'ChainMap'?
Other Language Changes¶
The parser now raises
SyntaxErrorwhen parsing source code containing null bytes. (Contributed by Pablo Galindo in gh-96670.)A backslash-character pair that is not a valid escape sequence now generates a
SyntaxWarning, instead ofDeprecationWarning. For example,re.compile("\d+\.\d+")now emits aSyntaxWarning("\d"is an invalid escape sequence, use raw strings for regular expression:re.compile(r"\d+\.\d+")). In a future Python version,SyntaxErrorwill eventually be raised, instead ofSyntaxWarning. (Contributed by Victor Stinner in gh-98401.)Octal escapes with value larger than
0o377(ex:"\477"), deprecated in Python 3.11, now produce aSyntaxWarning, instead ofDeprecationWarning. In a future Python version they will be eventually aSyntaxError. (Contributed by Victor Stinner in gh-98401.)Variables used in the target part of comprehensions that are not stored to can now be used in assignment expressions (
:=). For example, in[(b := 1) for a, b.prop in some_iter], the assignment tobis now allowed. Note that assigning to variables stored to in the target part of comprehensions (likea) is still disallowed, as per PEP 572. (Contributed by Nikita Sobolev in gh-100581.)Exceptions raised in a class or type’s
__set_name__method are no longer wrapped by aRuntimeError. Context information is added to the exception as a PEP 678 note. (Contributed by Irit Katriel in gh-77757.)When a
try-except*construct handles the entireExceptionGroupand raises one other exception, that exception is no longer wrapped in anExceptionGroup. Also changed in version 3.11.4. (Contributed by Irit Katriel in gh-103590.)The Garbage Collector now runs only on the eval breaker mechanism of the Python bytecode evaluation loop instead of object allocations. The GC can also run when
PyErr_CheckSignals()is called so C extensions that need to run for a long time without executing any Python code also have a chance to execute the GC periodically. (Contributed by Pablo Galindo in gh-97922.)All builtin and extension callables expecting boolean parameters now accept arguments of any type instead of just
boolandint. (Contributed by Serhiy Storchaka in gh-60203.)memoryviewnow supports the half-float type (the “e” format code). (Contributed by Donghee Na and Antoine Pitrou in gh-90751.)sliceobjects are now hashable, allowing them to be used as dict keys and set items. (Contributed by Will Bradshaw, Furkan Onder, and Raymond Hettinger in gh-101264.)sum()now uses Neumaier summation to improve accuracy and commutativity when summing floats or mixed ints and floats. (Contributed by Raymond Hettinger in gh-100425.)ast.parse()now raisesSyntaxErrorinstead ofValueErrorwhen parsing source code containing null bytes. (Contributed by Pablo Galindo in gh-96670.)The extraction methods in
tarfile, andshutil.unpack_archive(), 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. See tarfile extraction filters for details. In Python 3.14, the default will switch to'data'. (Contributed by Petr Viktorin in PEP 706.)types.MappingProxyTypeinstances are now hashable if the underlying mapping is hashable. (Contributed by Serhiy Storchaka in gh-87995.)Add support for the perf profiler through the new environment variable
PYTHONPERFSUPPORTand command-line option-X perf, as well as the newsys.activate_stack_trampoline(),sys.deactivate_stack_trampoline(), andsys.is_stack_trampoline_active()functions. (Design by Pablo Galindo. Contributed by Pablo Galindo and Christian Heimes with contributions from Gregory P. Smith [Google] and Mark Shannon in gh-96123.)
New Modules¶
None.
Improved Modules¶
array¶
The
array.arrayclass now supports subscripting, making it a generic type. (Contributed by Jelle Zijlstra in gh-98658.)
asyncio¶
The performance of writing to sockets in
asynciohas been significantly improved.asyncionow avoids unnecessary copying when writing to sockets and usessendmsg()if the platform supports it. (Contributed by Kumar Aditya in gh-91166.)Add
asyncio.eager_task_factory()andasyncio.create_eager_task_factory()functions to allow opting an event loop in to eager task execution, making some use-cases 2x to 5x faster. (Contributed by Jacob Bower & Itamar Oren in gh-102853, gh-104140, and gh-104138)On Linux,
asynciousesasyncio.PidfdChildWatcherby default ifos.pidfd_open()is available and functional instead ofasyncio.ThreadedChildWatcher. (Contributed by Kumar Aditya in gh-98024.)The event loop now uses the best available child watcher for each platform (
asyncio.PidfdChildWatcherif supported andasyncio.ThreadedChildWatcherotherwise), so manually configuring a child watcher is not recommended. (Contributed by Kumar Aditya in gh-94597.)Add loop_factory parameter to
asyncio.run()to allow specifying a custom event loop factory. (Contributed by Kumar Aditya in gh-99388.)Add C implementation of
asyncio.current_task()for 4x-6x speedup. (Contributed by Itamar Oren and Pranav Thulasiram Bhat in gh-100344.)asyncio.iscoroutine()now returnsFalsefor generators asasynciodoes not support legacy generator-based coroutines. (Contributed by Kumar Aditya in gh-102748.)asyncio.wait()andasyncio.as_completed()now accepts generators yielding tasks. (Contributed by Kumar Aditya in gh-78530.)
calendar¶
Add enums
calendar.Monthandcalendar.Daydefining months of the year and days of the week. (Contributed by Prince Roshan in gh-103636.)
csv¶
Add
csv.QUOTE_NOTNULLandcsv.QUOTE_STRINGSflags to provide finer grained control ofNoneand empty strings byreaderandwriterobjects.
dis¶
Pseudo instruction opcodes (which are used by the compiler but do not appear in executable bytecode) are now exposed in the
dismodule.HAVE_ARGUMENTis still relevant to real opcodes, but it is not useful for pseudo instructions. Use the newdis.hasargcollection instead. (Contributed by Irit Katriel in gh-94216.)Add the
dis.hasexccollection to signify instructions that set an exception handler. (Contributed by Irit Katriel in gh-94216.)
fractions¶
Objects of type
fractions.Fractionnow support float-style formatting. (Contributed by Mark Dickinson in gh-100161.)
importlib.resources¶
importlib.resources.as_file()now supports resource directories. (Contributed by Jason R. Coombs in gh-97930.)Rename first parameter of
importlib.resources.files()to anchor. (Contributed by Jason R. Coombs in gh-100598.)
inspect¶
Add
inspect.markcoroutinefunction()to mark sync functions that return a coroutine for use withinspect.iscoroutinefunction(). (Contributed by Carlton Gibson in gh-99247.)Add
inspect.getasyncgenstate()andinspect.getasyncgenlocals()for determining the current state of asynchronous generators. (Contributed by Thomas Krennwallner in gh-79940.)The performance of
inspect.getattr_static()has been considerably improved. Most calls to the function should be at least 2x faster than they were in Python 3.11. (Contributed by Alex Waygood in gh-103193.)
itertools¶
Add
itertools.batched()for collecting into even-sized tuples where the last batch may be shorter than the rest. (Contributed by Raymond Hettinger in gh-98363.)
math¶
Add
math.sumprod()for computing a sum of products. (Contributed by Raymond Hettinger in gh-100485.)Extend
math.nextafter()to include a steps argument for moving up or down multiple steps at a time. (Contributed by Matthias Goergens, Mark Dickinson, and Raymond Hettinger in gh-94906.)
os¶
Add
os.PIDFD_NONBLOCKto open a file descriptor for a process withos.pidfd_open()in non-blocking mode. (Contributed by Kumar Aditya in gh-93312.)os.DirEntrynow includes anos.DirEntry.is_junction()method to check if the entry is a junction. (Contributed by Charles Machalow in gh-99547.)Add
os.listdrives(),os.listvolumes()andos.listmounts()functions on Windows for enumerating drives, volumes and mount points. (Contributed by Steve Dower in gh-102519.)os.stat()andos.lstat()are now more accurate on Windows. Thest_birthtimefield will now be filled with the creation time of the file, andst_ctimeis deprecated but still contains the creation time (but in the future will return the last metadata change, for consistency with other platforms).st_devmay be up to 64 bits andst_inoup to 128 bits depending on your file system, andst_rdevis always set to zero rather than incorrect values. Both functions may be significantly faster on newer releases of Windows. (Contributed by Steve Dower in gh-99726.)
os.path¶
Add
os.path.isjunction()to check if a given path is a junction. (Contributed by Charles Machalow in gh-99547.)Add
os.path.splitroot()to split a path into a triad(drive, root, tail). (Contributed by Barney Gale in gh-101000.)
pathlib¶
Add support for subclassing
pathlib.PurePathandpathlib.Path, plus their Posix- and Windows-specific variants. Subclasses may override thepathlib.PurePath.with_segments()method to pass information between path instances.Add
pathlib.Path.walk()for walking the directory trees and generating all file or directory names within them, similar toos.walk(). (Contributed by Stanislav Zmiev in gh-90385.)Add walk_up optional parameter to
pathlib.PurePath.relative_to()to allow the insertion of..entries in the result; this behavior is more consistent withos.path.relpath(). (Contributed by Domenico Ragusa in gh-84538.)Add
pathlib.Path.is_junction()as a proxy toos.path.isjunction(). (Contributed by Charles Machalow in gh-99547.)Add case_sensitive optional parameter to
pathlib.Path.glob(),pathlib.Path.rglob()andpathlib.PurePath.match()for matching the path’s case sensitivity, allowing for more precise control over the matching process.
platform¶
Add support for detecting Windows 11 and Windows Server releases past 2012. Previously, lookups on Windows Server platforms newer than Windows Server 2012 and on Windows 11 would return
Windows-10. (Contributed by Steve Dower in gh-89545.)
pdb¶
Add convenience variables to hold values temporarily for debug session and provide quick access to values like the current frame or the return value. (Contributed by Tian Gao in gh-103693.)
random¶
Add
random.binomialvariate(). (Contributed by Raymond Hettinger in gh-81620.)Add a default of
lambd=1.0torandom.expovariate(). (Contributed by Raymond Hettinger in gh-100234.)
shutil¶
shutil.make_archive()now passes the root_dir argument to custom archivers which support it. In this case it no longer temporarily changes the current working directory of the process to root_dir to perform archiving. (Contributed by Serhiy Storchaka in gh-74696.)shutil.rmtree()now accepts a new argument onexc which is an error handler like onerror but which expects an exception instance rather than a (typ, val, tb) triplet. onerror is deprecated. (Contributed by Irit Katriel in gh-102828.)shutil.which()now consults the PATHEXT environment variable to find matches within PATH on Windows even when the given cmd includes a directory component. (Contributed by Charles Machalow in gh-103179.)shutil.which()will callNeedCurrentDirectoryForExePathWwhen querying for executables on Windows to determine if the current working directory should be prepended to the search path. (Contributed by Charles Machalow in gh-103179.)shutil.which()will return a path matching the cmd with a component fromPATHEXTprior to a direct match elsewhere in the search path on Windows. (Contributed by Charles Machalow in gh-103179.)
sqlite3¶
Add a command-line interface. (Contributed by Erlend E. Aasland in gh-77617.)
Add the
sqlite3.Connection.autocommitattribute tosqlite3.Connectionand the autocommit parameter tosqlite3.connect()to control PEP 249-compliant transaction handling. (Contributed by Erlend E. Aasland in gh-83638.)Add entrypoint keyword-only parameter to
sqlite3.Connection.load_extension(), for overriding the SQLite extension entry point. (Contributed by Erlend E. Aasland in gh-103015.)Add
sqlite3.Connection.getconfig()andsqlite3.Connection.setconfig()tosqlite3.Connectionto make configuration changes to a database connection. (Contributed by Erlend E. Aasland in gh-103489.)
statistics¶
Extend
statistics.correlation()to include as arankedmethod for computing the Spearman correlation of ranked data. (Contributed by Raymond Hettinger in gh-95861.)
sys¶
Add the
sys.monitoringnamespace to expose the new PEP 669 monitoring API. (Contributed by Mark Shannon in gh-103082.)Add
sys.activate_stack_trampoline()andsys.deactivate_stack_trampoline()for activating and deactivating stack profiler trampolines, andsys.is_stack_trampoline_active()for querying if stack profiler trampolines are active. (Contributed by Pablo Galindo and Christian Heimes with contributions from Gregory P. Smith [Google] and Mark Shannon in gh-96123.)Add
sys.last_excwhich holds the last unhandled exception that was raised (for post-mortem debugging use cases). Deprecate the three fields that have the same information in its legacy form:sys.last_type,sys.last_valueandsys.last_traceback. (Contributed by Irit Katriel in gh-102778.)sys._current_exceptions()now returns a mapping from thread-id to an exception instance, rather than to a(typ, exc, tb)tuple. (Contributed by Irit Katriel in gh-103176.)sys.setrecursionlimit()andsys.getrecursionlimit(). The recursion limit now applies only to Python code. Builtin functions do not use the recursion limit, but are protected by a different mechanism that prevents recursion from causing a virtual machine crash.
tempfile¶
The
tempfile.NamedTemporaryFilefunction has a new optional parameter delete_on_close (Contributed by Evgeny Zorin in gh-58451.)tempfile.mkdtemp()now always returns an absolute path, even if the argument provided to the dir parameter is a relative path.
threading¶
Add
threading.settrace_all_threads()andthreading.setprofile_all_threads()that allow to set tracing and profiling functions in all running threads in addition to the calling one. (Contributed by Pablo Galindo in gh-93503.)
tkinter¶
tkinter.Canvas.coords()now flattens its arguments. It now accepts not only coordinates as separate arguments (x1, y1, x2, y2, ...) and a sequence of coordinates ([x1, y1, x2, y2, ...]), but also coordinates grouped in pairs ((x1, y1), (x2, y2), ...and[(x1, y1), (x2, y2), ...]), likecreate_*()methods. (Contributed by Serhiy Storchaka in gh-94473.)
tokenize¶
The
tokenizemodule includes the changes introduced in PEP 701. (Contributed by Marta Gómez Macías and Pablo Galindo in gh-102856.) See Porting to Python 3.12 for more information on the changes to thetokenizemodule.
types¶
Add
types.get_original_bases()to allow for further introspection of User-defined generic types when subclassed. (Contributed by James Hilton-Balfe and Alex Waygood in gh-101827.)
typing¶
isinstance()checks againstruntime-checkable protocolsnow useinspect.getattr_static()rather thanhasattr()to lookup whether attributes exist. This means that descriptors and__getattr__()methods are no longer unexpectedly evaluated duringisinstance()checks against runtime-checkable protocols. However, it may also mean that some objects which used to be considered instances of a runtime-checkable protocol may no longer be considered instances of that protocol on Python 3.12+, and vice versa. Most users are unlikely to be affected by this change. (Contributed by Alex Waygood in gh-102433.)The members of a runtime-checkable protocol are now considered “frozen” at runtime as soon as the class has been created. Monkey-patching attributes onto a runtime-checkable protocol will still work, but will have no impact on
isinstance()checks comparing objects to the protocol. For example:>>> from typing import Protocol, runtime_checkable >>> @runtime_checkable ... class HasX(Protocol): ... x = 1 ... >>> class Foo: ... ... >>> f = Foo() >>> isinstance(f, HasX) False >>> f.x = 1 >>> isinstance(f, HasX) True >>> HasX.y = 2 >>> isinstance(f, HasX) # unchanged, even though HasX now also has a "y" attribute True
This change was made in order to speed up
isinstance()checks against runtime-checkable protocols.The performance profile of
isinstance()checks againstruntime-checkable protocolshas changed significantly. Mostisinstance()checks against protocols with only a few members should be at least 2x faster than in 3.11, and some may be 20x faster or more. However,isinstance()checks against protocols with many members may be slower than in Python 3.11. (Contributed by Alex Waygood in gh-74690 and gh-103193.)All
typing.TypedDictandtyping.NamedTupleclasses now have the__orig_bases__attribute. (Contributed by Adrian Garcia Badaracco in gh-103699.)Add
frozen_defaultparameter totyping.dataclass_transform(). (Contributed by Erik De Bonte in gh-99957.)
unicodedata¶
The Unicode database has been updated to version 15.0.0. (Contributed by Benjamin Peterson in gh-96734).
unittest¶
Add a --durations command line option, showing the N slowest test cases:
python3 -m unittest --durations=3 lib.tests.test_threading
.....
Slowest test durations
----------------------------------------------------------------------
1.210s test_timeout (Lib.test.test_threading.BarrierTests)
1.003s test_default_timeout (Lib.test.test_threading.BarrierTests)
0.518s test_timeout (Lib.test.test_threading.EventTests)
(0.000 durations hidden. Use -v to show these durations.)
----------------------------------------------------------------------
Ran 158 tests in 9.869s
OK (skipped=3)
(Contributed by Giampaolo Rodola in gh-48330)
uuid¶
Add a command-line interface. (Contributed by Adam Chhina in gh-88597.)
Optimizations¶
Remove
wstrandwstr_lengthmembers from Unicode objects. It reduces object size by 8 or 16 bytes on 64bit platform. (PEP 623) (Contributed by Inada Naoki in gh-92536.)Add experimental support for using the BOLT binary optimizer in the build process, which improves performance by 1-5%. (Contributed by Kevin Modzelewski in gh-90536 and tuned by Donghee Na in gh-101525)
Speed up the regular expression substitution (functions
re.sub()andre.subn()and correspondingre.Patternmethods) for replacement strings containing group references by 2–3 times. (Contributed by Serhiy Storchaka in gh-91524.)Speed up
asyncio.Taskcreation by deferring expensive string formatting. (Contributed by Itamar Oren in gh-103793.)The
tokenize.tokenize()andtokenize.generate_tokens()functions are up to 64% faster as a side effect of the changes required to cover PEP 701 in thetokenizemodule. (Contributed by Marta Gómez Macías and Pablo Galindo in gh-102856.)Speed up
super()method calls and attribute loads via the newLOAD_SUPER_ATTRinstruction. (Contributed by Carl Meyer and Vladimir Matveev in gh-103497.)
CPython bytecode changes¶
Remove the
LOAD_METHODinstruction. It has been merged intoLOAD_ATTR.LOAD_ATTRwill now behave like the oldLOAD_METHODinstruction if the low bit of its oparg is set. (Contributed by Ken Jin in gh-93429.)Remove the
JUMP_IF_FALSE_OR_POPandJUMP_IF_TRUE_OR_POPinstructions. (Contributed by Irit Katriel in gh-102859.)Remove the
PRECALLinstruction. (Contributed by Mark Shannon in gh-92925.)Add the
BINARY_SLICEandSTORE_SLICEinstructions. (Contributed by Mark Shannon in gh-94163.)Add the
CALL_INTRINSIC_1instructions. (Contributed by Mark Shannon in gh-99005.)Add the
CALL_INTRINSIC_2instruction. (Contributed by Irit Katriel in gh-101799.)Add the
CLEANUP_THROWinstruction. (Contributed by Brandt Bucher in gh-90997.)Add the
END_SENDinstruction. (Contributed by Mark Shannon in gh-103082.)Add the
LOAD_FAST_AND_CLEARinstruction as part of the implementation of PEP 709. (Contributed by Carl Meyer in gh-101441.)Add the
LOAD_FAST_CHECKinstruction. (Contributed by Dennis Sweeney in gh-93143.)Add the
LOAD_FROM_DICT_OR_DEREF,LOAD_FROM_DICT_OR_GLOBALS, andLOAD_LOCALSopcodes as part of the implementation of PEP 695. Remove theLOAD_CLASSDEREFopcode, which can be replaced withLOAD_LOCALSplusLOAD_FROM_DICT_OR_DEREF. (Contributed by Jelle Zijlstra in gh-103764.)Add the
LOAD_SUPER_ATTRinstruction. (Contributed by Carl Meyer and Vladimir Matveev in gh-103497.)Add the
RETURN_CONSTinstruction. (Contributed by Wenyang Wang in gh-101632.)
Demos and Tools¶
Remove the
Tools/demo/directory which contained old demo scripts. A copy can be found in the old-demos project. (Contributed by Victor Stinner in gh-97681.)Remove outdated example scripts of the
Tools/scripts/directory. A copy can be found in the old-demos project. (Contributed by Victor Stinner in gh-97669.)
Deprecated¶
argparse: The type, choices, and metavar parameters ofargparse.BooleanOptionalActionare deprecated and will be removed in 3.14. (Contributed by Nikita Sobolev in gh-92248.)ast: The followingastfeatures have been deprecated in documentation since Python 3.8, now cause aDeprecationWarningto be emitted at runtime when they are accessed or used, and will be removed in Python 3.14:ast.Numast.Strast.Bytesast.NameConstantast.Ellipsis
Use
ast.Constantinstead. (Contributed by Serhiy Storchaka in gh-90953.)-
The child watcher classes
asyncio.MultiLoopChildWatcher,asyncio.FastChildWatcher,asyncio.AbstractChildWatcherandasyncio.SafeChildWatcherare deprecated and will be removed in Python 3.14. (Contributed by Kumar Aditya in gh-94597.)asyncio.set_child_watcher(),asyncio.get_child_watcher(),asyncio.AbstractEventLoopPolicy.set_child_watcher()andasyncio.AbstractEventLoopPolicy.get_child_watcher()are deprecated and will be removed in Python 3.14. (Contributed by Kumar Aditya in gh-94597.)The
get_event_loop()method of the default event loop policy now emits aDeprecationWarningif there is no current event loop set and it decides to create one. (Contributed by Serhiy Storchaka and Guido van Rossum in gh-100160.)
calendar:calendar.Januaryandcalendar.Februaryconstants are deprecated and replaced bycalendar.JANUARYandcalendar.FEBRUARY. (Contributed by Prince Roshan in gh-103636.)collections.abc: Deprecatedcollections.abc.ByteString.Use
isinstance(obj, collections.abc.Buffer)to test ifobjimplements the buffer protocol at runtime. For use in type annotations, either useBufferor a union that explicitly specifies the types your code supports (e.g.,bytes | bytearray | memoryview).ByteStringwas originally intended to be an abstract class that would serve as a supertype of bothbytesandbytearray. However, since the ABC never had any methods, knowing that an object was an instance ofByteStringnever actually told you anything useful about the object. Other common buffer types such asmemoryviewwere also never understood as subtypes ofByteString(either at runtime or by static type checkers).See PEP 688 for more details. (Contributed by Shantanu Jain in gh-91896.)
datetime:datetime.datetime’sutcnow()andutcfromtimestamp()are deprecated and will be removed in a future version. Instead, use timezone-aware objects to represent datetimes in UTC: respectively, callnow()andfromtimestamp()with the tz parameter set todatetime.UTC. (Contributed by Paul Ganssle in gh-103857.)email: Deprecate the isdst parameter inemail.utils.localtime(). (Contributed by Alan Williams in gh-72346.)importlib.abc: Deprecated the following classes, scheduled for removal in Python 3.14:importlib.abc.ResourceReaderimportlib.abc.Traversableimportlib.abc.TraversableResources
Use
importlib.resources.abcclasses instead:(Contributed by Jason R. Coombs and Hugo van Kemenade in gh-93963.)
itertools: Deprecate the support for copy, deepcopy, and pickle operations, which is undocumented, inefficient, historically buggy, and inconsistent. This will be removed in 3.14 for a significant reduction in code volume and maintenance burden. (Contributed by Raymond Hettinger in gh-101588.)multiprocessing: In Python 3.14, the defaultmultiprocessingstart method will change to a safer one on Linux, BSDs, and other non-macOS POSIX platforms where'fork'is currently the default (gh-84559). Adding a runtime warning about this was deemed too disruptive as the majority of code is not expected to care. Use theget_context()orset_start_method()APIs to explicitly specify when your code requires'fork'. See contexts and start methods.pkgutil:pkgutil.find_loader()andpkgutil.get_loader()are deprecated and will be removed in Python 3.14; useimportlib.util.find_spec()instead. (Contributed by Nikita Sobolev in gh-97850.)pty: The module has two undocumentedmaster_open()andslave_open()functions that have been deprecated since Python 2 but only gained a properDeprecationWarningin 3.12. Remove them in 3.14. (Contributed by Soumendra Ganguly and Gregory P. Smith in gh-85984.)os:The
st_ctimefields return byos.stat()andos.lstat()on Windows are deprecated. In a future release, they will contain the last metadata change time, consistent with other platforms. For now, they still contain the creation time, which is also available in the newst_birthtimefield. (Contributed by Steve Dower in gh-99726.)On POSIX platforms,
os.fork()can now raise aDeprecationWarningwhen it can detect being called from a multithreaded process. There has always been a fundamental incompatibility with the POSIX platform when doing so. Even if such code appeared to work. We added the warning to raise awareness as issues encountered by code doing this are becoming more frequent. See theos.fork()documentation for more details along with this discussion on fork being incompatible with threads for why we’re now surfacing this longstanding platform compatibility problem to developers.
When this warning appears due to usage of
multiprocessingorconcurrent.futuresthe fix is to use a differentmultiprocessingstart method such as"spawn"or"forkserver".shutil: The onerror argument ofshutil.rmtree()is deprecated; use onexc instead. (Contributed by Irit Katriel in gh-102828.)-
default adapters and converters are now deprecated. Instead, use the Adapter and converter recipes and tailor them to your needs. (Contributed by Erlend E. Aasland in gh-90016.)
In
execute(),DeprecationWarningis now emitted when named placeholders are used together with parameters supplied as a sequence instead of as adict. Starting from Python 3.14, using named placeholders with parameters supplied as a sequence will raise aProgrammingError. (Contributed by Erlend E. Aasland in gh-101698.)
sys: Thesys.last_type,sys.last_valueandsys.last_tracebackfields are deprecated. Usesys.last_excinstead. (Contributed by Irit Katriel in gh-102778.)tarfile: Extracting tar archives without specifying filter is deprecated until Python 3.14, when'data'filter will become the default. See Extraction filters for details.-
typing.Hashableandtyping.Sized, aliases forcollections.abc.Hashableandcollections.abc.Sizedrespectively, are deprecated. (gh-94309.)typing.ByteString, deprecated since Python 3.9, now causes aDeprecationWarningto be emitted when it is used. (Contributed by Alex Waygood in gh-91896.)
xml.etree.ElementTree: The module now emitsDeprecationWarningwhen testing the truth value of anxml.etree.ElementTree.Element. Before, the Python implementation emittedFutureWarning, and the C implementation emitted nothing. (Contributed by Jacob Walls in gh-83122.)The 3-arg signatures (type, value, traceback) of
coroutine throw(),generator throw()andasync generator throw()are deprecated and may be removed in a future version of Python. Use the single-arg versions of these functions instead. (Contributed by Ofey Chan in gh-89874.)DeprecationWarningis now raised when__package__on a module differs from__spec__.parent(previously it wasImportWarning). (Contributed by Brett Cannon in gh-65961.)Setting
__package__or__cached__on a module is deprecated, and will cease to be set or taken into consideration by the import system in Python 3.14. (Contributed by Brett Cannon in gh-65961.)The bitwise inversion operator (
~) on bool is deprecated. It will throw an error in Python 3.16. Usenotfor logical negation of bools instead. In the rare case that you really need the bitwise inversion of the underlyingint, convert to int explicitly:~int(x). (Contributed by Tim Hoffmann in gh-103487.)Accessing
co_lnotabon code objects was deprecated in Python 3.10 via PEP 626, but it only got a properDeprecationWarningin 3.12. May be removed in 3.15. (Contributed by Nikita Sobolev in gh-101866.)
Pending removal in Python 3.13¶
Modules (see PEP 594):
aifcaudioopcgicgitbchunkcryptimghdrmailcapmsilibnisnntplibossaudiodevpipessndhdrspwdsunautelnetlibuuxdrlib
Other modules:
lib2to3, and the 2to3 program (gh-84540)
APIs:
configparser.LegacyInterpolation(gh-90765)locale.resetlocale()(gh-90817)turtle.RawTurtle.settiltangle()(gh-50096)unittest.findTestCases()(gh-50096)unittest.getTestCaseNames()(gh-50096)unittest.makeSuite()(gh-50096)unittest.TestProgram.usageExit()(gh-67048)webbrowser.MacOSX(gh-86421)classmethoddescriptor chaining (gh-89519)
Pending removal in Python 3.14¶
argparse: The type, choices, and metavar parameters ofargparse.BooleanOptionalActionare deprecated and will be removed in 3.14. (Contributed by Nikita Sobolev in gh-92248.)ast: The following features have been deprecated in documentation since Python 3.8, now cause aDeprecationWarningto be emitted at runtime when they are accessed or used, and will be removed in Python 3.14:ast.Numast.Strast.Bytesast.NameConstantast.Ellipsis
Use
ast.Constantinstead. (Contributed by Serhiy Storchaka in gh-90953.)-
The child watcher classes
asyncio.MultiLoopChildWatcher,asyncio.FastChildWatcher,asyncio.AbstractChildWatcherandasyncio.SafeChildWatcherare deprecated and will be removed in Python 3.14. (Contributed by Kumar Aditya in gh-94597.)asyncio.set_child_watcher(),asyncio.get_child_watcher(),asyncio.AbstractEventLoopPolicy.set_child_watcher()andasyncio.AbstractEventLoopPolicy.get_child_watcher()are deprecated and will be removed in Python 3.14. (Contributed by Kumar Aditya in gh-94597.)The
get_event_loop()method of the default event loop policy now emits aDeprecationWarningif there is no current event loop set and it decides to create one. (Contributed by Serhiy Storchaka and Guido van Rossum in gh-100160.)
email: Deprecated the isdst parameter inemail.utils.localtime(). (Contributed by Alan Williams in gh-72346.)importlib.abcdeprecated classes:importlib.abc.ResourceReaderimportlib.abc.Traversableimportlib.abc.TraversableResources
Use
importlib.resources.abcclasses instead:(Contributed by Jason R. Coombs and Hugo van Kemenade in gh-93963.)
itertoolshad undocumented, inefficient, historically buggy, and inconsistent support for copy, deepcopy, and pickle operations. This will be removed in 3.14 for a significant reduction in code volume and maintenance burden. (Contributed by Raymond Hettinger in gh-101588.)multiprocessing: The default start method will change to a safer one on Linux, BSDs, and other non-macOS POSIX platforms where'fork'is currently the default (gh-84559). Adding a runtime warning about this was deemed too disruptive as the majority of code is not expected to care. Use theget_context()orset_start_method()APIs to explicitly specify when your code requires'fork'. See Contexts and start methods.pathlib:is_relative_to()andrelative_to(): passing additional arguments is deprecated.pkgutil:pkgutil.find_loader()andpkgutil.get_loader()now raiseDeprecationWarning; useimportlib.util.find_spec()instead. (Contributed by Nikita Sobolev in gh-97850.)pty:master_open(): usepty.openpty().slave_open(): usepty.openpty().
-
versionandversion_info.execute()andexecutemany()if named placeholders are used and parameters is a sequence instead of adict.
urllib:urllib.parse.Quoteris deprecated: it was not intended to be a public API. (Contributed by Gregory P. Smith in gh-88168.)
Pending removal in Python 3.15¶
The import system:
Setting
__cached__on a module while failing to set