Skip to content

Commit 13f35ad

Browse files
authored
Fix crash related to propagating type narrowing to nested functions (#15181)
See #15133 (comment) for context.
1 parent d17b3ed commit 13f35ad

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

mypy/checker.py

+1
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,7 @@ def check_func_def(
12221222
if new_frame is None:
12231223
new_frame = self.binder.push_frame()
12241224
new_frame.types[key] = narrowed_type
1225+
self.binder.declarations[key] = old_binder.declarations[key]
12251226
with self.scope.push_function(defn):
12261227
# We suppress reachability warnings when we use TypeVars with value
12271228
# restrictions: we only want to report a warning if a certain statement is

test-data/unit/check-optional.test

+14
Original file line numberDiff line numberDiff line change
@@ -1329,3 +1329,17 @@ def narrow_with_multi_assign_3(x: Optional[str]) -> None:
13291329
y, x = None, None
13301330

13311331
[builtins fixtures/isinstance.pyi]
1332+
1333+
[case testNestedFunctionSpecialCase]
1334+
class C:
1335+
def __enter__(self, *args): ...
1336+
def __exit__(self, *args) -> bool: ...
1337+
1338+
def f(x: object) -> None:
1339+
if x is not None:
1340+
pass
1341+
1342+
def nested() -> None:
1343+
with C():
1344+
pass
1345+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)