@@ -842,14 +842,25 @@ using ``[]``.
842
842
.. versionadded :: 3.11
843
843
844
844
.. data :: Never
845
+ NoReturn
845
846
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 >`_,
847
849
a type that has no members.
848
850
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 ` ::
851
853
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
853
864
854
865
def never_call_me(arg: Never) -> None:
855
866
pass
@@ -862,31 +873,18 @@ using ``[]``.
862
873
case str():
863
874
print("It's a str")
864
875
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)
871
877
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.
873
880
874
- Special type indicating that a function never returns.
875
-
876
- For example::
881
+ .. versionadded :: 3.6.2
877
882
878
- from typing import NoReturn
883
+ Added :data: ` NoReturn `.
879
884
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
888
886
889
- .. versionadded :: 3.6.2
887
+ Added :data: ` Never `.
890
888
891
889
.. data :: Self
892
890
0 commit comments