summaryrefslogtreecommitdiff
path: root/astroid
diff options
context:
space:
mode:
authorDavid Liu <david@cs.toronto.edu>2021-07-19 19:14:00 +0000
committerGitHub <noreply@github.com>2021-07-19 21:14:00 +0200
commite66a5e2716e6c5970b0427f9fc76c8602bc2da60 (patch)
treef3abca72dd37e45f67c34a940f85d643feb0334c /astroid
parent9947a598e87a806ca74f1f152cb59c63914f367c (diff)
downloadastroid-git-e66a5e2716e6c5970b0427f9fc76c8602bc2da60.tar.gz
Support lookup in nested non-FunctionDef scopes (#1102)
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Diffstat (limited to 'astroid')
-rw-r--r--astroid/scoped_nodes.py19
1 files changed, 11 insertions, 8 deletions
diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py
index 5fa890d9..d6cda7fa 100644
--- a/astroid/scoped_nodes.py
+++ b/astroid/scoped_nodes.py
@@ -244,14 +244,17 @@ class LocalsDictNodeNG(node_classes.LookupMixIn, node_classes.NodeNG):
stmts = ()
if stmts:
return self, stmts
- if self.parent: # i.e. not Module
- # nested scope: if parent scope is a function, that's fine
- # else jump to the module
- pscope = self.parent.scope()
- if not pscope.is_function:
- pscope = pscope.root()
- return pscope.scope_lookup(node, name)
- return builtin_lookup(name) # Module
+
+ # Handle nested scopes: since class names do not extend to nested
+ # scopes (e.g., methods), we find the next enclosing non-class scope
+ pscope = self.parent and self.parent.scope()
+ while pscope is not None:
+ if not isinstance(pscope, ClassDef):
+ return pscope.scope_lookup(node, name)
+ pscope = pscope.parent and pscope.parent.scope()
+
+ # self is at the top level of a module, or is enclosed only by ClassDefs
+ return builtin_lookup(name)
def set_local(self, name, stmt):
"""Define that the given name is declared in the given statement node.