Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc2a82b9

Browse files
authored
Do not emit unreachable warnings for lines that returnNotImplemented. (#20083)
I think no one has complained so far. I just encountered this (in myunderstanding) lack in `TypeChecker.is_noop_for_reachability` working on#20068.
1 parent80d0066 commitc2a82b9

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
ifisinstance(s,AssertStmt)andis_false_literal(s.expr):
31493149
returnTrue
3150+
elifisinstance(s,ReturnStmt)andis_literal_not_implemented(s.expr):
3151+
returnTrue
31503152
elifisinstance(s, (RaiseStmt,PassStmt)):
31513153
returnTrue
31523154
elifisinstance(s,ExpressionStmt):
@@ -8281,7 +8283,7 @@ def is_literal_none(n: Expression) -> bool:
82818283
returnisinstance(n,NameExpr)andn.fullname=="builtins.None"
82828284

82838285

8284-
defis_literal_not_implemented(n:Expression)->bool:
8286+
defis_literal_not_implemented(n:Expression|None)->bool:
82858287
returnisinstance(n,NameExpr)andn.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)

[8]ページ先頭

©2009-2025 Movatter.jp