diff --git a/jedi/api/classes.py b/jedi/api/classes.py index c8c61250..c193da80 100644 --- a/jedi/api/classes.py +++ b/jedi/api/classes.py @@ -669,7 +669,7 @@ class CallSignature(Definition): Use :attr:`.module_name` for the module name. .. todo:: Remove! """ - return self._executable.get_parent_until() + return self._executable.get_root_node() def __repr__(self): return '<%s: %s index %s>' % \ diff --git a/jedi/api/completion.py b/jedi/api/completion.py index 5a5ecd98..9cb569a2 100644 --- a/jedi/api/completion.py +++ b/jedi/api/completion.py @@ -235,7 +235,7 @@ class Completion: Autocomplete inherited methods when overriding in child class. """ leaf = self._module_node.get_leaf_for_position(self._position, include_prefixes=True) - cls = leaf.get_parent_until(tree.Class) + cls = tree.search_ancestor(leaf, 'classdef') if isinstance(cls, (tree.Class, tree.Function)): # Complete the methods that are defined in the super classes. random_context = self._module_context.create_context( diff --git a/jedi/api/helpers.py b/jedi/api/helpers.py index 565e8435..742d4634 100644 --- a/jedi/api/helpers.py +++ b/jedi/api/helpers.py @@ -299,7 +299,7 @@ def cache_call_signatures(evaluator, context, bracket_leaf, code_lines, user_pos whole = '\n'.join(other_lines + [before_cursor]) before_bracket = re.match(r'.*\(', whole, re.DOTALL) - module_path = bracket_leaf.get_parent_until().path + module_path = bracket_leaf.get_root_node().path if module_path is None: yield None # Don't cache! else: diff --git a/jedi/api/keywords.py b/jedi/api/keywords.py index a29eaf1b..63627327 100644 --- a/jedi/api/keywords.py +++ b/jedi/api/keywords.py @@ -90,9 +90,6 @@ class Keyword(object): self.start_pos = pos self.parent = evaluator.BUILTINS - def get_parent_until(self): - return self.parent - @property def only_valid_as_leaf(self): return self.name.value in keywords_only_valid_as_leaf diff --git a/jedi/evaluate/__init__.py b/jedi/evaluate/__init__.py index 3cb517fd..c3b8992d 100644 --- a/jedi/evaluate/__init__.py +++ b/jedi/evaluate/__init__.py @@ -161,7 +161,7 @@ class Evaluator(object): left = context.py__getattribute__( name, position=stmt.start_pos, search_global=True) - for_stmt = stmt.get_parent_until(tree.ForStmt) + for_stmt = tree.search_ancestor(stmt, 'for_stmt') if isinstance(for_stmt, tree.ForStmt) and types \ and for_stmt.defines_one_name(): # Iterate through result and add the values, that's possible @@ -335,8 +335,8 @@ class Evaluator(object): # This is the first global lookup. stmt = atom.get_definition() if isinstance(stmt, tree.CompFor): - stmt = stmt.get_parent_until((tree.ClassOrFunc, tree.ExprStmt)) - if stmt.type != 'expr_stmt': + stmt = tree.search_ancestor(stmt, ('expr_stmt', 'lambda', 'funcdef', 'classdef')) + if stmt is None or stmt.type != 'expr_stmt': # We only need to adjust the start_pos for statements, because # there the name cannot be used. stmt = atom diff --git a/jedi/evaluate/finder.py b/jedi/evaluate/finder.py index 7a3f2e10..fe7cb9f8 100644 --- a/jedi/evaluate/finder.py +++ b/jedi/evaluate/finder.py @@ -78,7 +78,13 @@ class NameFinder(object): def _get_origin_scope(self): if isinstance(self._name, tree.Name): - return self._name.get_parent_until(tree.Scope, reverse=True) + scope = self._name + while scope.parent is not None: + # TODO why if classes? + if not isinstance(scope, tree.Scope): + break + scope = scope.parent + return scope else: return None diff --git a/jedi/evaluate/imports.py b/jedi/evaluate/imports.py index fb446fd0..fa6bf929 100644 --- a/jedi/evaluate/imports.py +++ b/jedi/evaluate/imports.py @@ -36,7 +36,7 @@ from jedi.evaluate.filters import AbstractNameDefinition @memoize_default(default=set()) def infer_import(context, tree_name, is_goto=False): module_context = context.get_root_context() - import_node = tree_name.get_parent_until(tree.Import) + import_node = tree.search_ancestor(tree_name, ('import_name', 'import_from')) import_path = import_node.path_for_name(tree_name) from_import_name = None evaluator = context.evaluator diff --git a/jedi/evaluate/pep0484.py b/jedi/evaluate/pep0484.py index 52e9e4ef..7e0f5a19 100644 --- a/jedi/evaluate/pep0484.py +++ b/jedi/evaluate/pep0484.py @@ -69,7 +69,7 @@ def _fix_forward_reference(context, node): debug.warning('Annotation not parsed: %s' % evaled_node.obj) return node else: - module = node.get_parent_until() + module = node.get_root_node() new_node.move(module.end_pos[0]) new_node.parent = context.tree_node return new_node diff --git a/jedi/parser/tree.py b/jedi/parser/tree.py index 83469bb9..f0b481fd 100644 --- a/jedi/parser/tree.py +++ b/jedi/parser/tree.py @@ -126,24 +126,6 @@ class Base(object): scope = scope.parent return scope - @Python3Method - def get_parent_until(self, classes=(), reverse=False, - include_current=True): - """ - Searches the parent "chain" until the object is an instance of - classes. If classes is empty return the last parent in the chain - (is without a parent). - """ - if type(classes) not in (tuple, list): - classes = (classes,) - scope = self if include_current else self.parent - while scope.parent is not None: - # TODO why if classes? - if classes and reverse != isinstance(scope, classes): - break - scope = scope.parent - return scope - def get_parent_scope(self, include_flows=False): """ Returns the underlying scope.