Skip to content

Commit 4bcc85c

Browse files
committed
Bugfix
1 parent 84e6c9b commit 4bcc85c

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

mypy/fastparse.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,8 @@ def fix_function_overloads(self, stmts: List[Statement]) -> List[Statement]:
461461
current_overload_name is not None
462462
and isinstance(stmt, IfStmt)
463463
and len(stmt.body[0].body) == 1
464+
and isinstance(stmt.else_body, Block)
465+
and len(stmt.else_body.body) == 0
464466
and isinstance(
465467
stmt.body[0].body[0], (Decorator, FuncDef, OverloadedFuncDef))
466468
and stmt.body[0].body[0].name == current_overload_name
@@ -518,6 +520,8 @@ def fix_function_overloads(self, stmts: List[Statement]) -> List[Statement]:
518520
ret.append(current_overload[0])
519521
elif len(current_overload) > 1:
520522
ret.append(OverloadedFuncDef(current_overload))
523+
elif last_if_stmt is not None:
524+
ret.append(last_if_stmt)
521525
return ret
522526

523527
def in_method_scope(self) -> bool:

test-data/unit/check-overloading.test

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5330,3 +5330,34 @@ def f2(g: Any) -> Any: ...
53305330
reveal_type(f2(42)) # N: Revealed type is "__main__.A"
53315331
reveal_type(f2("Hello")) # N: Revealed type is "__main__.A" \
53325332
# E: Argument 1 to "f2" has incompatible type "str"; expected "int"
5333+
5334+
[case testOverloadIfOldStyle]
5335+
# flags: --always-false var_false --always-true var_true
5336+
from typing import overload, Any
5337+
5338+
class A: ...
5339+
class B: ...
5340+
5341+
var_true = True
5342+
var_false = False
5343+
5344+
if var_false:
5345+
@overload
5346+
def f1(g: int) -> A: ...
5347+
@overload
5348+
def f1(g: str) -> B: ...
5349+
def f1(g: Any) -> Any: ...
5350+
elif var_true:
5351+
@overload
5352+
def f1(g: int) -> A: ...
5353+
@overload
5354+
def f1(g: str) -> B: ...
5355+
def f1(g: Any) -> Any: ...
5356+
else:
5357+
@overload
5358+
def f1(g: int) -> A: ...
5359+
@overload
5360+
def f1(g: str) -> B: ...
5361+
def f1(g: Any) -> Any: ...
5362+
reveal_type(f1(42)) # N: Revealed type is "__main__.A"
5363+
reveal_type(f1("Hello")) # N: Revealed type is "__main__.B"

0 commit comments

Comments
 (0)