Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ In the function ``greeting``, the argument ``name`` is expected to be of type
:class:`str` and the return type :class:`str`. Subtypes are accepted as
arguments.

.. _type-aliases:

Type aliases
============

Expand Down Expand Up @@ -489,6 +491,17 @@ These can be used as types in annotations and do not support ``[]``.
.. versionadded:: 3.5.4
.. versionadded:: 3.6.2

.. data:: TypeAlias

Special annotation for explicitly declaring a :ref:`type alias <type-aliases>`.
For example::

from typing import TypeAlias

Factors: TypeAlias = list[int]

.. versionadded:: 3.10

Special forms
"""""""""""""

Expand Down
46 changes: 43 additions & 3 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,50 @@ New Features
* :pep:`618`: The :func:`zip` function now has an optional ``strict`` flag, used
to require that all the iterables have an equal length.

* :pep:`613`: The :mod:`typing` module now has a special annotation
:class:`TypeAlias` to explicitly declare :pep:`484`-compliant type aliases,
better differentiating them from regular assignments.
PEP613: TypeAlias Annotation
----------------------------

:pep:`484` introduced the concept of type aliases, only requiring them to be
top-level unannotated assignments. This simplicity sometimes made it difficult
for type checkers to distinguish between type aliases and ordinary assignments,
especially when forward references or invalid types were involved. Compare::

StrCache = 'Cache[str]' # a type alias
LOG_PREFIX = 'LOG[DEBUG]' # a module constant

Now the :mod:`typing` module has a special annotation :data:`TypeAlias` to
declare type aliases more explicitly::

StrCache: TypeAlias = 'Cache[str]' # a type alias
LOG_PREFIX = 'LOG[DEBUG]' # a module constant

See :pep:`613` for more details.

(Contributed by Mikhail Golubev in :issue:`41923`.)

PEP604: New Type Operator
-------------------------
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
PEP604: New Type Operator
-------------------------
PEP 604: New Type Union Operator
--------------------------------


A new type union operator was introduced which enables the syntax ``X | Y``.
This provides a cleaner way of expressing 'either type X or type Y' instead of
using :data:`typing.Union`, especially in type hints (annotations).

In previous versions of Python, to apply a type hint for functions accepting
arguments of multiple types, :data:`typing.Union` was used::

def square(number: Union[int, float]) -> Union[int, float]:
return number ** 2


Now, type hints can be written in a more succinct manner::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Now, type hints can be written in a more succinct manner::
Type hints can now be written in a more succinct manner::


def square(number: int | float) -> int | float:
return number ** 2


See :pep:`604` for more details.

(Contributed by Maggie Moss and Philippe Prados in :issue:`41428`.)

Other Language Changes
======================
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Implement :pep:`613`, introducing :class:`typing.TypeAlias` annotation.
Implement :pep:`613`, introducing :data:`typing.TypeAlias` annotation.