8000 gh-117492: Clarify documentation of `typing.Never` (#117678) · python/cpython@852263e · GitHub
[go: up one dir, main page]

Skip to content

Commit 852263e

Browse files
nineteendoJelleZijlstraerlend-aasland
authored
gh-117492: Clarify documentation of typing.Never (#117678)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
1 parent f201628 commit 852263e

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

Doc/library/typing.rst

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -852,14 +852,25 @@ using ``[]``.
852852
.. versionadded:: 3.11
853853

854854
.. data:: Never
855+
NoReturn
855856

856-
The `bottom type <https://en.wikipedia.org/wiki/Bottom_type>`_,
857+
:data:`!Never` and :data:`!NoReturn` represent the
858+
`bottom type <https://en.wikipedia.org/wiki/Bottom_type>`_,
857859
a type that has no members.
858860

859-
This can be used to define a function that should never be
860-
called, or a function that never returns::
861+
They can be used to indicate that a function never returns,
862+
such as :func:`sys.exit`::
861863

862-
from typing import Never
864+
from typing import Never # or NoReturn
865+
866+
def stop() -> Never:
867+
raise RuntimeError('no way')
868+
869+
Or to define a function that should never be
870+
called, as there are no valid arguments, such as
871+
:func:`assert_never`::
872+
873+
from typing import Never # or NoReturn
863874

864875
def never_call_me(arg: Never) -> None:
865876
pass
@@ -872,31 +883,18 @@ using ``[]``.
872883
case str():
873884
print("It's a str")
874885
case _:
875-
never_call_me(arg) # OK, arg is of type Never
876-
877-
.. versionadded:: 3.11
878-
879-
On older Python versions, :data:`NoReturn` may be used to express the
880-
same concept. ``Never`` was added to make the intended meaning more explicit.
886+
never_call_me(arg) # OK, arg is of type Never (or NoReturn)
881887

882-
.. data:: NoReturn
888+
:data:`!Never` and :data:`!NoReturn` have the same meaning in the type system
889+
and static type checkers treat both equivalently.
883890

884-
Special type indicating that a function never returns.
885-
886-
For example::
891+
.. versionadded:: 3.6.2
887892

888-
from typing import NoReturn
893+
Added :data:`NoReturn`.
889894

890-
def stop() -> NoReturn:
891-
raise RuntimeError('no way')
892-
893-
``NoReturn`` can also be used as a
894-
`bottom type <https://en.wikipedia.org/wiki/Bottom_type>`_, a type that
895-
has no values. Starting in Python 3.11, the :data:`Never` type should
896-
be used for this concept instead. Type checkers should treat the two
897-
equivalently.
895+
.. versionadded:: 3.11
898896

899-
.. versionadded:: 3.6.2
897+
Added :data:`Never`.
900898

901899
.. data:: Self
902900

0 commit comments

Comments
 (0)
0