What’s new in Python 3.16¶
- Editor:
TBD
This article explains the new features in Python 3.16, compared to 3.15.
For full details, see the changelog.
Note
Prerelease users should be aware that this document is currently in draft form. It will be updated substantially as Python 3.16 moves towards release, so it’s worth checking back even after reading earlier versions.
Summary — release highlights¶
New features¶
Other language changes¶
New modules¶
None yet.
Improved modules¶
gzip¶
gzip.open()now accepts an optional argumentmtimewhich is passed on to the constructor of theGzipFileclass. (Contributed by Marin Misur in gh-91372.)
lzma¶
Add support of new BCJ filters ARM64 and RISC-V via
lzma.FILTER_ARM64andlzma.FILTER_RISCV. Note that the new filters will work only if runtime library supports them. ARM64 filter requireslzma5.4.0 or newer while RISC-V requires 5.6.0 or newer. (Contributed by Chien Wong in gh-115988.)
os¶
Add
os.pidfd_getfd()for duplicating a file descriptor from another process via a pidfd. Available on Linux 5.6+. (Contributed by Maurycy Pawłowski-Wieroński in gh-149464.)
xml¶
Add support for multiple multi-byte encodings in the
XML parser: “cp932”, “cp949”, “cp950”, “Big5”,”EUC-JP”, “GB2312”, “GBK”, “johab”, and “Shift_JIS”. Add partial support (only BMP characters) for multi-byte encodings “Big5-HKSCS”, “EUC_JIS-2004”, “EUC_JISX0213”, “Shift_JIS-2004”, “Shift_JISX0213”, “utf-8-sig” and non-standard aliases like “UTF8” (without hyphen). The parser now raisesValueErrorfor known unsupported multi-byte encodings such us “ISO-2022-JP” or “raw-unicode-escape” instead of failing later, when encounter non-ASCII data. (Contributed by Serhiy Storchaka in gh-62259.)
Optimizations¶
module_name¶
TODO
Removed¶
annotationlib¶
The
annotationlib.ForwardRef._evaluate()method which has been deprecated since Python 3.14. Useannotationlib.ForwardRef.evaluate()ortyping.evaluate_forward_ref()instead.
asyncio¶
The
asyncio.iscoroutinefunction()which has been deprecated since Python 3.14. Useinspect.iscoroutinefunction()instead.
functools¶
Calling the Python implementation of
functools.reduce()with function or sequence as keyword arguments has been deprecated since Python 3.14.
logging¶
Support for custom logging handlers with the strm argument is deprecated and scheduled for removal in Python 3.16. Define handlers with the stream argument instead.
mimetypes¶
Valid extensions start with a ‘.’ or are empty for
mimetypes.MimeTypes.add_type(). Undotted extensions now raise aValueError.
shutil¶
The
ExecErrorexception which has been deprecated since Python 3.14. It has not been used by any function inshutilsince Python 3.4. (Contributed by Stan Ulbrych in gh-149567.)
symtable¶
The
symtable.Class.get_methods()method which has been deprecated since Python 3.14.
sys¶
The
_enablelegacywindowsfsencoding()function which has been deprecated since Python 3.13. Use thePYTHONLEGACYWINDOWSFSENCODINGenvironment variable instead. (Contributed by Stan Ulbrych in gh-149595.)
sysconfig¶
The
sysconfig.expand_makefile_vars()function which has been deprecated since Python 3.14. Use thevarsargument ofsysconfig.get_paths()instead. (Contributed by Stan Ulbrych in gh-149499.)
tarfile¶
The undocumented and unused
tarfile.TarFile.tarfileattribute has been deprecated since Python 3.13.
xml.etree.ElementTree¶
Add the validate option to functions
tostring(),tostringlist(), and theElement.writemethod, which allows to validate the element or element tree before serialization. (Contributed by Serhiy Storchaka in gh-149468.)
Deprecated¶
-
Soft-deprecated since Python 3.3
abc.abstractclassmethod,abc.abstractstaticmethod, andabc.abstractpropertynow raise aDeprecationWarning. These classes will be removed in Python 3.21, instead useabc.abstractmethod()withclassmethod(),staticmethod(), andpropertyrespectively.
ast:Classes
slice,Index,ExtSlice,Suite,Param,AugLoadandAugStore, deprecated since Python 3.9, are no longer imported byfrom ast import *and issue a deprecation warning on use. The classes are slated for removal in Python 3.21. These types are not generated by the parser or accepted by the code generator.The
dimsproperty ofast.Tupleobjects, deprecated since Python 3.9, now issues a deprecation warning on use. This property is slated for removal in 3.21. Useast.Tuple.eltsinstead.
Pending removal in Python 3.17¶
-
strptime()calls using a format string containing%e(day of month) without a year. This has been deprecated since Python 3.15. (Contributed by Stan Ulbrych in gh-70647.)
-
collections.abc.ByteStringis scheduled for removal in Python 3.17.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.)
-
Passing non-ascii encoding names to
encodings.normalize_encoding()is deprecated and scheduled for removal in Python 3.17. (Contributed by Stan Ulbrych in gh-136702.)
-
webbrowser.MacOSXOSAScriptis deprecated in favour ofwebbrowser.MacOS. (gh-137586)
-
Before Python 3.14, old-style unions were implemented using the private class
typing._UnionGenericAlias. This class is no longer needed for the implementation, but it has been retained for backward compatibility, with removal scheduled for Python 3.17. Users should use documented introspection helpers liketyping.get_origin()andtyping.get_args()instead of relying on private implementation details.typing.ByteString, deprecated since Python 3.9, is scheduled for removal in Python 3.17.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.)
Pending removal in Python 3.19¶
-
In hash function constructors such as
new()or the direct hash-named constructors such asmd5()andsha256(), their optional initial data parameter could also be passed a keyword argument nameddata=orstring=in varioushashlibimplementations.Support for the
stringkeyword argument name is now deprecated and slated for removal in Python 3.19.Before Python 3.13, the
stringkeyword parameter was not correctly supported depending on the backend implementation of hash functions. Prefer passing the initial data as a positional argument for maximum backwards compatibility.
-
http.cookies.Morsel.js_output()is deprecated and will be removed in Python 3.19.http.cookies.BaseCookie.js_output()is deprecated and will be removed in Python 3.19.
-
Altering
IMAP4.fileis now deprecated and slated for removal in Python 3.19. This property is now unused and changing its value does not automatically close the current file.Before Python 3.14, this property was used to implement the corresponding
read()andreadline()methods forIMAP4but this is no longer the case since then.
Pending removal in Python 3.20¶
Calling the
__new__()method ofstruct.Structwithout the format argument is deprecated and will be removed in Python 3.20. Calling__init__()method on initializedStructobjects is deprecated and will be removed in Python 3.20.(Contributed by Sergey B Kirpichev and Serhiy Storchaka in gh-143715.)
The
__version__,versionandVERSIONattributes have been deprecated in these standard library modules and will be removed in Python 3.20. Usesys.version_infoinstead.ctypes.macholibdecimal(usedecimal.SPEC_VERSIONinstead)logging(__date__also deprecated)xml.sax.expatreader
(Contributed by Hugo van Kemenade and Stan Ulbrych in gh-76007.)
Deprecations defined by PEP 829:
Warnings are produced for
importlines found inname.pthfiles.name.pthfiles are no longer decoded in the locale encoding by default. They MUST be encoded inutf-8-sig.
(Contributed by Barry Warsaw in gh-148641.)
ast:Creating instances of abstract AST nodes (such as
ast.ASTorast.expr) is deprecated and will raise an error in Python 3.20.
Pending removal in future versions¶
The following APIs will be removed in the future, although there is currently no date scheduled for their removal.
-
Nesting argument groups and nesting mutually exclusive groups are deprecated.
Passing the undocumented keyword argument prefix_chars to
add_argument_group()is now deprecated.The
argparse.FileTypetype converter is deprecated.
-
Generators:
throw(type, exc, tb)andathrow(type, exc, tb)signature is deprecated: usethrow(exc)andathrow(exc)instead, the single argument signature.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]). A syntax warning is raised if the numeric literal is immediately followed by one of keywordsand,else,for,if,in,isandor. In a future release it will be changed to a syntax error. (gh-87999)Support for
__index__()and__int__()method returning non-int type: these methods will be required to return an instance of a strict subclass ofint.Support for
__float__()method returning a strict subclass offloat: these methods will be required to return an instance offloat.Support for
__complex__()method returning a strict subclass ofcomplex: these methods will be required to return an instance ofcomplex.Passing a complex number as the real or imag argument in the
complex()constructor is now deprecated; it should only be passed as a single positional argument. (Contributed by Serhiy Storchaka in gh-109218.)
calendar:calendar.Januaryandcalendar.Februaryconstants are deprecated and replaced bycalendar.JANUARYandcalendar.FEBRUARY. (Contributed by Prince Roshan in gh-103636.)codecs: useopen()instead ofcodecs.open(). (gh-133038)codeobject.co_lnotab: use thecodeobject.co_lines()method instead.-
utcnow(): usedatetime.datetime.now(tz=datetime.UTC).utcfromtimestamp(): usedatetime.datetime.fromtimestamp(timestamp, tz=datetime.UTC).
gettext: Plural value must be an integer.-
cache_from_source()debug_override parameter is deprecated: use the optimization parameter instead.
-
EntryPointstuple interface.Implicit
Noneon return values.
logging: thewarn()method has been deprecated since Python 3.3, usewarning()instead.mailbox: Use of StringIO input and text mode is deprecated, use BytesIO and binary mode instead.os: Callingos.register_at_fork()in a multi-threaded process.os.path:os.path.commonprefix()is deprecated, useos.path.commonpath()for path prefixes. Theos.path.commonprefix()function is being deprecated due to having a misleading name and module. The function is not safe to use for path prefixes despite being included in a module about path manipulation, meaning it is easy to accidentally introduce path traversal vulnerabilities into Python programs by using this function.pydoc.ErrorDuringImport: A tuple value for exc_info parameter is deprecated, use an exception instance.re: More strict rules are now applied for numerical group references and group names in regular expressions. Only sequence of ASCII digits is now accepted as a numerical reference. The group name in bytes patterns and replacement strings can now only contain ASCII letters and digits and underscore. (Contributed by Serhiy Storchaka in gh-91760.)shutil:rmtree()’s onerror parameter is deprecated in Python 3.12; use the onexc parameter instead.ssloptions and protocols:ssl.SSLContextwithout protocol argument is deprecated.ssl.SSLContext:set_npn_protocols()andselected_npn_protocol()are deprecated: use ALPN instead.ssl.OP_NO_SSL*optionsssl.OP_NO_TLS*optionsssl.PROTOCOL_SSLv3ssl.PROTOCOL_TLSssl.PROTOCOL_TLSv1ssl.PROTOCOL_TLSv1_1ssl.PROTOCOL_TLSv1_2ssl.TLSVersion.SSLv3ssl.TLSVersion.TLSv1ssl.TLSVersion.TLSv1_1
threadingmethods:threading.Condition.notifyAll(): usenotify_all().threading.Event.isSet(): useis_set().threading.Thread.isDaemon(),threading.Thread.setDaemon(): usethreading.Thread.daemonattribute.threading.Thread.getName(),threading.Thread.setName(): usethreading.Thread.nameattribute.threading.currentThread(): usethreading.current_thread().threading.activeCount(): usethreading.active_count().
The internal class
typing._UnionGenericAliasis no longer used to implementtyping.Union. To preserve compatibility with users using this private class, a compatibility shim will be provided until at least Python 3.17. (Contributed by Jelle Zijlstra in gh-105499.)unittest.IsolatedAsyncioTestCase: it is deprecated to return a value that is notNonefrom a test case.urllib.parsedeprecated functions:urlparse()insteadsplitattr()splithost()splitnport()splitpasswd()splitport()splitquery()splittag()splittype()splituser()splitvalue()to_bytes()
wsgiref:SimpleHandler.stdout.write()should not do partial writes.xml.etree.ElementTree: Testing the truth value of anElementis deprecated. In a future release it will always returnTrue. Prefer explicitlen(elem)orelem is not Nonetests instead.sys._clear_type_cache()is deprecated: usesys._clear_internal_caches()instead.
Porting to Python 3.16¶
This section lists previously described changes and other bugfixes that may require changes to your code.
Build changes¶
Remove the bundled copy of the libmpdec decimal library from the CPython source tree to simplify maintenence and updates. The
decimalmodule will now unconditionally use the system’s libmpdec decimal library. Also remove the now unused--with-system-libmpdecconfigure flag. This change has no impact on binary releases of Python, which have been built against a separate copy of libmpdec for the past several releases.(Contributed by Sergey B Kirpichev in gh-115119.)
Add a
--with-build-details-suffixconfigure flag to allow Linux distributions that co-install multiple versions of Python in the same tree to avoidbuild-details.jsonclashes.(Contributed by Stefano Rivera in gh-131372.)
C API changes¶
New features¶
TODO
Porting to Python 3.16¶
TODO
Deprecated C APIs¶
TODO