summaryrefslogtreecommitdiffstats
path: root/Doc/library
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2022-02-08 18:50:26 (GMT)
committerGitHub <noreply@github.com>2022-02-08 18:50:26 (GMT)
commit243436f3779c1e7bed1fd4b23d5a8ec5eff40699 (patch)
tree141646bc73b79f45ff8d875b45164f1b90a8b2eb /Doc/library
parent1e6214dbd6a980b47123229aefd60bb2c9341b53 (diff)
downloadcpython-243436f3779c1e7bed1fd4b23d5a8ec5eff40699.zip
cpython-243436f3779c1e7bed1fd4b23d5a8ec5eff40699.tar.gz
cpython-243436f3779c1e7bed1fd4b23d5a8ec5eff40699.tar.bz2
bpo-46475: Add typing.Never and typing.assert_never (GH-30842)
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/typing.rst56
1 files changed, 56 insertions, 0 deletions
diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst
index 8c1c34e..94a46b0 100644
--- a/Doc/library/typing.rst
+++ b/Doc/library/typing.rst
@@ -574,6 +574,34 @@ These can be used as types in annotations and do not support ``[]``.
* Every type is compatible with :data:`Any`.
* :data:`Any` is compatible with every type.
+.. data:: Never
+
+ The `bottom type <https://en.wikipedia.org/wiki/Bottom_type>`_,
+ a type that has no members.
+
+ This can be used to define a function that should never be
+ called, or a function that never returns::
+
+ from typing import Never
+
+ def never_call_me(arg: Never) -> None:
+ pass
+
+ def int_or_str(arg: int | str) -> None:
+ never_call_me(arg) # type checker error
+ match arg:
+ case int():
+ print("It's an int")
+ case str():
+ print("It's a str")
+ case _:
+ never_call_me(arg) # ok, arg is of type Never
+
+ .. versionadded:: 3.11
+
+ On older Python versions, :data:`NoReturn` may be used to express the
+ same concept. ``Never`` was added to make the intended meaning more explicit.
+
.. data:: NoReturn
Special type indicating that a function never returns.
@@ -584,6 +612,12 @@ These can be used as types in annotations and do not support ``[]``.
def stop() -> NoReturn:
raise RuntimeError('no way')
+ ``NoReturn`` can also be used as a
+ `bottom type <https://en.wikipedia.org/wiki/Bottom_type>`_, a type that
+ has no values. Starting in Python 3.11, the :data:`Never` type should
+ be used for this concept instead. Type checkers should treat the two
+ equivalently.
+
.. versionadded:: 3.5.4
.. versionadded:: 3.6.2
@@ -1979,6 +2013,28 @@ Functions and decorators
runtime we intentionally don't check anything (we want this
to be as fast as possible).
+.. function:: assert_never(arg, /)
+
+ Assert to the type checker that a line of code is unreachable.
+
+ Example::
+
+ def int_or_str(arg: int | str) -> None:
+ match arg:
+ case int():
+ print("It's an int")
+ case str():
+ print("It's a str")
+ case _ as unreachable:
+ assert_never(unreachable)
+
+ If a type checker finds that a call to ``assert_never()`` is
+ reachable, it will emit an error.
+
+ At runtime, this throws an exception when called.
+
+ .. versionadded:: 3.11
+
.. function:: reveal_type(obj)
Reveal the inferred static type of an expression.