Skip to content

Commit c688f4a

Browse files
authored
Allow binder to understand assignment via def (#13508)
Fixes #2569
1 parent e3827a1 commit c688f4a

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

mypy/checker.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,9 @@ def _visit_func_def(self, defn: FuncDef) -> None:
972972
# Trying to redefine something like partial empty list as function.
973973
self.fail(message_registry.INCOMPATIBLE_REDEFINITION, defn)
974974
else:
975-
# TODO: Update conditional type binder.
975+
name_expr = NameExpr(defn.name)
976+
name_expr.node = defn.original_def
977+
self.binder.assign_type(name_expr, new_type, orig_type)
976978
self.check_subtype(
977979
new_type,
978980
orig_type,

test-data/unit/check-narrowing.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,3 +1249,14 @@ def two_type_vars(x: Union[str, Dict[str, int], Dict[bool, object], int]) -> Non
12491249
else:
12501250
reveal_type(x) # N: Revealed type is "builtins.int"
12511251
[builtins fixtures/dict.pyi]
1252+
1253+
1254+
[case testNarrowingWithDef]
1255+
from typing import Callable, Optional
1256+
1257+
def g() -> None:
1258+
foo: Optional[Callable[[], None]] = None
1259+
if foo is None:
1260+
def foo(): ...
1261+
foo()
1262+
[builtins fixtures/dict.pyi]

0 commit comments

Comments
 (0)