Skip to content

Commit 5f77dee

Browse files
Improve docs of PEP 604 Union (#24301)
1 parent d65b903 commit 5f77dee

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

Doc/library/functions.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -870,19 +870,27 @@ are always available. They are listed here in alphabetical order.
870870
class>`) subclass thereof. If *object* is not
871871
an object of the given type, the function always returns ``False``.
872872
If *classinfo* is a tuple of type objects (or recursively, other such
873-
tuples), return ``True`` if *object* is an instance of any of the types.
873+
tuples) or a :ref:`types-union` of multiple types, return ``True`` if
874+
*object* is an instance of any of the types.
874875
If *classinfo* is not a type or tuple of types and such tuples,
875876
a :exc:`TypeError` exception is raised.
876877

878+
.. versionchanged:: 3.10
879+
*classinfo* can be a :ref:`types-union`.
880+
877881

878882
.. function:: issubclass(class, classinfo)
879883

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

891+
.. versionchanged:: 3.10
892+
*classinfo* can be a :ref:`types-union`.
893+
886894

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

Doc/library/stdtypes.rst

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5022,8 +5022,10 @@ enables cleaner type hinting syntax compared to :data:`typing.Union`.
50225022
str | None == typing.Optional[str]
50235023

50245024
.. describe:: isinstance(obj, union_object)
5025+
.. describe:: issubclass(obj, union_object)
50255026

5026-
Calls to :func:`isinstance` are also supported with a union object::
5027+
Calls to :func:`isinstance` and :func:`issubclass` are also supported with a
5028+
union object::
50275029

50285030
>>> isinstance("", int | str)
50295031
True
@@ -5036,21 +5038,6 @@ enables cleaner type hinting syntax compared to :data:`typing.Union`.
50365038
File "<stdin>", line 1, in <module>
50375039
TypeError: isinstance() argument 2 cannot contain a parameterized generic
50385040

5039-
.. describe:: issubclass(obj, union_object)
5040-
5041-
Calls to :func:`issubclass` are also supported with a union object::
5042-
5043-
>>> issubclass(bool, int | str)
5044-
True
5045-
5046-
However, union objects containing :ref:`parameterized generics
5047-
<types-genericalias>` cannot be used::
5048-
5049-
>>> issubclass(bool, bool | list[str])
5050-
Traceback (most recent call last):
5051-
File "<stdin>", line 1, in <module>
5052-
TypeError: issubclass() argument 2 cannot contain a parameterized generic
5053-
50545041
The user-exposed type for the union object can be accessed from
50555042
:data:`types.Union` and used for :func:`isinstance` checks. An object cannot be
50565043
instantiated from the type::

Doc/whatsnew/3.10.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,13 @@ Type hints can now be written in a more succinct manner::
193193
return number ** 2
194194

195195

196-
See :pep:`604` for more details.
196+
This new syntax is also accepted as the second argument to :func:`isinstance`
197+
and :func:`issubclass`::
198+
199+
>>> isinstance(1, int | str)
200+
True
201+
202+
See :ref:`types-union` and :pep:`604` for more details.
197203

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

0 commit comments

Comments
 (0)