@@ -852,14 +852,25 @@ using ``[]``.
852
852
.. versionadded :: 3.11
853
853
854
854
.. data :: Never
855
+ NoReturn
855
856
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 >`_,
857
859
a type that has no members.
858
860
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 ` ::
861
863
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
863
874
864
875
def never_call_me(arg: Never) -> None:
865
876
pass
@@ -872,31 +883,18 @@ using ``[]``.
872
883
case str():
873
884
print("It's a str")
874
885
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)
881
887
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.
883
890
884
- Special type indicating that a function never returns.
885
-
886
- For example::
891
+ .. versionadded :: 3.6.2
887
892
888
- from typing import NoReturn
893
+ Added :data: ` NoReturn `.
889
894
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
898
896
899
- .. versionadded :: 3.6.2
897
+ Added :data: ` Never `.
900
898
901
899
.. data :: Self
902
900
0 commit comments