@@ -321,11 +321,11 @@ not generic but implicitly inherits from ``Iterable[Any]``::
321321User defined generic type aliases are also supported. Examples::
322322
323323 from collections.abc import Iterable
324- from typing import TypeVar, Union
324+ from typing import TypeVar
325325 S = TypeVar('S')
326- Response = Union[ Iterable[S], int]
326+ Response = Iterable[S] | int
327327
328- # Return type here is same as Union[ Iterable[str], int]
328+ # Return type here is same as Iterable[str] | int
329329 def response(query: str) -> Response[str]:
330330 ...
331331
@@ -588,9 +588,9 @@ These can be used as types in annotations using ``[]``, each having a unique syn
588588
589589.. data :: Union
590590
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.
592592
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:
594594
595595 * The arguments must be types and there must be at least one.
596596
@@ -604,18 +604,16 @@ These can be used as types in annotations using ``[]``, each having a unique syn
604604
605605 * Redundant arguments are skipped, e.g.::
606606
607- Union[int, str, int] == Union[int, str]
607+ Union[int, str, int] == Union[int, str] == int | str
608608
609609 * When comparing unions, the argument order is ignored, e.g.::
610610
611611 Union[int, str] == Union[str, int]
612612
613- * You cannot subclass or instantiate a union .
613+ * You cannot subclass or instantiate a `` Union `` .
614614
615615 * You cannot write ``Union[X][Y] ``.
616616
617- * You can use ``Optional[X] `` as a shorthand for ``Union[X, None] ``.
618-
619617 .. versionchanged :: 3.7
620618 Don't remove explicit subclasses from unions at runtime.
621619
@@ -627,7 +625,7 @@ These can be used as types in annotations using ``[]``, each having a unique syn
627625
628626 Optional type.
629627
630- ``Optional[X] `` is equivalent to ``Union[X, None] ``.
628+ ``Optional[X] `` is equivalent to ``X | None `` (or `` Union[X, None] ``) .
631629
632630 Note that this is not the same concept as an optional argument,
633631 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
644642 def foo(arg: Optional[int] = None) -> None:
645643 ...
646644
645+ .. versionchanged :: 3.10
646+ Optional can now be written as ``X | None ``. See
647+ :ref: `union type expressions<types-union> `.
648+
647649.. data :: Callable
648650
649651 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
770772 :ref: `type variables <generics >`, and unions of any of these types.
771773 For example::
772774
773- def new_non_team_user(user_class: Type[Union[ BasicUser, ProUser] ]): ...
775+ def new_non_team_user(user_class: Type[BasicUser | ProUser]): ...
774776
775777 ``Type[Any] `` is equivalent to ``Type `` which in turn is equivalent
776778 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
951953 conditional code flow and applying the narrowing to a block of code. The
952954 conditional expression here is sometimes referred to as a "type guard"::
953955
954- def is_str(val: Union[ str, float] ):
956+ def is_str(val: str | float):
955957 # "isinstance" type guard
956958 if isinstance(val, str):
957959 # Type of ``val`` is narrowed to ``str``
@@ -2031,7 +2033,7 @@ Introspection helpers
20312033 For a typing object of the form ``X[Y, Z, ...] `` these functions return
20322034 ``X `` and ``(Y, Z, ...) ``. If ``X `` is a generic alias for a builtin or
20332035 :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
20352037 generic type, the order of ``(Y, Z, ...) `` may be different from the order
20362038 of the original arguments ``[Y, Z, ...] `` due to type caching.
20372039 For unsupported objects return ``None `` and ``() `` correspondingly.
@@ -2056,7 +2058,7 @@ Introspection helpers
20562058 year: int
20572059
20582060 is_typeddict(Film) # => True
2059- is_typeddict(Union[ list, str] ) # => False
2061+ is_typeddict(list | str) # => False
20602062
20612063 .. versionadded :: 3.10
20622064
0 commit comments