Skip to content

Commit

Permalink
Fix the issue
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra committed Mar 11, 2024
1 parent 74f367d commit 0c7adc6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Fix incorrect `undefined_name` errors when a class is nested in a nested
function and uses a name from the outer function (#750)
- Fix incorrect `possibly_undefined_name` error on certain uses of the
walrus operator (#749)
- Fix narrowing on `isinstance` calls with arguments that are not
Expand Down
10 changes: 9 additions & 1 deletion pyanalyze/name_check_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1822,7 +1822,15 @@ def _get_current_class_object(self, node: ast.ClassDef) -> Optional[type]:
def _visit_class_and_get_value(
self, node: ast.ClassDef, current_class: Optional[type]
) -> Value:
if self._is_checking():
if self._is_collecting():
# If this is a nested class, we need to run the collecting phase to get data
# about names accessed from the class.
if len(self.scopes.scopes) > 2:
with self.scopes.add_scope(
ScopeType.class_scope, scope_node=node, scope_object=current_class
), self._set_current_class(current_class):
self._generic_visit_list(node.body)
else:
with self.scopes.add_scope(
ScopeType.class_scope, scope_node=None, scope_object=current_class
), self._set_current_class(current_class):
Expand Down
18 changes: 18 additions & 0 deletions pyanalyze/test_stacked_scopes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1937,3 +1937,21 @@ class Nested:
return Nested

return inner

@assert_passes()
def test_triple_function_nesting(self):
from typing_extensions import Literal, assert_type

def outer():
outer_var = "outer"

def inner():
inner_var = "inner"

def innermost():
assert_type(outer_var, Literal["outer"])
assert_type(inner_var, Literal["inner"])

return innermost

return inner

0 comments on commit 0c7adc6

Please sign in to comment.