Skip to content

Commit 3f81076

Browse files
committed
Use shorthand for Union and Optional
1 parent 6b5d8b3 commit 3f81076

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

docs/guides/libraries.rst

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ is obvious from the context:
239239
that is defined at a module level with a single assignment where the
240240
assigned value is an instantiable type, as opposed to a class
241241
instance
242-
(e.g. ``Foo = Callable[[Literal["a", "b"]], Union[int, str]]`` or
243-
``Bar = Optional[MyGenericClass[int]]``).
242+
(e.g. ``Foo = Callable[[Literal["a", "b"]], int | str]`` or
243+
``Bar = MyGenericClass[int] | None``).
244244
- The “self” parameter in an instance method and the “cls” parameter in
245245
a class method do not require an explicit annotation.
246246
- The return type for an ``__init__`` method does not need to be
@@ -266,17 +266,17 @@ Examples of known and unknown types
266266
267267
# Type alias with partially unknown type (because type
268268
# arguments are missing for list and dict)
269-
DictOrList = Union[list, dict]
269+
DictOrList = list | dict
270270
271271
# Type alias with known type
272-
DictOrList = Union[list[Any], dict[str, Any]]
272+
DictOrList = list[Any] | dict[str, Any]
273273
274274
# Generic type alias with known type
275275
_T = TypeVar("_T")
276-
DictOrList = Union[list[_T], dict[str, _T]]
276+
DictOrList = list[_T] | dict[str, _T]
277277
278278
# Function with known type
279-
def func(a: Optional[int], b: dict[str, float] = {}) -> None:
279+
def func(a: int | None, b: dict[str, float] = {}) -> None:
280280
pass
281281
282282
# Function with partially unknown type (because type annotations
@@ -386,10 +386,10 @@ instead of mutable forms (unless the function needs to modify the
386386
container). Use ``Sequence`` rather than ``list``, ``Mapping`` rather
387387
than ``dict``, etc. Immutable containers allow for more flexibility
388388
because their type parameters are covariant rather than invariant. A
389-
parameter that is typed as ``Sequence[Union[str, int]]`` can accept a
389+
parameter that is typed as ``Sequence[str | int]`` can accept a
390390
``list[int]``, ``Sequence[str]``, and a ``Sequence[int]``. But a
391-
parameter typed as ``list[Union[str, int]]`` is much more restrictive
392-
and accepts only a ``list[Union[str, int]]``.
391+
parameter typed as ``list[str | int]`` is much more restrictive
392+
and accepts only a ``list[str | int]``.
393393

394394
Overloads
395395
---------
@@ -409,7 +409,7 @@ specified only by name, use the keyword-only separator (``*``).
409409

410410
.. code:: python
411411
412-
def create_user(age: int, *, dob: Optional[date] = None):
412+
def create_user(age: int, *, dob: date | None = None):
413413
...
414414
415415
.. _annotating-decorators:
@@ -534,16 +534,16 @@ annotation.
534534
.. code:: python
535535
536536
# Simple type alias
537-
FamilyPet = Union[Cat, Dog, GoldFish]
537+
FamilyPet = Cat | Dog | GoldFish
538538
539539
# Generic type alias
540-
ListOrTuple = Union[list[_T], tuple[_T, ...]]
540+
ListOrTuple = list[_T] | tuple[_T, ...]
541541
542542
# Recursive type alias
543-
TreeNode = Union[LeafNode, list["TreeNode"]]
543+
TreeNode = LeafNode | list["TreeNode"]
544544
545545
# Explicit type alias using PEP 613 syntax
546-
StrOrInt: TypeAlias = Union[str, int]
546+
StrOrInt: TypeAlias = str | int
547547
548548
Abstract Classes and Methods
549549
----------------------------

0 commit comments

Comments
 (0)