Skip to content

Commit

Permalink
Fix crash related to propagating type narrowing to nested functions (#…
Browse files Browse the repository at this point in the history
…15181)

See #15133 (comment)
for context.
  • Loading branch information
JukkaL authored May 4, 2023
1 parent d17b3ed commit 13f35ad
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,7 @@ def check_func_def(
if new_frame is None:
new_frame = self.binder.push_frame()
new_frame.types[key] = narrowed_type
self.binder.declarations[key] = old_binder.declarations[key]
with self.scope.push_function(defn):
# We suppress reachability warnings when we use TypeVars with value
# restrictions: we only want to report a warning if a certain statement is
Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-optional.test
Original file line number Diff line number Diff line change
Expand Up @@ -1329,3 +1329,17 @@ def narrow_with_multi_assign_3(x: Optional[str]) -> None:
y, x = None, None

[builtins fixtures/isinstance.pyi]

[case testNestedFunctionSpecialCase]
class C:
def __enter__(self, *args): ...
def __exit__(self, *args) -> bool: ...

def f(x: object) -> None:
if x is not None:
pass

def nested() -> None:
with C():
pass
[builtins fixtures/tuple.pyi]

0 comments on commit 13f35ad

Please sign in to comment.