mirror of
https://github.com/davidhalter/jedi.git
synced 2026-01-23 20:31:48 +08:00
Avoid the need to import search_ancestor
This commit is contained in:
@@ -483,7 +483,7 @@ class Script:
|
||||
|
||||
module_context = self._get_module_context()
|
||||
|
||||
n = tree.search_ancestor(leaf, 'funcdef', 'classdef')
|
||||
n = leaf.search_ancestor('funcdef', 'classdef')
|
||||
if n is not None and n.start_pos < pos <= n.children[-1].start_pos:
|
||||
# This is a bit of a special case. The context of a function/class
|
||||
# name/param/keyword is always it's parent context, not the
|
||||
|
||||
@@ -17,8 +17,6 @@ import re
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from parso.tree import search_ancestor
|
||||
|
||||
from jedi import settings
|
||||
from jedi import debug
|
||||
from jedi.inference.utils import unite
|
||||
@@ -509,7 +507,7 @@ class BaseName:
|
||||
# - param: The parent_context of a param is not its function but
|
||||
# e.g. the outer class or module.
|
||||
cls_or_func_node = self._name.tree_name.get_definition()
|
||||
parent = search_ancestor(cls_or_func_node, 'funcdef', 'classdef', 'file_input')
|
||||
parent = cls_or_func_node.search_ancestor('funcdef', 'classdef', 'file_input')
|
||||
context = self._get_module_context().create_value(parent).as_context()
|
||||
else:
|
||||
context = self._name.parent_context
|
||||
|
||||
@@ -4,7 +4,7 @@ from inspect import Parameter
|
||||
|
||||
from parso.python.token import PythonTokenTypes
|
||||
from parso.python import tree
|
||||
from parso.tree import search_ancestor, Leaf
|
||||
from parso.tree import Leaf
|
||||
from parso import split_lines
|
||||
|
||||
from jedi import debug
|
||||
@@ -244,8 +244,8 @@ class Completion:
|
||||
if previous_leaf is not None:
|
||||
stmt = previous_leaf
|
||||
while True:
|
||||
stmt = search_ancestor(
|
||||
stmt, 'if_stmt', 'for_stmt', 'while_stmt', 'try_stmt',
|
||||
stmt = stmt.search_ancestor(
|
||||
'if_stmt', 'for_stmt', 'while_stmt', 'try_stmt',
|
||||
'error_node',
|
||||
)
|
||||
if stmt is None:
|
||||
@@ -356,7 +356,7 @@ class Completion:
|
||||
stack_node = self.stack[-3]
|
||||
if stack_node.nonterminal == 'funcdef':
|
||||
context = get_user_context(self._module_context, self._position)
|
||||
node = search_ancestor(leaf, 'error_node', 'funcdef')
|
||||
node = leaf.search_ancestor('error_node', 'funcdef')
|
||||
if node is not None:
|
||||
if node.type == 'error_node':
|
||||
n = node.children[0]
|
||||
@@ -426,7 +426,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 = tree.search_ancestor(leaf, 'classdef')
|
||||
cls = leaf.search_ancestor('classdef')
|
||||
if cls is None:
|
||||
return
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ from contextlib import contextmanager
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from parso.tree import search_ancestor
|
||||
from parso.python.tree import Name
|
||||
|
||||
from jedi.inference.filters import ParserTreeFilter, MergedFilter, \
|
||||
@@ -290,7 +289,7 @@ class TreeContextMixin:
|
||||
def create_name(self, tree_name):
|
||||
definition = tree_name.get_definition()
|
||||
if definition and definition.type == 'param' and definition.name == tree_name:
|
||||
funcdef = search_ancestor(definition, 'funcdef', 'lambdef')
|
||||
funcdef = definition.search_ancestor('funcdef', 'lambdef')
|
||||
func = self.create_value(funcdef)
|
||||
return AnonymousParamName(func, tree_name)
|
||||
else:
|
||||
@@ -416,13 +415,13 @@ def _get_global_filters_for_name(context, name_or_none, position):
|
||||
# function and get inferred in the value before the function. So
|
||||
# make sure to exclude the function/class name.
|
||||
if name_or_none is not None:
|
||||
ancestor = search_ancestor(name_or_none, 'funcdef', 'classdef', 'lambdef')
|
||||
ancestor = name_or_none.search_ancestor('funcdef', 'classdef', 'lambdef')
|
||||
lambdef = None
|
||||
if ancestor == 'lambdef':
|
||||
# For lambdas it's even more complicated since parts will
|
||||
# be inferred later.
|
||||
lambdef = ancestor
|
||||
ancestor = search_ancestor(name_or_none, 'funcdef', 'classdef')
|
||||
ancestor = name_or_none.search_ancestor('funcdef', 'classdef')
|
||||
if ancestor is not None:
|
||||
colon = ancestor.children[-2]
|
||||
if position is not None and position < colon.start_pos:
|
||||
|
||||
@@ -6,7 +6,6 @@ from abc import abstractmethod
|
||||
from typing import List, MutableMapping, Type
|
||||
import weakref
|
||||
|
||||
from parso.tree import search_ancestor
|
||||
from parso.python.tree import Name, UsedNamesMapping
|
||||
|
||||
from jedi.inference import flow_analysis
|
||||
@@ -181,7 +180,7 @@ class _FunctionExecutionFilter(ParserTreeFilter):
|
||||
@to_list
|
||||
def _convert_names(self, names):
|
||||
for name in names:
|
||||
param = search_ancestor(name, 'param')
|
||||
param = name.search_ancestor('param')
|
||||
# Here we don't need to check if the param is a default/annotation,
|
||||
# because those are not definitions and never make it to this
|
||||
# point.
|
||||
|
||||
@@ -15,7 +15,6 @@ Unfortunately every other thing is being ignored (e.g. a == '' would be easy to
|
||||
check for -> a is a string). There's big potential in these checks.
|
||||
"""
|
||||
|
||||
from parso.tree import search_ancestor
|
||||
from parso.python.tree import Name
|
||||
|
||||
from jedi import settings
|
||||
@@ -76,7 +75,7 @@ def check_flow_information(value, flow, search_name, pos):
|
||||
])
|
||||
|
||||
for name in names:
|
||||
ass = search_ancestor(name, 'assert_stmt')
|
||||
ass = name.search_ancestor('assert_stmt')
|
||||
if ass is not None:
|
||||
result = _check_isinstance_type(value, ass.assertion, search_name)
|
||||
if result is not None:
|
||||
|
||||
@@ -12,7 +12,6 @@ import os
|
||||
from pathlib import Path
|
||||
|
||||
from parso.python import tree
|
||||
from parso.tree import search_ancestor
|
||||
|
||||
from jedi import debug
|
||||
from jedi import settings
|
||||
@@ -95,7 +94,7 @@ def goto_import(context, tree_name):
|
||||
|
||||
|
||||
def _prepare_infer_import(module_context, tree_name):
|
||||
import_node = search_ancestor(tree_name, 'import_name', 'import_from')
|
||||
import_node = tree_name.search_ancestor('import_name', 'import_from')
|
||||
import_path = import_node.get_path_for_name(tree_name)
|
||||
from_import_name = None
|
||||
try:
|
||||
@@ -549,7 +548,7 @@ def load_namespace_from_path(inference_state, folder_io):
|
||||
|
||||
|
||||
def follow_error_node_imports_if_possible(context, name):
|
||||
error_node = tree.search_ancestor(name, 'error_node')
|
||||
error_node = name.search_ancestor('error_node')
|
||||
if error_node is not None:
|
||||
# Get the first command start of a started simple_stmt. The error
|
||||
# node is sometimes a small_stmt and sometimes a simple_stmt. Check
|
||||
|
||||
@@ -2,8 +2,6 @@ from abc import abstractmethod
|
||||
from inspect import Parameter
|
||||
from typing import Optional, Tuple
|
||||
|
||||
from parso.tree import search_ancestor
|
||||
|
||||
from jedi.parser_utils import find_statement_documentation, clean_scope_docstring
|
||||
from jedi.inference.utils import unite
|
||||
from jedi.inference.base_value import ValueSet, NO_VALUES
|
||||
@@ -112,7 +110,7 @@ class AbstractTreeName(AbstractNameDefinition):
|
||||
self.tree_name = tree_name
|
||||
|
||||
def get_qualified_names(self, include_module_names=False):
|
||||
import_node = search_ancestor(self.tree_name, 'import_name', 'import_from')
|
||||
import_node = self.tree_name.search_ancestor('import_name', 'import_from')
|
||||
# For import nodes we cannot just have names, because it's very unclear
|
||||
# how they would look like. For now we just ignore them in most cases.
|
||||
# In case of level == 1, it works always, because it's like a submodule
|
||||
@@ -205,15 +203,13 @@ class AbstractTreeName(AbstractNameDefinition):
|
||||
values = infer_call_of_leaf(context, name, cut_own_trailer=True)
|
||||
return values.goto(name, name_context=context)
|
||||
else:
|
||||
stmt = search_ancestor(
|
||||
name, 'expr_stmt', 'lambdef'
|
||||
) or name
|
||||
stmt = name.search_ancestor('expr_stmt', 'lambdef') or name
|
||||
if stmt.type == 'lambdef':
|
||||
stmt = name
|
||||
return context.goto(name, position=stmt.start_pos)
|
||||
|
||||
def is_import(self):
|
||||
imp = search_ancestor(self.tree_name, 'import_from', 'import_name')
|
||||
imp = self.tree_name.search_ancestor('import_from', 'import_name')
|
||||
return imp is not None
|
||||
|
||||
@property
|
||||
@@ -451,7 +447,7 @@ class _ActualTreeParamName(BaseTreeParamName):
|
||||
self.function_value = function_value
|
||||
|
||||
def _get_param_node(self):
|
||||
return search_ancestor(self.tree_name, 'param')
|
||||
return self.tree_name.search_ancestor('param')
|
||||
|
||||
@property
|
||||
def annotation_node(self):
|
||||
|
||||
@@ -12,15 +12,12 @@ The signature here for bar should be `bar(b, c)` instead of bar(*args).
|
||||
"""
|
||||
from inspect import Parameter
|
||||
|
||||
from parso import tree
|
||||
|
||||
from jedi.inference.utils import to_list
|
||||
from jedi.inference.names import ParamNameWrapper
|
||||
from jedi.inference.helpers import is_big_annoying_library
|
||||
|
||||
|
||||
def _iter_nodes_for_param(param_name):
|
||||
from parso.python.tree import search_ancestor
|
||||
from jedi.inference.arguments import TreeArguments
|
||||
|
||||
execution_context = param_name.parent_context
|
||||
@@ -28,7 +25,7 @@ def _iter_nodes_for_param(param_name):
|
||||
# tree rather than going via the execution context so that we're agnostic of
|
||||
# the specific scope we're evaluating within (i.e: module or function,
|
||||
# etc.).
|
||||
function_node = tree.search_ancestor(param_name.tree_name, 'funcdef', 'lambdef')
|
||||
function_node = param_name.tree_name.search_ancestor('funcdef', 'lambdef')
|
||||
module_node = function_node.get_root_node()
|
||||
start = function_node.children[-1].start_pos
|
||||
end = function_node.children[-1].end_pos
|
||||
@@ -38,7 +35,7 @@ def _iter_nodes_for_param(param_name):
|
||||
argument = name.parent
|
||||
if argument.type == 'argument' \
|
||||
and argument.children[0] == '*' * param_name.star_count:
|
||||
trailer = search_ancestor(argument, 'trailer')
|
||||
trailer = argument.search_ancestor('trailer')
|
||||
if trailer is not None: # Make sure we're in a function
|
||||
context = execution_context.create_context(trailer)
|
||||
if _goes_to_param_name(param_name, context, name):
|
||||
|
||||
@@ -290,7 +290,7 @@ def infer_atom(context, atom):
|
||||
state = context.inference_state
|
||||
if atom.type == 'name':
|
||||
# This is the first global lookup.
|
||||
stmt = tree.search_ancestor(atom, 'expr_stmt', 'lambdef', 'if_stmt') or atom
|
||||
stmt = atom.search_ancestor('expr_stmt', 'lambdef', 'if_stmt') or atom
|
||||
if stmt.type == 'if_stmt':
|
||||
if not any(n.start_pos <= atom.start_pos < n.end_pos for n in stmt.get_test_nodes()):
|
||||
stmt = atom
|
||||
@@ -436,7 +436,7 @@ def _infer_expr_stmt(context, stmt, seek_name=None):
|
||||
else:
|
||||
operator = copy.copy(first_operator)
|
||||
operator.value = operator.value[:-1]
|
||||
for_stmt = tree.search_ancestor(stmt, 'for_stmt')
|
||||
for_stmt = stmt.search_ancestor('for_stmt')
|
||||
if for_stmt is not None and for_stmt.type == 'for_stmt' and value_set \
|
||||
and parser_utils.for_stmt_defines_one_name(for_stmt):
|
||||
# Iterate through result and add the values, that's possible
|
||||
@@ -549,7 +549,7 @@ def _infer_comparison(context, left_values, operator, right_values):
|
||||
|
||||
|
||||
def _is_annotation_name(name):
|
||||
ancestor = tree.search_ancestor(name, 'param', 'funcdef', 'expr_stmt')
|
||||
ancestor = name.search_ancestor('param', 'funcdef', 'expr_stmt')
|
||||
if ancestor is None:
|
||||
return False
|
||||
|
||||
|
||||
@@ -262,8 +262,8 @@ class BaseFunctionExecutionContext(ValueContext, TreeContextMixin):
|
||||
@recursion.execution_recursion_decorator(default=iter([]))
|
||||
def get_yield_lazy_values(self, is_async=False):
|
||||
# TODO: if is_async, wrap yield statements in Awaitable/async_generator_asend
|
||||
for_parents = [(y, tree.search_ancestor(y, 'for_stmt', 'funcdef',
|
||||
'while_stmt', 'if_stmt'))
|
||||
for_parents = [(y, y.search_ancestor('for_stmt', 'funcdef',
|
||||
'while_stmt', 'if_stmt'))
|
||||
for y in get_yield_exprs(self.inference_state, self.tree_node)]
|
||||
|
||||
# Calculate if the yields are placed within the same for loop.
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
from abc import abstractproperty
|
||||
|
||||
from parso.tree import search_ancestor
|
||||
|
||||
from jedi import debug
|
||||
from jedi import settings
|
||||
from jedi.inference import compiled
|
||||
@@ -229,7 +227,7 @@ class _BaseTreeInstance(AbstractInstanceValue):
|
||||
new = node
|
||||
while True:
|
||||
func_node = new
|
||||
new = search_ancestor(new, 'funcdef', 'classdef')
|
||||
new = new.search_ancestor('funcdef', 'classdef')
|
||||
if class_context.tree_node is new:
|
||||
func = FunctionValue.from_context(class_context, func_node)
|
||||
bound_method = BoundMethod(self, class_context, func)
|
||||
@@ -498,7 +496,7 @@ class SelfName(TreeNameDefinition):
|
||||
return self._instance
|
||||
|
||||
def infer(self):
|
||||
stmt = search_ancestor(self.tree_name, 'expr_stmt')
|
||||
stmt = self.tree_name.search_ancestor('expr_stmt')
|
||||
if stmt is not None:
|
||||
if stmt.children[1].type == "annassign":
|
||||
from jedi.inference.gradual.annotation import infer_annotation
|
||||
|
||||
@@ -2,7 +2,6 @@ import sys
|
||||
from typing import List
|
||||
from pathlib import Path
|
||||
|
||||
from parso.tree import search_ancestor
|
||||
from jedi.inference.cache import inference_state_method_cache
|
||||
from jedi.inference.imports import goto_import, load_module_from_path
|
||||
from jedi.inference.filters import ParserTreeFilter
|
||||
@@ -120,7 +119,7 @@ def _is_a_pytest_param_and_inherited(param_name):
|
||||
|
||||
This is a heuristic and will work in most cases.
|
||||
"""
|
||||
funcdef = search_ancestor(param_name.tree_name, 'funcdef')
|
||||
funcdef = param_name.tree_name.search_ancestor('funcdef')
|
||||
if funcdef is None: # A lambda
|
||||
return False, False
|
||||
decorators = funcdef.get_decorators()
|
||||
|
||||
Reference in New Issue
Block a user