Writing a different Name.get_definition() implementation, returns the node, if there's no expr_stmt parent.

This commit is contained in:
Dave Halter
2015-03-05 15:17:08 +01:00
parent 0ceadf69a3
commit 8f58258f4d
2 changed files with 9 additions and 4 deletions

View File

@@ -546,7 +546,11 @@ class Definition(use_metaclass(CachedMetaClass, BaseDefinition)):
elif isinstance(d, pr.Param):
d = d.get_code()
else: # ExprStmt
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, ''

View File

@@ -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):