From 9285ac8c3b4f58a358ebb66f0d5c371dc506f51a Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Mon, 27 Feb 2017 23:47:24 +0100 Subject: [PATCH 1/4] Document that --war-no-return is on by default --- docs/source/command_line.rst | 1 + docs/source/config_file.rst | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/source/command_line.rst b/docs/source/command_line.rst index 3fd3339a1d09f..be7b2f16bcb85 100644 --- a/docs/source/command_line.rst +++ b/docs/source/command_line.rst @@ -365,6 +365,7 @@ Here are some more useful flags: for functions with ``None`` or ``Any`` return types. Mypy also currently ignores functions with an empty body or a body that is just ellipsis (``...``), since these can be valid as abstract methods. + This option is on by default. - ``--warn-return-any`` causes mypy to generate a warning when returning a value with type ``Any`` from a function declared with a non- ``Any`` return type. diff --git a/docs/source/config_file.rst b/docs/source/config_file.rst index 56f44249c6afd..613b716487f3c 100644 --- a/docs/source/config_file.rst +++ b/docs/source/config_file.rst @@ -165,7 +165,7 @@ overridden by the pattern sections matching the module name. - ``ignore_errors`` (Boolean, default False) ignores all non-fatal errors. -- ``warn_no_return`` (Boolean, default False) shows errors for +- ``warn_no_return`` (Boolean, default True) shows errors for missing return statements on some execution paths. - ``warn_return_any`` (Boolean, default False) shows a warning when From 239d1e6d2e85e74f0763a86856beeef668d3e0d6 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Tue, 28 Feb 2017 00:25:59 +0100 Subject: [PATCH 2/4] Document NoReturn --- docs/source/kinds_of_types.rst | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/docs/source/kinds_of_types.rst b/docs/source/kinds_of_types.rst index dc639d910d6c8..87e09d36c1a98 100644 --- a/docs/source/kinds_of_types.rst +++ b/docs/source/kinds_of_types.rst @@ -219,6 +219,43 @@ narrow down the type to a specific type: f('x') # OK f(1.1) # Error +.. _noreturn: + +The NoReturn type +***************** + +Mypy provides an experimental support for functions that supposed to never +return, such as ``sys.exit()``. For example, a function that raises +an exception: + +.. code-block:: python + + from mypy_extensions import NoReturn + + def stop() -> NoReturn: + raise Exception('no way') + +Mypy will recognize that the code after such function is unreachable and will +behave accordingly: + +.. code-block:: python + + def f(x: int) -> int: + if x == 0: + return x + stop() + return 'whatever works' # No error in an unreachable block + +Note that ``NoReturn`` annotation is different from just not returning +a value from function, the latter simply means that an implicit ``None`` +is returned. Mypy will make a note "Implicit return in function which +does not return" in cases like this: + +.. code-block:: python + + def f() -> NoReturn: + non_trivial_function = True + .. _optional: The type of None and optional types From b656cc36ceb4a2e142f9ca002c75800aad67c7af Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Tue, 28 Feb 2017 00:33:57 +0100 Subject: [PATCH 3/4] Move NoReturn section after None/Optional section --- docs/source/kinds_of_types.rst | 74 +++++++++++++++++----------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/docs/source/kinds_of_types.rst b/docs/source/kinds_of_types.rst index 87e09d36c1a98..00dd7000f42e6 100644 --- a/docs/source/kinds_of_types.rst +++ b/docs/source/kinds_of_types.rst @@ -219,43 +219,6 @@ narrow down the type to a specific type: f('x') # OK f(1.1) # Error -.. _noreturn: - -The NoReturn type -***************** - -Mypy provides an experimental support for functions that supposed to never -return, such as ``sys.exit()``. For example, a function that raises -an exception: - -.. code-block:: python - - from mypy_extensions import NoReturn - - def stop() -> NoReturn: - raise Exception('no way') - -Mypy will recognize that the code after such function is unreachable and will -behave accordingly: - -.. code-block:: python - - def f(x: int) -> int: - if x == 0: - return x - stop() - return 'whatever works' # No error in an unreachable block - -Note that ``NoReturn`` annotation is different from just not returning -a value from function, the latter simply means that an implicit ``None`` -is returned. Mypy will make a note "Implicit return in function which -does not return" in cases like this: - -.. code-block:: python - - def f() -> NoReturn: - non_trivial_function = True - .. _optional: The type of None and optional types @@ -393,6 +356,43 @@ check against ``None`` in the if condition. ``--strict-optional`` is experimental and still has known issues. +.. _noreturn: + +The NoReturn type +***************** + +Mypy also provides an experimental support for functions that supposed to +never return, such as ``sys.exit()``. For example, a function that raises +an exception: + +.. code-block:: python + + from mypy_extensions import NoReturn + + def stop() -> NoReturn: + raise Exception('no way') + +Mypy will recognize that the code after such function is unreachable and will +behave accordingly: + +.. code-block:: python + + def f(x: int) -> int: + if x == 0: + return x + stop() + return 'whatever works' # No error in an unreachable block + +Note that ``NoReturn`` annotation is different from just not returning +a value from function, the latter simply means that an implicit ``None`` +is returned. Mypy will make a note "Implicit return in function which +does not return" in cases like this: + +.. code-block:: python + + def f() -> NoReturn: + non_trivial_function = True + Class name forward references ***************************** From 6cc525ffa184d76e6f3ead9a3e77d95a715b8f4e Mon Sep 17 00:00:00 2001 From: David Fisher Date: Mon, 27 Feb 2017 16:58:04 -0800 Subject: [PATCH 4/4] Update kinds_of_types.rst --- docs/source/kinds_of_types.rst | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/docs/source/kinds_of_types.rst b/docs/source/kinds_of_types.rst index 00dd7000f42e6..8aff044a367e4 100644 --- a/docs/source/kinds_of_types.rst +++ b/docs/source/kinds_of_types.rst @@ -361,9 +361,8 @@ check against ``None`` in the if condition. The NoReturn type ***************** -Mypy also provides an experimental support for functions that supposed to -never return, such as ``sys.exit()``. For example, a function that raises -an exception: +Mypy provides support for functions that never return. For +example, a function that unconditionally raises an exception: .. code-block:: python @@ -372,8 +371,10 @@ an exception: def stop() -> NoReturn: raise Exception('no way') -Mypy will recognize that the code after such function is unreachable and will -behave accordingly: +Mypy will ensure that functions annotated as returning ``NoReturn`` +truly never return, either implicitly or explicitly. Mypy will also +recognize that the code after calls to such functions is unreachable +and will behave accordingly: .. code-block:: python @@ -383,15 +384,6 @@ behave accordingly: stop() return 'whatever works' # No error in an unreachable block -Note that ``NoReturn`` annotation is different from just not returning -a value from function, the latter simply means that an implicit ``None`` -is returned. Mypy will make a note "Implicit return in function which -does not return" in cases like this: - -.. code-block:: python - - def f() -> NoReturn: - non_trivial_function = True Class name forward references *****************************