Skip to content

Commit d9ceadf

Browse files
committed
Bugfix
1 parent 3df1b9e commit d9ceadf

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

mypy/fastparse.py

+5-2
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

+31
Original file line numberDiff line numberDiff line change
@@ -5346,3 +5346,34 @@ def f2(g: Any) -> Any: ...
53465346
reveal_type(f2(42)) # N: Revealed type is "__main__.A"
53475347
reveal_type(f2("Hello")) # N: Revealed type is "__main__.A" \
53485348
# E: Argument 1 to "f2" has incompatible type "str"; expected "int"
5349+
5350+
[case testOverloadIfOldStyle]
5351+
# flags: --always-false var_false --always-true var_true
5352+
from typing import overload, Any
5353+
5354+
class A: ...
5355+
class B: ...
5356+
5357+
var_true = True
5358+
var_false = False
5359+
5360+
if var_false:
5361+
@overload
5362+
def f1(g: int) -> A: ...
5363+
@overload
5364+
def f1(g: str) -> B: ...
5365+
def f1(g: Any) -> Any: ...
5366+
elif var_true:
5367+
@overload
5368+
def f1(g: int) -> A: ...
5369+
@overload
5370+
def f1(g: str) -> B: ...
5371+
def f1(g: Any) -> Any: ...
5372+
else:
5373+
@overload
5374+
def f1(g: int) -> A: ...
5375+
@overload
5376+
def f1(g: str) -> B: ...
5377+
def f1(g: Any) -> Any: ...
5378+
reveal_type(f1(42)) # N: Revealed type is "__main__.A"
5379+
reveal_type(f1("Hello")) # N: Revealed type is "__main__.B"

0 commit comments

Comments
 (0)