Skip to content

Commit 7e7502b

Browse files
committed
Bugfix
1 parent 99436ee commit 7e7502b

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
@@ -509,11 +509,12 @@ def fix_function_overloads(self, stmts: List[Statement]) -> List[Statement]:
509509
and len(stmt.body[0].body) == 1
510510
and isinstance(
511511
stmt.body[0].body[0], (Decorator, OverloadedFuncDef))
512-
and infer_reachability_of_if_statement(
512+
and infer_reachability_of_if_statement( # type: ignore[func-returns-value]
513513
stmt, self.options
514-
) is None # type: ignore[func-returns-value]
514+
) is None
515515
and stmt.body[0].is_unreachable is False
516516
):
517+
current_overload = []
517518
current_overload_name = stmt.body[0].body[0].name
518519
last_if_stmt = stmt
519520
last_if_overload = stmt.body[0].body[0]
@@ -526,6 +527,8 @@ def fix_function_overloads(self, stmts: List[Statement]) -> List[Statement]:
526527
ret.append(current_overload[0])
527528
elif len(current_overload) > 1:
528529
ret.append(OverloadedFuncDef(current_overload))
530+
elif last_if_stmt is not None:
531+
ret.append(last_if_stmt)
529532
return ret
530533

531534
def in_method_scope(self) -> bool:

test-data/unit/check-overloading.test

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

0 commit comments

Comments
 (0)