Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
105 changes: 105 additions & 0 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4743,6 +4743,111 @@ define these methods must provide them as a normal Python accessible method.
Compared to the overhead of setting up the runtime context, the overhead of a
single class dictionary lookup is negligible.

.. _types-union:

Union Type
====================
Comment thread
Fidget-Spinner marked this conversation as resolved.
Resolved

.. 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`.
Comment thread
Fidget-Spinner marked this conversation as resolved.
Resolved

.. 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
Comment thread
Fidget-Spinner marked this conversation as resolved.
Resolved
``typing.Union[X, Y, ...]``.
Example::

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

.. describe:: union_object == other

Union objects can be tested for equality with other union objects. Details:

* Unions of unions are flattened, e.g.::

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

* Redundant arguments are skipped, e.g.::
Comment thread
Fidget-Spinner marked this conversation as resolved.
Resolved

int | str | int == int | str

* When comparing unions, the argument order is ignored, e.g.::
Comment thread
Fidget-Spinner marked this conversation as resolved.
Resolved

int | str == str | int

* Compatible with :data:`typing.Union`::
Comment thread
Fidget-Spinner marked this conversation as resolved.
Resolved

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

* Optional values are equivalent to :data:`typing.Optional`::
Comment thread
Fidget-Spinner marked this conversation as resolved.
Resolved

str | None == typing.Optional[str]

.. describe:: isinstance(obj, union_object)

Calls to :func:`isinstance` are also supported with a Union object::

>>> isinstance("", int | str)
True

Union objects containing parametrized generics cannot be used::
Comment thread
Fidget-Spinner marked this conversation as resolved.
Resolved

>>> isinstance(1, int | list[int])
TypeError: isinstance() argument 2 cannot contain a parameterized generic

.. describe:: issubclass(obj, union_object)

Calls to :func:`issubclass` are also supported with a Union Object.::

>>> issubclass(bool, int | str)
True

Union objects containing parametrized generics cannot be used::
Comment thread
Fidget-Spinner marked this conversation as resolved.
Resolved

>>> issubclass(bool, bool | list[str])
TypeError: issubclass() argument 2 cannot contain a parameterized generic


The type for the Union object is :data:`types.Union`. An object
cannot be instantiated from the type::

>>> import types
>>> isinstance(int | str, types.Union)
True
>>> types.Union()
TypeError: cannot create 'types.Union' instances

.. 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
Comment thread
Fidget-Spinner marked this conversation as resolved.
Resolved
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)
Comment thread
Fidget-Spinner marked this conversation as resolved.
Resolved

.. seealso::

:pep:`604` -- PEP proposing the Union object and type.

.. versionadded:: 3.10


.. _typesother:

Expand Down
5 changes: 5 additions & 0 deletions Doc/library/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ Standard names are defined for the following types:

.. versionadded:: 3.10

.. data:: Union

The type of :ref:`builtins.Union<types-union>`.
Comment thread
Fidget-Spinner marked this conversation as resolved.
Resolved

.. versionadded:: 3.10

.. class:: TracebackType(tb_next, tb_frame, tb_lasti, tb_lineno)

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

.. versionchanged:: 3.10
Unions can now be written as X | Y. See :ref:`builtins.Union
Comment thread
Fidget-Spinner marked this conversation as resolved.
Resolved
<types-union>`.

.. data:: Optional

Optional type.
Expand Down
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ 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
Comment thread
Fidget-Spinner marked this conversation as resolved.
Resolved
``typing.Union[X, Y]``.
(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
@@ -0,0 +1 @@
Add documentation for :pep:`604` (Allow writing union types as X | Y).
Comment thread
Fidget-Spinner marked this conversation as resolved.
Resolved