@@ -125,105 +125,48 @@ Check :pep:`617` for more details.
125125in :issue: `12782 ` and :issue: `40334 `.)
126126
127127
128- PEP 563: Postponed Evaluation of Annotations Becomes Default
129- ------------------------------------------------------------
130-
131- In Python 3.7, postponed evaluation of annotations was added,
132- to be enabled with a ``from __future__ import annotations ``
133- directive. In 3.10 this became the default behavior, even
134- without that future directive. With this being default, all
135- annotations stored in :attr: `__annotations__ ` will be strings.
136- If needed, annotations can be resolved at runtime using
137- :func: `typing.get_type_hints `. See :pep: `563 ` for a full
138- description. Also, the :func: `inspect.signature ` will try to
139- resolve types from now on, and when it fails it will fall back to
140- showing the string annotations. (Contributed by Batuhan Taskaya
141- in :issue: `38605 `.)
142-
143- * The :class: `int ` type has a new method :meth: `int.bit_count `, returning the
144- number of ones in the binary expansion of a given integer, also known
145- as the population count. (Contributed by Niklas Fiekas in :issue: `29882 `.)
146-
147- * The views returned by :meth: `dict.keys `, :meth: `dict.values ` and
148- :meth: `dict.items ` now all have a ``mapping `` attribute that gives a
149- :class: `types.MappingProxyType ` object wrapping the original
150- dictionary. (Contributed by Dennis Sweeney in :issue: `40890 `.)
151-
152- * :pep: `618 `: The :func: `zip ` function now has an optional ``strict `` flag, used
153- to require that all the iterables have an equal length.
154-
155- PEP 613: TypeAlias Annotation
156- -----------------------------
157-
158- :pep: `484 ` introduced the concept of type aliases, only requiring them to be
159- top-level unannotated assignments. This simplicity sometimes made it difficult
160- for type checkers to distinguish between type aliases and ordinary assignments,
161- especially when forward references or invalid types were involved. Compare::
162-
163- StrCache = 'Cache[str]' # a type alias
164- LOG_PREFIX = 'LOG[DEBUG]' # a module constant
165-
166- Now the :mod: `typing ` module has a special annotation :data: `TypeAlias ` to
167- declare type aliases more explicitly::
168-
169- StrCache: TypeAlias = 'Cache[str]' # a type alias
170- LOG_PREFIX = 'LOG[DEBUG]' # a module constant
171-
172- See :pep: `613 ` for more details.
173-
174- (Contributed by Mikhail Golubev in :issue: `41923 `.)
175-
176- PEP 604: New Type Union Operator
177- --------------------------------
178-
179- A new type union operator was introduced which enables the syntax ``X | Y ``.
180- This provides a cleaner way of expressing 'either type X or type Y' instead of
181- using :data: `typing.Union `, especially in type hints (annotations).
182-
183- In previous versions of Python, to apply a type hint for functions accepting
184- arguments of multiple types, :data: `typing.Union ` was used::
185-
186- def square(number: Union[int, float]) -> Union[int, float]:
187- return number ** 2
128+ Better error messages in the parser
129+ -----------------------------------
188130
131+ When parsing code that contains unclosed parentheses or brackets the interpreter
132+ now includes the location of the unclosed bracket of parentheses instead of displaying
133+ *SyntaxError: unexpected EOF while parsing * or pointing to some incorrect location.
134+ For instance, consider the following code (notice the unclosed '{'):
189135
190- Type hints can now be written in a more succinct manner::
136+ .. code-block :: python
191137
192- def square(number: int | float) -> int | float:
193- return number ** 2
138+ expected = {9 : 1 , 18 : 2 , 19 : 2 , 27 : 3 , 28 : 3 , 29 : 3 , 36 : 4 , 37 : 4 ,
139+ 38 : 4 , 39 : 4 , 45 : 5 , 46 : 5 , 47 : 5 , 48 : 5 , 49 : 5 , 54 : 6 ,
140+ some_other_code = foo()
194141
142+ previous versions of the interpreter reported confusing places as the location of
143+ the syntax error:
195144
196- This new syntax is also accepted as the second argument to :func: `isinstance `
197- and :func: `issubclass `::
145+ .. code- block:: text
198146
199- >>> isinstance(1, int | str)
200- True
147+ File " example.py" , line 3
148+ some_other_code = foo()
149+ ^
150+ SyntaxError : invalid syntax
201151
202- See :ref: ` types-union ` and :pep: ` 604 ` for more details.
152+ but in Python3.10 a more informative error is emitted:
203153
204- (Contributed by Maggie Moss and Philippe Prados in :issue: ` 41428 `.)
154+ .. code - block:: text
205155
206- PEP 612: Parameter Specification Variables
207- ------------------------------------------
156+ File " example.py" , line 1
157+ expected = {9 : 1 , 18 : 2 , 19 : 2 , 27 : 3 , 28 : 3 , 29 : 3 , 36 : 4 , 37 : 4 ,
158+ ^
159+ SyntaxError : ' {' was never closed
208160
209- Two new options to improve the information provided to static type checkers for
210- :pep: `484 `\ 's ``Callable `` have been added to the :mod: `typing ` module.
211161
212- The first is the parameter specification variable. They are used to forward the
213- parameter types of one callable to another callable -- a pattern commonly
214- found in higher order functions and decorators. Examples of usage can be found
215- in :class: `typing.ParamSpec `. Previously, there was no easy way to type annotate
216- dependency of parameter types in such a precise manner.
162+ In a similar way, errors involving unclosed string literals (single and triple
163+ quoted) now point to the start of the string instead of reporting EOF / EOL .
217164
218- The second option is the new ``Concatenate `` operator. It's used in conjunction
219- with parameter specification variables to type annotate a higher order callable
220- which adds or removes parameters of another callable. Examples of usage can
221- be found in :class: `typing.Concatenate `.
165+ These improvements are inspired by previous work in the PyPy interpreter.
222166
223- See :class: ` typing.Callable `, :class: ` typing.ParamSpec `,
224- :class: ` typing.Concatenate ` and :pep: ` 612 ` for more details.
167+ (Contributed by Pablo Galindo in :issue: ` 42864 ` and Batuhan Taskaya in
168+ :issue: ` 40176 ` .)
225169
226- (Contributed by Ken Jin in :issue: `41559 `.)
227170
228171PEP 634 : Structural Pattern Matching
229172------------------------------------
@@ -500,57 +443,128 @@ Several other key features:
500443For the full specification see :pep:`634 ` . Motivation and rationale
501444are in :pep:`635 ` , and a longer tutorial is in :pep:`636 ` .
502445
503- Better error messages in the parser
504- -----------------------------------
505446
506- When parsing code that contains unclosed parentheses or brackets the interpreter
507- now includes the location of the unclosed bracket of parentheses instead of displaying
508- *SyntaxError: unexpected EOF while parsing * or pointing to some incorrect location.
509- For instance, consider the following code (notice the unclosed '{'):
447+ New Features Related to Type Annotations
448+ ========================================
510449
511- .. code-block :: python
450+ This section covers major changes affecting :pep:`484 ` type annotations and
451+ the :mod:`typing` module.
512452
513- expected = {9 : 1 , 18 : 2 , 19 : 2 , 27 : 3 , 28 : 3 , 29 : 3 , 36 : 4 , 37 : 4 ,
514- 38 : 4 , 39 : 4 , 45 : 5 , 46 : 5 , 47 : 5 , 48 : 5 , 49 : 5 , 54 : 6 ,
515- some_other_code = foo()
516453
517- previous versions of the interpreter reported confusing places as the location of
518- the syntax error:
454+ PEP 563 : Postponed Evaluation of Annotations Becomes Default
455+ ------------------------------------------------------------
519456
520- .. code- block:: text
457+ In Python 3.7 , postponed evaluation of annotations was added,
458+ to be enabled with a `` from __future__ import annotations``
459+ directive. In 3.10 this became the default behavior, even
460+ without that future directive. With this being default, all
461+ annotations stored in :attr:`__annotations__ ` will be strings.
462+ If needed, annotations can be resolved at runtime using
463+ :func:`typing.get_type_hints` . See :pep:`563 ` for a full
464+ description. Also, the :func:`inspect.signature` will try to
465+ resolve types from now on, and when it fails it will fall back to
466+ showing the string annotations. (Contributed by Batuhan Taskaya
467+ in :issue:`38605 ` .)
521468
522- File " example.py" , line 3
523- some_other_code = foo()
524- ^
525- SyntaxError : invalid syntax
526469
527- but in Python3.10 a more informative error is emitted:
470+ PEP 604 : New Type Union Operator
471+ --------------------------------
528472
529- .. code- block:: text
473+ A new type union operator was introduced which enables the syntax `` X | Y`` .
474+ This provides a cleaner way of expressing ' either type X or type Y' instead of
475+ using :data:`typing.Union` , especially in type hints (annotations).
530476
531- File " example.py" , line 1
532- expected = {9 : 1 , 18 : 2 , 19 : 2 , 27 : 3 , 28 : 3 , 29 : 3 , 36 : 4 , 37 : 4 ,
533- ^
534- SyntaxError : ' {' was never closed
477+ In previous versions of Python, to apply a type hint for functions accepting
478+ arguments of multiple types, :data:`typing.Union` was used::
535479
480+ def square(number: Union[int , float ]) -> Union[int , float ]:
481+ return number ** 2
536482
537- In a similar way, errors involving unclosed string literals (single and triple
538- quoted) now point to the start of the string instead of reporting EOF / EOL .
539483
540- These improvements are inspired by previous work in the PyPy interpreter.
484+ Type hints can now be written in a more succinct manner::
485+
486+ def square(number: int | float ) -> int | float :
487+ return number ** 2
488+
489+
490+ This new syntax is also accepted as the second argument to :func:`isinstance `
491+ and :func:`issubclass ` ::
492+
493+ >> > isinstance (1 , int | str )
494+ True
495+
496+ See :ref:`types- union` and :pep:`604 ` for more details.
497+
498+ (Contributed by Maggie Moss and Philippe Prados in :issue:`41428 ` .)
499+
500+
501+ PEP 612 : Parameter Specification Variables
502+ ------------------------------------------
503+
504+ Two new options to improve the information provided to static type checkers for
505+ :pep:`484 ` \ 's ``Callable`` have been added to the :mod:`typing` module.
506+
507+ The first is the parameter specification variable. They are used to forward the
508+ parameter types of one callable to another callable -- a pattern commonly
509+ found in higher order functions and decorators. Examples of usage can be found
510+ in :class :`typing.ParamSpec` . Previously, there was no easy way to type annotate
511+ dependency of parameter types in such a precise manner.
512+
513+ The second option is the new `` Concatenate`` operator. It' s used in conjunction
514+ with parameter specification variables to type annotate a higher order callable
515+ which adds or removes parameters of another callable . Examples of usage can
516+ be found in :class :`typing.Concatenate` .
517+
518+ See :class :`typing.Callable` , :class :`typing.ParamSpec` ,
519+ :class :`typing.Concatenate` and :pep:`612 ` for more details.
520+
521+ (Contributed by Ken Jin in :issue:`41559 ` .)
522+
523+
524+ PEP 613 : TypeAlias Annotation
525+ ---------------------------- -
526+
527+ :pep:`484 ` introduced the concept of type aliases, only requiring them to be
528+ top- level unannotated assignments. This simplicity sometimes made it difficult
529+ for type checkers to distinguish between type aliases and ordinary assignments,
530+ especially when forward references or invalid types were involved. Compare::
531+
532+ StrCache = ' Cache[str]' # a type alias
533+ LOG_PREFIX = ' LOG[DEBUG]' # a module constant
534+
535+ Now the :mod:`typing` module has a special annotation :data:`TypeAlias` to
536+ declare type aliases more explicitly::
537+
538+ StrCache: TypeAlias = ' Cache[str]' # a type alias
539+ LOG_PREFIX = ' LOG[DEBUG]' # a module constant
540+
541+ See :pep:`613 ` for more details.
542+
543+ (Contributed by Mikhail Golubev in :issue:`41923 ` .)
541544
542- (Contributed by Pablo Galindo in :issue:`42864 ` and Batuhan Taskaya in
543- :issue:`40176 ` .)
544545
545546Other Language Changes
546547======================
547548
549+ * The :class :`int ` type has a new method :meth:`int .bit_count` , returning the
550+ number of ones in the binary expansion of a given integer, also known
551+ as the population count. (Contributed by Niklas Fiekas in :issue:`29882 ` .)
552+
553+ * The views returned by :meth:`dict .keys` , :meth:`dict .values` and
554+ :meth:`dict .items` now all have a `` mapping`` attribute that gives a
555+ :class :`types.MappingProxyType` object wrapping the original
556+ dictionary. (Contributed by Dennis Sweeney in :issue:`40890 ` .)
557+
558+ * :pep:`618 ` : The :func:`zip ` function now has an optional `` strict`` flag, used
559+ to require that all the iterables have an equal length.
560+
548561* Builtin and extension functions that take integer arguments no longer accept
549562 :class :`~ decimal.Decimal` \ s, :class:`~fractions.Fraction`\ s and other
550563 objects that can be converted to integers only with a loss (e.g. that have
551564 the :meth:`~ object .__int__ ` method but do not have the
552565 :meth:`~ object .__index__ ` method).
553566 (Contributed by Serhiy Storchaka in :issue:`37999 ` .)
567+
554568* If :func:`object .__ipow__ ` returns :const:`NotImplemented ` , the operator will
555569 correctly fall back to :func:`object .__pow__ ` and :func:`object .__rpow__ ` as expected.
556570 (Contributed by Alex Shkop in :issue:`38302 ` .)
@@ -800,6 +814,8 @@ of types readily interpretable by type checkers.
800814typing
801815------
802816
817+ For major changes, see `New Features Related to Type Annotations` _.
818+
803819The behavior of :class :`typing.Literal` was changed to conform with :pep:`586 `
804820and to match the behavior of static type checkers specified in the PEP .
805821
0 commit comments