Skip to content

Commit 5c4c4b1

Browse files
committed
Do not emit unreachable warnings for lines that return NotImplemented.
1 parent 37333c7 commit 5c4c4b1

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

mypy/checker.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3147,6 +3147,8 @@ def is_noop_for_reachability(self, s: Statement) -> bool:
31473147
"""
31483148
if isinstance(s, AssertStmt) and is_false_literal(s.expr):
31493149
return True
3150+
elif isinstance(s, ReturnStmt) and is_literal_not_implemented(s.expr):
3151+
return True
31503152
elif isinstance(s, (RaiseStmt, PassStmt)):
31513153
return True
31523154
elif isinstance(s, ExpressionStmt):

test-data/unit/check-unreachable-code.test

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,26 @@ reveal_type(bar().attr) # N: Revealed type is "Never"
16201620
reveal_type(foo().attr) # N: Revealed type is "Never"
16211621
1 # E: Statement is unreachable
16221622

1623+
[case testIgnoreReturningNotImplemented]
1624+
# flags: --warn-unreachable
1625+
1626+
class C:
1627+
def __add__(self, o: C) -> C:
1628+
if not isinstance(o, C):
1629+
return NotImplemented
1630+
return C()
1631+
def __sub__(self, o: C) -> C:
1632+
if isinstance(o, C):
1633+
return C()
1634+
return NotImplemented
1635+
def __mul__(self, o: C) -> C:
1636+
if isinstance(o, C):
1637+
return C()
1638+
else:
1639+
return NotImplemented
1640+
1641+
[builtins fixtures/isinstance.pyi]
1642+
16231643
[case testUnreachableStatementPrettyHighlighting]
16241644
# flags: --warn-unreachable --pretty
16251645
def x() -> None:

0 commit comments

Comments
 (0)