Skip to content

Commit a514ba2

Browse files
Michael0x2ailevkivskyi
authored andcommitted
Add docs for the '--warn-unreachable' flag (#7170)
This pull request adds a segment about the '--warn-unreachable' flag to the command line docs page and a shorter blurb to the config files page.
1 parent a8e664a commit a514ba2

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

docs/source/command_line.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,43 @@ potentially problematic or redundant in some way.
364364
This flag causes mypy to generate a warning when returning a value
365365
with type ``Any`` from a function declared with a non- ``Any`` return type.
366366

367+
``--warn-unreachable``
368+
This flag will make mypy report an error whenever it encounters
369+
code determined to be unreachable or redundant after performing type analysis.
370+
This can be a helpful way of detecting certain kinds of bugs in your code.
371+
372+
For example, enabling this flag will make mypy report that the ``x > 7``
373+
check is redundant and that the ``else`` block below is unreachable.
374+
375+
.. code-block:: python
376+
377+
def process(x: int) -> None:
378+
# Error: Right operand of 'or' is never evaluated
379+
if isinstance(x, int) or x > 7:
380+
# Error: Unsupported operand types for + ("int" and "str")
381+
print(x + "bad")
382+
else:
383+
# Error: 'Statement is unreachable' error
384+
print(x + "bad")
385+
386+
To help prevent mypy from generating spurious warnings, the "Statement is
387+
unreachable" warning will be silenced in exactly two cases:
388+
389+
1. When the unreachable statement is a ``raise`` statement, is an
390+
``assert False`` statement, or calls a function that has the ``NoReturn``
391+
return type hint. In other words, when the unreachable statement
392+
throws an error or terminates the program in some way.
393+
2. When the unreachable statement was *intentionally* marked as unreachable
394+
using :ref:`version_and_platform_checks`.
395+
396+
.. note::
397+
398+
Mypy currently cannot detect and report unreachable or redundant code
399+
inside any functions using :ref:`type-variable-value-restriction`.
400+
401+
This limitation will be removed in future releases of mypy.
402+
403+
367404
Miscellaneous strictness flags
368405
******************************
369406

docs/source/config_file.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ section of the command line docs.
277277
Shows a warning when returning a value with type ``Any`` from a function
278278
declared with a non- ``Any`` return type.
279279

280+
``warn_unreachable`` (bool, default False)
281+
Shows a warning when encountering any code inferred to be unreachable or
282+
redundant after performing type analysis.
283+
280284
.. _config-file-suppressing-errors:
281285

282286
Suppressing errors

0 commit comments

Comments
 (0)