From e66a5e2716e6c5970b0427f9fc76c8602bc2da60 Mon Sep 17 00:00:00 2001 From: David Liu Date: Mon, 19 Jul 2021 19:14:00 +0000 Subject: Support lookup in nested non-FunctionDef scopes (#1102) Co-authored-by: Pierre Sassoulas --- astroid/scoped_nodes.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'astroid') 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. -- cgit v1.2.1