Skip to content

Commit a7f495c

Browse files
miss-islingtonJelleZijlstranineteendoerlend-aasland
authored
[3.12] gh-117492: Clarify documentation of typing.Never (GH-117678) (#118547)
(cherry picked from commit 852263e) Co-authored-by: Jelle Zijlstra <[email protected]> Co-authored-by: Nice Zombies <[email protected]> Co-authored-by: Erlend E. Aasland <[email protected]>
1 parent 23ba96e commit a7f495c

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
@@ -842,14 +842,25 @@ using ``[]``.
842842
.. versionadded:: 3.11
843843

844844
.. data:: Never
845+
NoReturn
845846

846-
The `bottom type <https://en.wikipedia.org/wiki/Bottom_type>`_,
847+
:data:`!Never` and :data:`!NoReturn` represent the
848+
`bottom type <https://en.wikipedia.org/wiki/Bottom_type>`_,
847849
a type that has no members.
848850

849-
This can be used to define a function that should never be
850-
called, or a function that never returns::
851+
They can be used to indicate that a function never returns,
852+
such as :func:`sys.exit`::
851853

852-
from typing import Never
854+
from typing import Never # or NoReturn
855+
856+
def stop() -> Never:
857+
raise RuntimeError('no way')
858+
859+
Or to define a function that should never be
860+
called, as there are no valid arguments, such as
861+
:func:`assert_never`::
862+
863+
from typing import Never # or NoReturn
853864

854865
def never_call_me(arg: Never) -> None:
855866
pass
@@ -862,31 +873,18 @@ using ``[]``.
862873
case str():
863874
print("It's a str")
864875
case _:
865-
never_call_me(arg) # OK, arg is of type Never
866-
867-
.. versionadded:: 3.11
868-
869-
On older Python versions, :data:`NoReturn` may be used to express the
870-
same concept. ``Never`` was added to make the intended meaning more explicit.
876+
never_call_me(arg) # OK, arg is of type Never (or NoReturn)
871877

872-
.. data:: NoReturn
878+
:data:`!Never` and :data:`!NoReturn` have the same meaning in the type system
879+
and static type checkers treat both equivalently.
873880

874-
Special type indicating that a function never returns.
875-
876-
For example::
881+
.. versionadded:: 3.6.2
877882

878-
from typing import NoReturn
883+
Added :data:`NoReturn`.
879884

880-
def stop() -> NoReturn:
881-
raise RuntimeError('no way')
882-
883-
``NoReturn`` can also be used as a
884-
`bottom type <https://en.wikipedia.org/wiki/Bottom_type>`_, a type that
885-
has no values. Starting in Python 3.11, the :data:`Never` type should
886-
be used for this concept instead. Type checkers should treat the two
887-
equivalently.
885+
.. versionadded:: 3.11
888886

889-
.. versionadded:: 3.6.2
887+
Added :data:`Never`.
890888

891889
.. data:: Self
892890

0 commit comments

Comments
 (0)