Skip to content

Commit 626147a

Browse files
cdce8pJukkaL
authored andcommitted
Fix small conditional overload regression (#12336)
Don't merge conditional FuncDef after an unconditional one.
1 parent 8e9ac15 commit 626147a

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

mypy/fastparse.py

+5
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ def fix_function_overloads(self, stmts: List[Statement]) -> List[Statement]:
486486
ret: List[Statement] = []
487487
current_overload: List[OverloadPart] = []
488488
current_overload_name: Optional[str] = None
489+
seen_unconditional_func_def = False
489490
last_if_stmt: Optional[IfStmt] = None
490491
last_if_overload: Optional[Union[Decorator, FuncDef, OverloadedFuncDef]] = None
491492
last_if_stmt_overload_name: Optional[str] = None
@@ -498,6 +499,7 @@ def fix_function_overloads(self, stmts: List[Statement]) -> List[Statement]:
498499
if (
499500
isinstance(stmt, IfStmt)
500501
and len(stmt.body[0].body) == 1
502+
and seen_unconditional_func_def is False
501503
and (
502504
isinstance(stmt.body[0].body[0], (Decorator, OverloadedFuncDef))
503505
or current_overload_name is not None
@@ -527,6 +529,8 @@ def fix_function_overloads(self, stmts: List[Statement]) -> List[Statement]:
527529
self.fail_merge_overload(last_if_unknown_truth_value)
528530
last_if_unknown_truth_value = None
529531
current_overload.append(stmt)
532+
if isinstance(stmt, FuncDef):
533+
seen_unconditional_func_def = True
530534
elif (
531535
current_overload_name is not None
532536
and isinstance(stmt, IfStmt)
@@ -583,6 +587,7 @@ def fix_function_overloads(self, stmts: List[Statement]) -> List[Statement]:
583587
# most of mypy/mypyc assumes that all the functions in an OverloadedFuncDef are
584588
# related, but multiple underscore functions next to each other aren't necessarily
585589
# related
590+
seen_unconditional_func_def = False
586591
if isinstance(stmt, Decorator) and not unnamed_function(stmt.name):
587592
current_overload = [stmt]
588593
current_overload_name = stmt.name

test-data/unit/check-overloading.test

+26
Original file line numberDiff line numberDiff line change
@@ -6302,3 +6302,29 @@ if True:
63026302
def f12(x): ...
63036303
reveal_type(f12(A())) # N: Revealed type is "__main__.A"
63046304
[typing fixtures/typing-medium.pyi]
6305+
6306+
[case testOverloadIfUnconditionalFuncDef]
6307+
# flags: --always-true True --always-false False
6308+
from typing import overload
6309+
6310+
class A: ...
6311+
class B: ...
6312+
6313+
# -----
6314+
# Don't merge conditional FuncDef after unconditional one
6315+
# -----
6316+
6317+
@overload
6318+
def f1(x: A) -> A: ...
6319+
@overload
6320+
def f1(x: B) -> B: ...
6321+
def f1(x): ...
6322+
6323+
@overload
6324+
def f2(x: A) -> A: ...
6325+
if True:
6326+
@overload
6327+
def f2(x: B) -> B: ...
6328+
def f2(x): ...
6329+
if True:
6330+
def f2(x): ... # E: Name "f2" already defined on line 17

0 commit comments

Comments
 (0)