Skip to content

Python changes since 3.8 (summary)

Jerry Morrison edited this page Sep 30, 2022 · 5 revisions

Python 3.8.10

Supports macOS 11.0 (Big Sur) and Apple Silicon.

Python 3.8.14

A security improvement in int <-> str conversions.

Python 3.9 to 3.9.14

  • dict union operator d1 | d2, d1 |= d2.
  • Type hinting allowed standard collection types, e.g. list[str].
  • Any valid expression can now be used as a decorator.
  • str.removeprefix(prefix) and str.removesuffix(suffix) methods.
  • PEP 593, Flexible function and variable annotations. typing.Annotated type to decorate existing types with context-specific metadata.
  • Add os.pidfd_open() which allows process management without races and signals.
  • CPython now uses a PEG parser.
  • The IANA Time Zone Database is now in the standard library in the zoneinfo module.
  • New zoneinfo (time zone) module and graphlib module (topo-sort).
  • math.gcd() now handles multiple arguments.
  • Added random.Random.randbytes().

Speedups

  • builtins (range, tuple, set, frozenset, list, dict, ...) use vectorcall;
  • modules (_abc, audioop, _bz2, _codecs, _contextvars, _crypt, _functools, _json, _locale, math, operator, resource, time, _weakref) use multiphase initialization;
  • fast access to module state from methods of C extension types;
  • GC doesn't block on resurrected objects.

Compatibility issues

  • => Use the -W default or -W error CLI option to check for DeprecationWarning in your code since various Python 2 backward compatibility details are being removed after Python 3.9. (Warnings Filter can be used to ignore warnings from third-party code.)
  • => The collections.* aliases to collections.abc.* will be removed in Python 3.10. wcEcoli uses collections.Mapping and collections.Sequence.
  • __import__() now raises ImportError instead of ValueError.
  • => The __file__ attribute of the __main__ module is now an absolute path.
  • => "".replace("", s, n) returns s instead of an empty string for all non-zero n, consistent with "".replace("", s). Ditto for bytes and bytearray objects.
  • => Unexpected errors in calling the __iter__ method are no longer masked by TypeError in the in operator and functions contains(), indexOf(), and countOf() of the operator module.

Deprecations:

  • After Python 3.9, the random module will restrict its seeds to None, int, float, str, bytes, and bytearray values.

Python 3.10.7

  • Structural Pattern Matching (match/case statements).
  • Parenthesized context managers are now officially allowed.
  • Better error messages.
  • zip() now has an optional strict flag to require that all the iterables have an equal length. Precise line numbers for debugging and other tools.
  • PEP 604, Allow writing union types as X | Y in type hints, isinstance(), and issubclass().
  • PEP 613, Explicit Type Aliases: MyType: TypeAlias = "ClassName"; y: TypeAlias = ClassName.
  • PEP 612, Parameter Specification Variables: typing.ParamSpec and typing.Concatenate.
  • PEP 647, User-Defined Type Guards.
  • Add int.bit_count() method (the number of 1 bits).
  • pprint.pprint() now accepts a new underscore_numbers keyword argument and it can pretty-print dataclasses.dataclass instances.
  • Add statistics.covariance(), Pearson’s .correlation(), and simple .linear_regression() functions.

Deprecations

  • => The 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, shutil, subprocess or sysconfig).
  • Any numeric literal immediately followed by one of keywords and, else, for, if, in, is, and or since these can be ambiguous, e.g. 0x1for.

Speedups

  • Constructors str(), bytes(), and bytearray() are faster.
  • Python starts faster.
  • Byte code speedups.
  • Faster bz2 / lzma / zlib (de)compressors.
  • When using stringized type annotations, code loads faster.
  • Faster str1 in str2 and str2.find(str1).
  • Faster map(), filter(), reversed(), bool(), and float() via vectorcall.

Python 3.11

  • PEP 654: Exception Groups, except*.
  • PEP 678: Enriching Exceptions with Notes.
  • PEP 680: Add tomllib library to parse TOML files.
  • PEP 657: Fine grained error locations in tracebacks.
  • New -P command line option and PYTHONSAFEPATH environment variable to disable automatically prepending a potentially unsafe working dir or script directory to sys.path.
  • PEP 646: Variadic generic type hints, e.g. NumPy arrays parameterized with the array shape.
  • PEP 655: Type hints can declare individual TypedDict fields as required or optional.
  • PEP 673: Self type, a simple way to annotate a method as returning an instance of its class.
  • PEP 675: LiteralString type hint.
  • PEP 681: @typing.dataclass_transform() to decorate an annotation which transforms a class, like @dataclass() does.
  • __future__.annotations is in limbo.
  • Add object.__getstate__().
  • The format specifier z formats -0 (float or decimal) as 0, e.g. f'{-.00001:z.2f}'.
  • Add StrEnum (the enum members must be strings), ReprEnum, @member, @global_enum, and other enum features.
  • Add regex atomic grouping (?>...) and possessive quantifiers (*+, ++, ?+, {m,n}+).
  • Add typing.assert_never() and typing.Never, which are useful for asking a type checker to confirm that a line of code is unreachable.
  • Add typing.reveal_type(), which is useful for asking a type checker what type it inferred for an expression.
  • Add typing.assert_type(), which is useful for asking a type checker to confirm that the type inferred the expected type.
  • Add TypedDict and NamedTuple classes can inherit from Generic[T].
  • Add @typing.final which marks a method or class as final, i.e. can't be overridden.

Speedups

Deprecations

  • PEP 594: Removing modules from the standard library.
  • PEP 624: Remove Py_UNICODE encoder APIs.
  • PEP 670: Convert macros to functions in the Python C API.
  • PEP 594: The chunk module is deprecated and slated to be removed in version 3.13. tablereader.py uses it.
  • typing.Text (for Python 2/3 compatibility).