mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-08 06:44:46 +08:00
Definition.parent is working. fixes #325
This commit is contained in:
@@ -632,6 +632,11 @@ class Definition(use_metaclass(CachedMetaClass, BaseDefinition)):
|
||||
iterable = list(iterable)
|
||||
return list(chain.from_iterable(iterable))
|
||||
|
||||
def parent(self):
|
||||
scope = self._definition.get_parent_until(pr.IsScope, include_current=False)
|
||||
non_flow = scope.get_parent_until(pr.Flow, reverse=True)
|
||||
return Definition(self._evaluator, non_flow)
|
||||
|
||||
|
||||
class CallSignature(Definition):
|
||||
"""
|
||||
|
||||
@@ -146,17 +146,19 @@ def test_signature_params():
|
||||
|
||||
class TestParent(TestCase):
|
||||
def _parent(self, source):
|
||||
defs = Script(dedent(source)).goto_definitions()
|
||||
defs = Script(dedent(source)).goto_assignments()
|
||||
assert len(defs) == 1
|
||||
return defs[0].parent()
|
||||
|
||||
def test_parent(self):
|
||||
parent = self._parent('foo')
|
||||
assert isinstance(parent, pr.SubModule)
|
||||
parent = self._parent('foo=1\nfoo')
|
||||
assert parent.type == 'module'
|
||||
|
||||
parent = self._parent('''
|
||||
def spam():
|
||||
if 1:
|
||||
''')
|
||||
assert isinstance(parent, pr.SubModule)
|
||||
assert isinstance(parent.parent(), pr.SubModule)
|
||||
y=1
|
||||
y''')
|
||||
assert parent.name == 'spam'
|
||||
print(parent, parent.parent())
|
||||
assert parent.parent().type == 'module'
|
||||
|
||||
Reference in New Issue
Block a user