Skip to content

Commit

Permalink
Bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p committed Jul 7, 2021
1 parent 3df1b9e commit d9ceadf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
7 changes: 5 additions & 2 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,11 +501,12 @@ def fix_function_overloads(self, stmts: List[Statement]) -> List[Statement]:
and len(stmt.body[0].body) == 1
and isinstance(
stmt.body[0].body[0], (Decorator, OverloadedFuncDef))
and infer_reachability_of_if_statement(
and infer_reachability_of_if_statement( # type: ignore[func-returns-value]
stmt, self.options
) is None # type: ignore[func-returns-value]
) is None
and stmt.body[0].is_unreachable is False
):
current_overload = []
current_overload_name = stmt.body[0].body[0].name
last_if_stmt = stmt
last_if_overload = stmt.body[0].body[0]
Expand All @@ -518,6 +519,8 @@ def fix_function_overloads(self, stmts: List[Statement]) -> List[Statement]:
ret.append(current_overload[0])
elif len(current_overload) > 1:
ret.append(OverloadedFuncDef(current_overload))
elif last_if_stmt is not None:
ret.append(last_if_stmt)
return ret

def in_method_scope(self) -> bool:
Expand Down
31 changes: 31 additions & 0 deletions test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -5346,3 +5346,34 @@ def f2(g: Any) -> Any: ...
reveal_type(f2(42)) # N: Revealed type is "__main__.A"
reveal_type(f2("Hello")) # N: Revealed type is "__main__.A" \
# E: Argument 1 to "f2" has incompatible type "str"; expected "int"

[case testOverloadIfOldStyle]
# flags: --always-false var_false --always-true var_true
from typing import overload, Any

class A: ...
class B: ...

var_true = True
var_false = False

if var_false:
@overload
def f1(g: int) -> A: ...
@overload
def f1(g: str) -> B: ...
def f1(g: Any) -> Any: ...
elif var_true:
@overload
def f1(g: int) -> A: ...
@overload
def f1(g: str) -> B: ...
def f1(g: Any) -> Any: ...
else:
@overload
def f1(g: int) -> A: ...
@overload
def f1(g: str) -> B: ...
def f1(g: Any) -> Any: ...
reveal_type(f1(42)) # N: Revealed type is "__main__.A"
reveal_type(f1("Hello")) # N: Revealed type is "__main__.B"

0 comments on commit d9ceadf

Please sign in to comment.