Skip to content

Add new error code for unreachable statement #8312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/source/error_code_list2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,22 @@ that ``Cat`` falls back to ``Any`` in a type annotation:
# Error: Argument 1 to "feed" becomes "Any" due to an unfollowed import [no-any-unimported]
def feed(cat: Cat) -> None:
...

Check that statement or expression is unreachable [unreachable]
---------------------------------------------------------------

If you use :option:`--warn-unreachable <mypy --warn-unreachable>`, mypy generates an error if it
thinks that a statement or expression will never be executed. In most cases, this is due to
incorrect control flow or conditional checks that are accidentally always true or false.

.. code-block:: python

# mypy: warn-unreachable

def example(x: int) -> None:
# Error: Right operand of 'or' is never evaluated [unreachable]
assert isinstance(x, int) or x == 'unused'

return
# Error: Statement is unreachable [unreachable]
print('unreachable')
2 changes: 2 additions & 0 deletions mypy/errorcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ def __str__(self) -> str:
NO_ANY_RETURN = ErrorCode(
'no-any-return', 'Reject returning value with "Any" type if return type is not "Any"',
'General') # type: Final
UNREACHABLE = ErrorCode(
'unreachable', "Warn about unreachable statements or expressions", 'General') # type: Final

# Syntax errors are often blocking.
SYNTAX = ErrorCode(
Expand Down
8 changes: 5 additions & 3 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ def note_call(self,
context, code=code)

def unreachable_statement(self, context: Context) -> None:
self.fail("Statement is unreachable", context)
self.fail("Statement is unreachable", context, code=codes.UNREACHABLE)

def redundant_left_operand(self, op_name: str, context: Context) -> None:
"""Indicates that the left operand of a boolean expression is redundant:
Expand All @@ -1249,7 +1249,8 @@ def redundant_right_operand(self, op_name: str, context: Context) -> None:
it does not change the truth value of the entire condition as a whole.
'op_name' should either be the string "and" or the string "or".
"""
self.fail("Right operand of '{}' is never evaluated".format(op_name), context)
self.fail("Right operand of '{}' is never evaluated".format(op_name),
context, code=codes.UNREACHABLE)

def redundant_condition_in_comprehension(self, truthiness: bool, context: Context) -> None:
self.redundant_expr("If condition in comprehension", truthiness, context)
Expand All @@ -1261,7 +1262,8 @@ def redundant_condition_in_assert(self, truthiness: bool, context: Context) -> N
self.redundant_expr("Condition in assert", truthiness, context)

def redundant_expr(self, description: str, truthiness: bool, context: Context) -> None:
self.fail("{} is always {}".format(description, str(truthiness).lower()), context)
self.fail("{} is always {}".format(description, str(truthiness).lower()),
context, code=codes.UNREACHABLE)

def report_protocol_problems(self,
subtype: Union[Instance, TupleType, TypedDictType],
Expand Down