Skip to content

Commit

Permalink
Fix small conditional overload regression (#12336)
Browse files Browse the repository at this point in the history
Don't merge conditional FuncDef
after an unconditional one.
  • Loading branch information
cdce8p authored and JukkaL committed Mar 21, 2022
1 parent 8e9ac15 commit 626147a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ def fix_function_overloads(self, stmts: List[Statement]) -> List[Statement]:
ret: List[Statement] = []
current_overload: List[OverloadPart] = []
current_overload_name: Optional[str] = None
seen_unconditional_func_def = False
last_if_stmt: Optional[IfStmt] = None
last_if_overload: Optional[Union[Decorator, FuncDef, OverloadedFuncDef]] = None
last_if_stmt_overload_name: Optional[str] = None
Expand All @@ -498,6 +499,7 @@ def fix_function_overloads(self, stmts: List[Statement]) -> List[Statement]:
if (
isinstance(stmt, IfStmt)
and len(stmt.body[0].body) == 1
and seen_unconditional_func_def is False
and (
isinstance(stmt.body[0].body[0], (Decorator, OverloadedFuncDef))
or current_overload_name is not None
Expand Down Expand Up @@ -527,6 +529,8 @@ def fix_function_overloads(self, stmts: List[Statement]) -> List[Statement]:
self.fail_merge_overload(last_if_unknown_truth_value)
last_if_unknown_truth_value = None
current_overload.append(stmt)
if isinstance(stmt, FuncDef):
seen_unconditional_func_def = True
elif (
current_overload_name is not None
and isinstance(stmt, IfStmt)
Expand Down Expand Up @@ -583,6 +587,7 @@ def fix_function_overloads(self, stmts: List[Statement]) -> List[Statement]:
# most of mypy/mypyc assumes that all the functions in an OverloadedFuncDef are
# related, but multiple underscore functions next to each other aren't necessarily
# related
seen_unconditional_func_def = False
if isinstance(stmt, Decorator) and not unnamed_function(stmt.name):
current_overload = [stmt]
current_overload_name = stmt.name
Expand Down
26 changes: 26 additions & 0 deletions test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -6302,3 +6302,29 @@ if True:
def f12(x): ...
reveal_type(f12(A())) # N: Revealed type is "__main__.A"
[typing fixtures/typing-medium.pyi]

[case testOverloadIfUnconditionalFuncDef]
# flags: --always-true True --always-false False
from typing import overload

class A: ...
class B: ...

# -----
# Don't merge conditional FuncDef after unconditional one
# -----

@overload
def f1(x: A) -> A: ...
@overload
def f1(x: B) -> B: ...
def f1(x): ...

@overload
def f2(x: A) -> A: ...
if True:
@overload
def f2(x: B) -> B: ...
def f2(x): ...
if True:
def f2(x): ... # E: Name "f2" already defined on line 17

0 comments on commit 626147a

Please sign in to comment.