Skip to content

Commit 3f102d3

Browse files
committed
Bugfix
1 parent 84e6c9b commit 3f102d3

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

mypy/fastparse.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,11 +501,12 @@ def fix_function_overloads(self, stmts: List[Statement]) -> List[Statement]:
501501
and len(stmt.body[0].body) == 1
502502
and isinstance(
503503
stmt.body[0].body[0], (Decorator, OverloadedFuncDef))
504-
and infer_reachability_of_if_statement(
504+
and infer_reachability_of_if_statement( # type: ignore[func-returns-value]
505505
stmt, self.options
506-
) is None # type: ignore[func-returns-value]
506+
) is None
507507
and stmt.body[0].is_unreachable is False
508508
):
509+
current_overload = []
509510
current_overload_name = stmt.body[0].body[0].name
510511
last_if_stmt = stmt
511512
last_if_overload = stmt.body[0].body[0]
@@ -518,6 +519,8 @@ def fix_function_overloads(self, stmts: List[Statement]) -> List[Statement]:
518519
ret.append(current_overload[0])
519520
elif len(current_overload) > 1:
520521
ret.append(OverloadedFuncDef(current_overload))
522+
elif last_if_stmt is not None:
523+
ret.append(last_if_stmt)
521524
return ret
522525

523526
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)