Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash related to propagating type narrowing to nested functions #15181

Merged
merged 1 commit into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]