@@ -321,11 +321,11 @@ not generic but implicitly inherits from ``Iterable[Any]``::
321
321
User defined generic type aliases are also supported. Examples::
322
322
323
323
from collections.abc import Iterable
324
- from typing import TypeVar, Union
324
+ from typing import TypeVar
325
325
S = TypeVar('S')
326
- Response = Union[ Iterable[S], int]
326
+ Response = Iterable[S] | int
327
327
328
- # Return type here is same as Union[ Iterable[str], int]
328
+ # Return type here is same as Iterable[str] | int
329
329
def response(query: str) -> Response[str]:
330
330
...
331
331
@@ -588,9 +588,9 @@ These can be used as types in annotations using ``[]``, each having a unique syn
588
588
589
589
.. data :: Union
590
590
591
- Union type; ``Union[X, Y] `` means either X or Y.
591
+ Union type; ``Union[X, Y] `` is equivalent to `` X | Y `` and means either X or Y.
592
592
593
- To define a union, use e.g. ``Union[int, str] ``. Details:
593
+ To define a union, use e.g. ``Union[int, str] `` or the shorthand `` int | str `` . Details:
594
594
595
595
* The arguments must be types and there must be at least one.
596
596
@@ -604,18 +604,16 @@ These can be used as types in annotations using ``[]``, each having a unique syn
604
604
605
605
* Redundant arguments are skipped, e.g.::
606
606
607
- Union[int, str, int] == Union[int, str]
607
+ Union[int, str, int] == Union[int, str] == int | str
608
608
609
609
* When comparing unions, the argument order is ignored, e.g.::
610
610
611
611
Union[int, str] == Union[str, int]
612
612
613
- * You cannot subclass or instantiate a union .
613
+ * You cannot subclass or instantiate a `` Union `` .
614
614
615
615
* You cannot write ``Union[X][Y] ``.
616
616
617
- * You can use ``Optional[X] `` as a shorthand for ``Union[X, None] ``.
618
-
619
617
.. versionchanged :: 3.7
620
618
Don't remove explicit subclasses from unions at runtime.
621
619
@@ -627,7 +625,7 @@ These can be used as types in annotations using ``[]``, each having a unique syn
627
625
628
626
Optional type.
629
627
630
- ``Optional[X] `` is equivalent to ``Union[X, None] ``.
628
+ ``Optional[X] `` is equivalent to ``X | None `` (or `` Union[X, None] ``) .
631
629
632
630
Note that this is not the same concept as an optional argument,
633
631
which is one that has a default. An optional argument with a
@@ -644,6 +642,10 @@ These can be used as types in annotations using ``[]``, each having a unique syn
644
642
def foo(arg: Optional[int] = None) -> None:
645
643
...
646
644
645
+ .. versionchanged :: 3.10
646
+ Optional can now be written as ``X | None ``. See
647
+ :ref: `union type expressions<types-union> `.
648
+
647
649
.. data :: Callable
648
650
649
651
Callable type; ``Callable[[int], str] `` is a function of (int) -> str.
@@ -770,7 +772,7 @@ These can be used as types in annotations using ``[]``, each having a unique syn
770
772
:ref: `type variables <generics >`, and unions of any of these types.
771
773
For example::
772
774
773
- def new_non_team_user(user_class: Type[Union[ BasicUser, ProUser] ]): ...
775
+ def new_non_team_user(user_class: Type[BasicUser | ProUser]): ...
774
776
775
777
``Type[Any] `` is equivalent to ``Type `` which in turn is equivalent
776
778
to ``type ``, which is the root of Python's metaclass hierarchy.
@@ -951,7 +953,7 @@ These can be used as types in annotations using ``[]``, each having a unique syn
951
953
conditional code flow and applying the narrowing to a block of code. The
952
954
conditional expression here is sometimes referred to as a "type guard"::
953
955
954
- def is_str(val: Union[ str, float] ):
956
+ def is_str(val: str | float):
955
957
# "isinstance" type guard
956
958
if isinstance(val, str):
957
959
# Type of ``val`` is narrowed to ``str``
@@ -2031,7 +2033,7 @@ Introspection helpers
2031
2033
For a typing object of the form ``X[Y, Z, ...] `` these functions return
2032
2034
``X `` and ``(Y, Z, ...) ``. If ``X `` is a generic alias for a builtin or
2033
2035
:mod: `collections ` class, it gets normalized to the original class.
2034
- If ``X `` is a :class: ` Union ` or :class: `Literal ` contained in another
2036
+ If ``X `` is a union or :class: `Literal ` contained in another
2035
2037
generic type, the order of ``(Y, Z, ...) `` may be different from the order
2036
2038
of the original arguments ``[Y, Z, ...] `` due to type caching.
2037
2039
For unsupported objects return ``None `` and ``() `` correspondin
98D5
gly.
@@ -2056,7 +2058,7 @@ Introspection helpers
2056
2058
year: int
2057
2059
2058
2060
is_typeddict(Film) # => True
2059
- is_typeddict(Union[ list, str] ) # => False
2061
+ is_typeddict(list | str) # => False
2060
2062
2061
2063
.. versionadded :: 3.10
2062
2064
0 commit comments