@@ -659,10 +659,10 @@ are added to enable the warning.
659
659
See :ref:`io- text- encoding` for more information.
660
660
661
661
662
- New Features Related to Type Annotations
663
- ========================================
662
+ New Features Related to Type Hints
663
+ ==================================
664
664
665
- This section covers major changes affecting :pep:`484 ` type annotations and
665
+ This section covers major changes affecting :pep:`484 ` type hints and
666
666
the :mod:`typing` module.
667
667
668
668
@@ -671,7 +671,7 @@ PEP 604: New Type Union Operator
671
671
672
672
A new type union operator was introduced which enables the syntax `` X | Y`` .
673
673
This 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.
675
675
676
676
In previous versions of Python, to apply a type hint for functions accepting
677
677
arguments of multiple types, :data:`typing.Union` was used::
@@ -722,8 +722,8 @@ See :class:`typing.Callable`, :class:`typing.ParamSpec`,
722
722
Zijlstra in :issue:`43783 ` . PEP written by Mark Mendoza.)
723
723
724
724
725
- PEP 613 : TypeAlias Annotation
726
- ---------------------------- -
725
+ PEP 613 : TypeAlias
726
+ ------------------
727
727
728
728
:pep:`484 ` introduced the concept of type aliases, only requiring them to be
729
729
top- level unannotated assignments. This simplicity sometimes made it difficult
@@ -733,8 +733,8 @@ especially when forward references or invalid types were involved. Compare::
733
733
StrCache = ' Cache[str]' # a type alias
734
734
LOG_PREFIX = ' LOG[DEBUG]' # a module constant
735
735
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::
738
738
739
739
StrCache: TypeAlias = ' Cache[str]' # a type alias
740
740
LOG_PREFIX = ' LOG[DEBUG]' # a module constant
@@ -805,6 +805,10 @@ Other Language Changes
805
805
defined by :pep:`526 ` ) no longer cause any runtime effects with `` from __future__ import annotations`` .
806
806
(Contributed by Batuhan Taskaya in :issue:`42737 ` .)
807
807
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 ` .)
808
812
809
813
New Modules
810
814
========== =
@@ -987,10 +991,18 @@ inspect
987
991
When a module does not define `` __loader__`` , fall back to `` __spec__ .loader`` .
988
992
(Contributed by Brett Cannon in :issue:`42133 ` .)
989
993
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 ` .)
994
1006
995
1007
linecache
996
1008
-------- -
@@ -1155,7 +1167,7 @@ of types readily interpretable by type checkers.
1155
1167
typing
1156
1168
------
1157
1169
1158
- For major changes, see `New Features Related to Type Annotations ` _.
1170
+ For major changes, see `New Features Related to Type Hints ` _.
1159
1171
1160
1172
The behavior of :class :`typing.Literal` was changed to conform with :pep:`586 `
1161
1173
and to match the behavior of static type checkers specified in the PEP .
@@ -1255,11 +1267,12 @@ Optimizations
1255
1267
faster, lzma decompression 1. 20x ~ 1. 32x faster, `` GzipFile.read(- 1 )`` 1. 11x
1256
1268
~ 1. 18x faster. (Contributed by Ma Lin, reviewed by Gregory P. Smith, in :issue:`41486 ` )
1257
1269
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 ` )
1263
1276
1264
1277
* Substring search functions such as `` str1 in str2`` and `` str2.find(str1)``
1265
1278
now sometimes use Crochemore & Perrin' s "Two-Way" string searching
@@ -1569,8 +1582,8 @@ Changes in the Python API
1569
1582
CPython bytecode changes
1570
1583
========================
1571
1584
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 .
1574
1587
(Contributed by Yurii Karabas and Inada Naoki in :issue:`42202 ` )
1575
1588
1576
1589
Build Changes
0 commit comments