Skip to content
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

Improve docs of PEP 604 Union #24301

Merged
merged 2 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 10 additions & 2 deletions Doc/library/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -862,19 +862,27 @@ are always available. They are listed here in alphabetical order.
class>`) subclass thereof. If *object* is not
an object of the given type, the function always returns ``False``.
If *classinfo* is a tuple of type objects (or recursively, other such
tuples), return ``True`` if *object* is an instance of any of the types.
tuples) or a :ref:`types-union` of multiple types, return ``True`` if
*object* is an instance of any of the types.
If *classinfo* is not a type or tuple of types and such tuples,
a :exc:`TypeError` exception is raised.

.. versionchanged:: 3.10
*classinfo* can be a :ref:`types-union`.


.. function:: issubclass(class, classinfo)

Return ``True`` if *class* is a subclass (direct, indirect or :term:`virtual
<abstract base class>`) of *classinfo*. A
class is considered a subclass of itself. *classinfo* may be a tuple of class
objects, in which case every entry in *classinfo* will be checked. In any other
objects or a :ref:`types-union`, in which case every entry in *classinfo*
will be checked. In any other
case, a :exc:`TypeError` exception is raised.

.. versionchanged:: 3.10
*classinfo* can be a :ref:`types-union`.


.. function:: iter(object[, sentinel])

Expand Down
19 changes: 3 additions & 16 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5022,8 +5022,10 @@ enables cleaner type hinting syntax compared to :data:`typing.Union`.
str | None == typing.Optional[str]

.. describe:: isinstance(obj, union_object)
.. describe:: issubclass(obj, union_object)

Calls to :func:`isinstance` are also supported with a union object::
Calls to :func:`isinstance` and :func:`issubclass` are also supported with a
union object::

>>> isinstance("", int | str)
True
Expand All @@ -5036,21 +5038,6 @@ enables cleaner type hinting syntax compared to :data:`typing.Union`.
File "<stdin>", line 1, in <module>
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

However, union objects containing :ref:`parameterized generics
<types-genericalias>` cannot be used::

>>> issubclass(bool, bool | list[str])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: issubclass() argument 2 cannot contain a parameterized generic

The user-exposed type for the union object can be accessed from
:data:`types.Union` and used for :func:`isinstance` checks. An object cannot be
instantiated from the type::
Expand Down
8 changes: 7 additions & 1 deletion Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,13 @@ Type hints can now be written in a more succinct manner::
return number ** 2


See :pep:`604` for more details.
This new syntax is also accepted as the second argument to :func:`isinstance`
and :func:`issubclass`::

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

See :ref:`types-union` and :pep:`604` for more details.

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

Expand Down