8000 bpo-41428: Documentation for PEP 604 by Fidget-Spinner · Pull Request #22517 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-41428: Documentation for PEP 604 #22517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Implement suggestions by gvanrossum and pablosgal
  • Loading branch information
Fidget-Spinner committed Oct 4, 2020
commit a2167db4eef6ee782437f5180afdc6cffdaee92f
50 changes: 25 additions & 25 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4746,21 +4746,21 @@ single class dictionary lookup is negligible.
.. _types-union:

Union Type
====================
==========

.. index::
object: Union
pair: union; type

A union object holds the value of the ``|`` (bitwise or) operation on
multiple :ref:`type objects<bltin-type-objects>`. This enables cleaner type
hinting syntax compared to :data:`typing.Union`.
multiple :ref:`type objects<bltin-type-objects>`. These types are intended
primarily for type annotations. The union type expression
enables cleaner type hinting syntax compared to :data:`typing.Union`.

.. describe:: X | Y | ...

Defines a union object which holds types *X*, *Y*, and so forth. X | Y
means either X or Y. It is syntactically equivalent to
``typing.Union[X, Y, ...]``.
Defines a union object which holds types *X*, *Y*, and so forth. ``X | Y``
means either X or Y. It is equivalent to ``typing.Union[X, Y, ...]``.
Example::

def square(number: int | float) -> int | float:
Expand All @@ -4774,19 +4774,19 @@ hinting syntax compared to :data:`typing.Union`.

(int | str) | float == int | str | float

* Redundant arguments are skipped, e.g.::
* Redundant types are removed, e.g.::

int | str | int == int | str

* When comparing unions, the argument order is ignored, e.g.::
* When comparing unions, the order is ignored, e.g.::

int | str == str | int

* Compatible with :data:`typing.Union`::
* It is compatible with :data:`typing.Union`::

int | str == typing.Union[int, str]

* Optional values are equivalent to :data:`typing.Optional`::
* Optional types can be spelled as a union with None::

str | None == typing.Optional[str]

Expand All @@ -4797,7 +4797,7 @@ hinting syntax compared to :data:`typing.Union`.
>>> isinstance("", int | str)
True

Union objects containing parametrized generics cannot be used::
However, union objects containing parameterized generics cannot be used::

>>> isinstance(1, int | list[int])
TypeError: isinstance() argument 2 cannot contain a parameterized generic
Expand All @@ -4809,7 +4809,8 @@ hinting syntax compared to :data:`typing.Union`.
>>> issubclass(bool, int | str)
True

Union objects containing parametrized generics cannot be used::
However, union objects containing parameterized :ref:`generics<generics>`
cannot be used::

>>> issubclass(bool, bool | list[str])
TypeError: issubclass() argument 2 cannot contain a parameterized generic
Expand All @@ -4826,21 +4827,20 @@ cannot be instantiated from the type::

.. note::
The :meth:`__or__` method for type objects was added to support the syntax
X | Y. If a metaclass implements :meth:`__or__`, the Union may
``X | Y``. If a metaclass implements :meth:`__or__`, the Union may
override it::

class M(type):
def __or__(self, other):
return "Hello"

class C(metaclass=M):
pass

# 'Hello'
print(C | int)

# 'int | __main__.C'
print(int | C)
>>> class M(type):
... def __or__(self, other):
... return "Hello"
...
>>> class C(metaclass=M):
... pass
...
>>> C | int
'Hello'
>>> int | C
int | __main__.C

.. seealso::

Expand Down
2 changes: 1 addition & 1 deletion Doc/library/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ Standard names are defined for the following types:

.. data:: Union

The type of :ref:`builtins.Union<types-union>`.
The type of :ref:`union type expressions<types-union>`.

.. versionadded:: 3.10

Expand Down
4 changes: 2 additions & 2 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,8 @@ These can be used as types in annotations using ``[]``, each having a unique syn
Don't remove explicit subclasses from unions at runtime.

.. versionchanged:: 3.10
Unions can now be written as X | Y. See :ref:`builtins.Union
<types-union>`.
Unions can now be written as ``X | Y``. See
:ref:`union type expressions<types-union>`.

.. data:: Optional

Expand Down
EF4A 21 changes: 18 additions & 3 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,24 @@ 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:`604`: Builtin Union type to allow writing X | Y in place of
``typing.Union[X, Y]``.
(Contributed by Maggie Moss and Philippe Prados in :issue:`41428`.)
PEP604: New Type Operators
--------------------------
There is a new syntax ``X | Y`` that allows for type union expressions. This
provides a cleaner way of writing 'either type X or type Y' instead of
using :data:`typing.Union`.

For example::

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

Which is equivalent to, but more readable than using ``typing.Union``::

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

See :pep:`604` 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 @@
Add documentation for :pep:`604` (Allow writing union types as X | Y).
Add documentation for :pep:`604` (Allow writing union types as ``X | Y``).
0