Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c2a82b9

Browse files
authored
Do not emit unreachable warnings for lines that return NotImplemented. (#20083)
I think no one has complained so far. I just encountered this (in my understanding) lack in `TypeChecker.is_noop_for_reachability` working on #20068.
1 parent 80d0066 commit c2a82b9

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

mypy/checker.py

Lines changed: 3 additions & 1 deletion
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):
@@ -8281,7 +8283,7 @@ def is_literal_none(n: Expression) -> bool:
82818283
return isinstance(n, NameExpr) and n.fullname == "builtins.None"
82828284

82838285

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

82878289

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)