diff --git a/jedi/api/classes.py b/jedi/api/classes.py index 1e3c042b..9a3df1dd 100644 --- a/jedi/api/classes.py +++ b/jedi/api/classes.py @@ -546,7 +546,11 @@ class Definition(use_metaclass(CachedMetaClass, BaseDefinition)): elif isinstance(d, pr.Param): d = d.get_code() else: # ExprStmt - first_leaf = d.first_leaf() + try: + first_leaf = d.first_leaf() + except AttributeError: + # `d` is already a Leaf (Name). + first_leaf = d # Remove the prefix, because that's not what we want for get_code # here. old, first_leaf.prefix = first_leaf.prefix, '' diff --git a/jedi/parser/tree.py b/jedi/parser/tree.py index 3a300d70..474b43dc 100644 --- a/jedi/parser/tree.py +++ b/jedi/parser/tree.py @@ -272,18 +272,19 @@ class Name(Leaf): self.start_pos[0], self.start_pos[1]) def get_definition(self): - scope = self.parent + scope = self while scope.parent is not None: - if scope.isinstance(Node): + parent = scope.parent + if scope.isinstance(Node, Name) and parent.type != 'simple_stmt': if scope.type == 'testlist_comp': try: if isinstance(scope.children[1], CompFor): return scope.children[1] except IndexError: pass + scope = parent else: break - scope = scope.parent return scope def is_definition(self):