@@ -364,6 +364,43 @@ potentially problematic or redundant in some way.
364
364
This flag causes mypy to generate a warning when returning a value
365
365
with type ``Any `` from a function declared with a non- ``Any `` return type.
366
366
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
+
367
404
Miscellaneous strictness flags
368
405
******************************
369
406
0 commit comments