Skip to content

Commit 3d0397a

Browse files
committed
Don't merge starting If blocks without overloads
1 parent 80b05d5 commit 3d0397a

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

mypy/fastparse.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,11 @@ def fix_function_overloads(self, stmts: List[Statement]) -> List[Statement]:
486486
if (
487487
isinstance(stmt, IfStmt)
488488
and len(stmt.body[0].body) == 1
489-
and isinstance(stmt.body[0].body[0], (Decorator, FuncDef, OverloadedFuncDef))
489+
and (
490+
isinstance(stmt.body[0].body[0], (Decorator, OverloadedFuncDef))
491+
or current_overload_name is not None
492+
and isinstance(stmt.body[0].body[0], FuncDef)
493+
)
490494
):
491495
# Check IfStmt block to determine if function overloads can be merged
492496
if_overload_name = self._check_ifstmt_for_overloads(stmt)

test-data/unit/check-functions.test

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,9 +1400,8 @@ def top() -> None:
14001400
from typing import Any
14011401
x = None # type: Any
14021402
if x:
1403-
pass # some other node
14041403
def f(): pass
1405-
def f(): pass # E: Name "f" already defined on line 5
1404+
def f(): pass # E: Name "f" already defined on line 4
14061405

14071406
[case testIncompatibleConditionalFunctionDefinition]
14081407
from typing import Any
@@ -1647,9 +1646,8 @@ from typing import Any
16471646
x = None # type: Any
16481647
class A:
16491648
if x:
1650-
pass # Some other node
16511649
def f(self): pass
1652-
def f(self): pass # E: Name "f" already defined on line 6
1650+
def f(self): pass # E: Name "f" already defined on line 5
16531651

16541652
[case testIncompatibleConditionalMethodDefinition]
16551653
from typing import Any

test-data/unit/check-overloading.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6153,11 +6153,11 @@ reveal_type(f5(A())) # N: Revealed type is "__main__.A"
61536153
reveal_type(f5(B())) # N: Revealed type is "__main__.B"
61546154

61556155
# Test from check-functions - testUnconditionalRedefinitionOfConditionalFunction
6156-
# If IfStmt only contains FuncDef, block is ignore if uncertain about execution
6157-
# Necessary to be able to ignore always-false cases
6156+
# Don't merge If blocks if they appear before any overloads
6157+
# and don't contain any overloads themselves.
61586158
if maybe_true: # E: Name "maybe_true" is not defined
61596159
def f6(x): ...
6160-
def f6(x): ...
6160+
def f6(x): ... # E: Name "f6" already defined on line 61
61616161

61626162
if maybe_true: # E: Name "maybe_true" is not defined
61636163
pass # Some other node

0 commit comments

Comments
 (0)