Skip to content
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
4 changes: 3 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3147,6 +3147,8 @@ def is_noop_for_reachability(self, s: Statement) -> bool:
"""
if isinstance(s, AssertStmt) and is_false_literal(s.expr):
return True
elif isinstance(s, ReturnStmt) and is_literal_not_implemented(s.expr):
return True
elif isinstance(s, (RaiseStmt, PassStmt)):
return True
elif isinstance(s, ExpressionStmt):
Expand Down Expand Up @@ -8281,7 +8283,7 @@ def is_literal_none(n: Expression) -> bool:
return isinstance(n, NameExpr) and n.fullname == "builtins.None"


def is_literal_not_implemented(n: Expression) -> bool:
def is_literal_not_implemented(n: Expression | None) -> bool:
return isinstance(n, NameExpr) and n.fullname == "builtins.NotImplemented"


Expand Down
20 changes: 20 additions & 0 deletions test-data/unit/check-unreachable-code.test
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,26 @@ reveal_type(bar().attr) # N: Revealed type is "Never"
reveal_type(foo().attr) # N: Revealed type is "Never"
1 # E: Statement is unreachable

[case testIgnoreReturningNotImplemented]
# flags: --warn-unreachable

class C:
def __add__(self, o: C) -> C:
if not isinstance(o, C):
return NotImplemented
return C()
def __sub__(self, o: C) -> C:
if isinstance(o, C):
return C()
return NotImplemented
def __mul__(self, o: C) -> C:
if isinstance(o, C):
return C()
else:
return NotImplemented

[builtins fixtures/isinstance.pyi]

[case testUnreachableStatementPrettyHighlighting]
# flags: --warn-unreachable --pretty
def x() -> None:
Expand Down