@@ -659,10 +659,10 @@ are added to enable the warning.
659659See :ref:`io- text- encoding` for more information.
660660
661661
662- New Features Related to Type Annotations
663- ========================================
662+ New Features Related to Type Hints
663+ ==================================
664664
665- This section covers major changes affecting :pep:`484 ` type annotations and
665+ This section covers major changes affecting :pep:`484 ` type hints and
666666the :mod:`typing` module.
667667
668668
@@ -671,7 +671,7 @@ PEP 604: New Type Union Operator
671671
672672A new type union operator was introduced which enables the syntax `` X | Y`` .
673673This provides a cleaner way of expressing ' either type X or type Y' instead of
674- using :data:`typing.Union` , especially in type hints (annotations) .
674+ using :data:`typing.Union` , especially in type hints.
675675
676676In previous versions of Python, to apply a type hint for functions accepting
677677arguments of multiple types, :data:`typing.Union` was used::
@@ -722,8 +722,8 @@ See :class:`typing.Callable`, :class:`typing.ParamSpec`,
722722Zijlstra in :issue:`43783 ` . PEP written by Mark Mendoza.)
723723
724724
725- PEP 613 : TypeAlias Annotation
726- ---------------------------- -
725+ PEP 613 : TypeAlias
726+ ------------------
727727
728728:pep:`484 ` introduced the concept of type aliases, only requiring them to be
729729top- level unannotated assignments. This simplicity sometimes made it difficult
@@ -733,8 +733,8 @@ especially when forward references or invalid types were involved. Compare::
733733 StrCache = ' Cache[str]' # a type alias
734734 LOG_PREFIX = ' LOG[DEBUG]' # a module constant
735735
736- Now the :mod:`typing` module has a special annotation :data:`TypeAlias` to
737- declare type aliases more explicitly::
736+ Now the :mod:`typing` module has a special value :data:`TypeAlias`
737+ which lets you declare type aliases more explicitly::
738738
739739 StrCache: TypeAlias = ' Cache[str]' # a type alias
740740 LOG_PREFIX = ' LOG[DEBUG]' # a module constant
@@ -805,6 +805,10 @@ Other Language Changes
805805 defined by :pep:`526 ` ) no longer cause any runtime effects with `` from __future__ import annotations`` .
806806 (Contributed by Batuhan Taskaya in :issue:`42737 ` .)
807807
808+ * Class and module objects now lazy- create empty annotations dicts on demand.
809+ The annotations dicts are stored in the object ’s `` __dict__ `` for
810+ backwards compatibility.
811+ (Contributed by Larry Hastings in :issue:`43901 ` .)
808812
809813New Modules
810814========== =
@@ -987,10 +991,18 @@ inspect
987991When a module does not define `` __loader__`` , fall back to `` __spec__ .loader`` .
988992(Contributed by Brett Cannon in :issue:`42133 ` .)
989993
990- Added * globalns* and * localns* parameters in :func:`~ inspect.signature` and
991- :meth:`inspect.Signature.from_callable` to retrieve the annotations in given
992- local and global namespaces.
993- (Contributed by Batuhan Taskaya in :issue:`41960 ` .)
994+ Added :func:`inspect.get_annotations` , which safely computes the annotations
995+ defined on an object . It works around the quirks of accessing the annotations
996+ on various types of objects, and makes very few assumptions about the object
997+ it examines. :func:`inspect.get_annotations` can also correctly un- stringize
998+ stringized annotations. :func:`inspect.get_annotations` is now considered
999+ best practice for accessing the annotations dict defined on any Python object .
1000+ Relatedly, :func:`inspect.signature` ,
1001+ :func:`inspect.Signature.from_callable` , and `` inspect.Signature.from_function``
1002+ now call :func:`inspect.get_annotations` to retrieve annotations. This means
1003+ :func:`inspect.signature` and :func:`inspect.Signature.from_callable` can
1004+ also now un- stringize stringized annotations.
1005+ (Contributed by Larry Hastings in :issue:`43817 ` .)
9941006
9951007linecache
9961008-------- -
@@ -1155,7 +1167,7 @@ of types readily interpretable by type checkers.
11551167typing
11561168------
11571169
1158- For major changes, see `New Features Related to Type Annotations ` _.
1170+ For major changes, see `New Features Related to Type Hints ` _.
11591171
11601172The behavior of :class :`typing.Literal` was changed to conform with :pep:`586 `
11611173and to match the behavior of static type checkers specified in the PEP .
@@ -1255,11 +1267,12 @@ Optimizations
12551267 faster, lzma decompression 1. 20x ~ 1. 32x faster, `` GzipFile.read(- 1 )`` 1. 11x
12561268 ~ 1. 18x faster. (Contributed by Ma Lin, reviewed by Gregory P. Smith, in :issue:`41486 ` )
12571269
1258- * Function parameters and their annotations are no longer computed at runtime,
1259- but rather at compilation time. They are stored as a tuple of strings at the
1260- bytecode level. It is now around 2 times faster to create a function with
1261- parameter annotations. (Contributed by Yurii Karabas and Inada Naoki
1262- in :issue:`42202 ` )
1270+ * When using stringized annotations, annotations dicts for functions are no longer
1271+ created when the function is created. Instead, they' re stored as a tuple of
1272+ strings, and the function object lazily converts this into the annotations dict
1273+ on demand. This optimization cuts the CPU time needed to define an annotated
1274+ function by half.
1275+ (Contributed by Yurii Karabas and Inada Naoki in :issue:`42202 ` )
12631276
12641277* Substring search functions such as `` str1 in str2`` and `` str2.find(str1)``
12651278 now sometimes use Crochemore & Perrin' s "Two-Way" string searching
@@ -1569,8 +1582,8 @@ Changes in the Python API
15691582CPython bytecode changes
15701583========================
15711584
1572- * The `` MAKE_FUNCTION `` instruction accepts tuple of strings as annotations
1573- instead of dictionary .
1585+ * The `` MAKE_FUNCTION `` instruction now accepts either a dict or a tuple of
1586+ strings as the function ' s annotations .
15741587 (Contributed by Yurii Karabas and Inada Naoki in :issue:`42202 ` )
15751588
15761589Build Changes
0 commit comments