@@ -4968,15 +4968,16 @@ Union Type
4968
4968
pair: union; type
4969
4969
4970
4970
A union object holds the value of the ``| `` (bitwise or) operation on
4971
- multiple :ref: `type objects<bltin-type-objects> `. These types are intended
4972
- primarily for type annotations. The union type expression enables cleaner
4973
- type hinting syntax compared to :data: `typing.Union `.
4971
+ multiple :ref: `type objects <bltin-type-objects >`. These types are intended
4972
+ primarily for :term: ` type annotations <annotation> ` . The union type expression
4973
+ enables cleaner type hinting syntax compared to :data: `typing.Union `.
4974
4974
4975
4975
.. describe :: X | Y | ...
4976
4976
4977
4977
Defines a union object which holds types *X *, *Y *, and so forth. ``X | Y ``
4978
4978
means either X or Y. It is equivalent to ``typing.Union[X, Y] ``.
4979
- Example::
4979
+ For example, the following function expects an argument of type
4980
+ :class: `int ` or :class: `float `::
4980
4981
4981
4982
def square(number: int | float) -> int | float:
4982
4983
return number ** 2
@@ -4985,15 +4986,15 @@ type hinting syntax compared to :data:`typing.Union`.
4985
4986
4986
4987
Union objects can be tested for equality with other union objects. Details:
4987
4988
4988
- * Unions of unions are flattened, e.g. ::
4989
+ * Unions of unions are flattened::
4989
4990
4990
4991
(int | str) | float == int | str | float
4991
4992
4992
- * Redundant types are removed, e.g. ::
4993
+ * Redundant types are removed::
4993
4994
4994
4995
int | str | int == int | str
4995
4996
4996
- * When comparing unions, the order is ignored, e.g. ::
4997
+ * When comparing unions, the order is ignored::
4997
4998
4998
4999
int | str == str | int
4999
5000
@@ -5012,14 +5013,8 @@ type hinting syntax compared to :data:`typing.Union`.
5012
5013
>>> isinstance("", int | str)
5013
5014
True
5014
5015
5015
- ..
5016
- At the time of writing this, there is no documentation for parameterized
5017
- generics or PEP 585. Thus the link currently points to PEP 585 itself.
5018
- Please change the link for parameterized generics to reference the correct
5019
- documentation once documentation for PEP 585 becomes available.
5020
-
5021
- However, union objects containing `parameterized generics
5022
- <https://www.python.org/dev/peps/pep-0585/> `_ cannot be used::
5016
+ However, union objects containing :ref: `parameterized generics
5017
+ <types-genericalias>` cannot be used::
5023
5018
5024
5019
>>> isinstance (1 , int | list[int ])
5025
5020
Traceback (most recent call last):
@@ -5033,20 +5028,16 @@ type hinting syntax compared to :data:`typing.Union`.
5033
5028
>>> issubclass(bool, int | str)
5034
5029
True
5035
5030
5036
- ..
5037
- Once again, please change the link below for parameterized generics to
5038
- reference the correct documentation once documentation for PEP 585
5039
- becomes available.
5040
-
5041
- However, union objects containing `parameterized generics
5042
- <https://www.python.org/dev/peps/pep-0585/> `_ cannot be used::
5031
+ However, union objects containing :ref: `parameterized generics
5032
+ <types-genericalias>` cannot be used::
5043
5033
5044
5034
>>> issubclass (bool , bool | list[str ])
5045
5035
Traceback (most recent call last):
5046
5036
File "<stdin>", line 1, in <module>
5047
5037
TypeError: issubclass() argument 2 cannot contain a parameterized generic
5048
5038
5049
- The type of a union object is :data: `types.Union `. An object cannot be
5039
+ The user-exposed type for the union object can be accessed from
5040
+ :data: `types.Union ` and used for :func: `isinstance ` checks. An object cannot be
5050
5041
instantiated from the type::
5051
5042
5052
5043
>>> import types
0 commit comments