Skip to content

Commit 151393b

Browse files
committed
Bugfix
1 parent 399976b commit 151393b

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
@@ -507,11 +507,12 @@ def fix_function_overloads(self, stmts: List[Statement]) -> List[Statement]:
507507
and len(stmt.body[0].body) == 1
508508
and isinstance(
509509
stmt.body[0].body[0], (Decorator, OverloadedFuncDef))
510-
and infer_reachability_of_if_statement(
510+
and infer_reachability_of_if_statement( # type: ignore[func-returns-value]
511511
stmt, self.options
512-
) is None # type: ignore[func-returns-value]
512+
) is None
513513
and stmt.body[0].is_unreachable is False
514514
):
515+
current_overload = []
515516
current_overload_name = stmt.body[0].body[0].name
516517
last_if_stmt = stmt
517518
last_if_overload = stmt.body[0].body[0]
@@ -524,6 +525,8 @@ def fix_function_overloads(self, stmts: List[Statement]) -> List[Statement]:
524525
ret.append(current_overload[0])
525526
elif len(current_overload) > 1:
526527
ret.append(OverloadedFuncDef(current_overload))
528+
elif last_if_stmt is not None:
529+
ret.append(last_if_stmt)
527530
return ret
528531

529532
def in_method_scope(self) -> bool:

test-data/unit/check-overloading.test

+31
Original file line numberDiff line numberDiff line change
@@ -5494,3 +5494,34 @@ def f2(g: Any) -> Any: ...
54945494
reveal_type(f2(42)) # N: Revealed type is "__main__.A"
54955495
reveal_type(f2("Hello")) # N: Revealed type is "__main__.A" \
54965496
# E: Argument 1 to "f2" has incompatible type "str"; expected "int"
5497+
5498+
[case testOverloadIfOldStyle]
5499+
# flags: --always-false var_false --always-true var_true
5500+
from typing import overload, Any
5501+
5502+
class A: ...
5503+
class B: ...
5504+
5505+
var_true = True
5506+
var_false = False
5507+
5508+
if var_false:
5509+
@overload
5510+
def f1(g: int) -> A: ...
5511+
@overload
5512+
def f1(g: str) -> B: ...
5513+
def f1(g: Any) -> Any: ...
5514+
elif var_true:
5515+
@overload
5516+
def f1(g: int) -> A: ...
5517+
@overload
5518+
def f1(g: str) -> B: ...
5519+
def f1(g: Any) -> Any: ...
5520+
else:
5521+
@overload
5522+
def f1(g: int) -> A: ...
5523+
@overload
5524+
def f1(g: str) -> B: ...
5525+
def f1(g: Any) -> Any: ...
5526+
reveal_type(f1(42)) # N: Revealed type is "__main__.A"
5527+
reveal_type(f1("Hello")) # N: Revealed type is "__main__.B"

0 commit comments

Comments
 (0)