mirror of
https://github.com/davidhalter/jedi.git
synced 2026-05-25 17:58:35 +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()
|
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:
|
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
|
# 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
|
# name/param/keyword is always it's parent context, not the
|
||||||
|
|||||||
+1
-3
@@ -17,8 +17,6 @@ import re
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from parso.tree import search_ancestor
|
|
||||||
|
|
||||||
from jedi import settings
|
from jedi import settings
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
from jedi.inference.utils import unite
|
from jedi.inference.utils import unite
|
||||||
@@ -509,7 +507,7 @@ class BaseName:
|
|||||||
# - param: The parent_context of a param is not its function but
|
# - param: The parent_context of a param is not its function but
|
||||||
# e.g. the outer class or module.
|
# e.g. the outer class or module.
|
||||||
cls_or_func_node = self._name.tree_name.get_definition()
|
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()
|
context = self._get_module_context().create_value(parent).as_context()
|
||||||
else:
|
else:
|
||||||
context = self._name.parent_context
|
context = self._name.parent_context
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ from inspect import Parameter
|
|||||||
|
|
||||||
from parso.python.token import PythonTokenTypes
|
from parso.python.token import PythonTokenTypes
|
||||||
from parso.python import tree
|
from parso.python import tree
|
||||||
from parso.tree import search_ancestor, Leaf
|
from parso.tree import Leaf
|
||||||
from parso import split_lines
|
from parso import split_lines
|
||||||
|
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
@@ -244,8 +244,8 @@ class Completion:
|
|||||||
if previous_leaf is not None:
|
if previous_leaf is not None:
|
||||||
stmt = previous_leaf
|
stmt = previous_leaf
|
||||||
while True:
|
while True:
|
||||||
stmt = search_ancestor(
|
stmt = stmt.search_ancestor(
|
||||||
stmt, 'if_stmt', 'for_stmt', 'while_stmt', 'try_stmt',
|
'if_stmt', 'for_stmt', 'while_stmt', 'try_stmt',
|
||||||
'error_node',
|
'error_node',
|
||||||
)
|
)
|
||||||
if stmt is None:
|
if stmt is None:
|
||||||
@@ -356,7 +356,7 @@ class Completion:
|
|||||||
stack_node = self.stack[-3]
|
stack_node = self.stack[-3]
|
||||||
if stack_node.nonterminal == 'funcdef':
|
if stack_node.nonterminal == 'funcdef':
|
||||||
context = get_user_context(self._module_context, self._position)
|
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 is not None:
|
||||||
if node.type == 'error_node':
|
if node.type == 'error_node':
|
||||||
n = node.children[0]
|
n = node.children[0]
|
||||||
@@ -426,7 +426,7 @@ class Completion:
|
|||||||
Autocomplete inherited methods when overriding in child class.
|
Autocomplete inherited methods when overriding in child class.
|
||||||
"""
|
"""
|
||||||
leaf = self._module_node.get_leaf_for_position(self._position, include_prefixes=True)
|
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:
|
if cls is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ from contextlib import contextmanager
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from parso.tree import search_ancestor
|
|
||||||
from parso.python.tree import Name
|
from parso.python.tree import Name
|
||||||
|
|
||||||
from jedi.inference.filters import ParserTreeFilter, MergedFilter, \
|
from jedi.inference.filters import ParserTreeFilter, MergedFilter, \
|
||||||
@@ -290,7 +289,7 @@ class TreeContextMixin:
|
|||||||
def create_name(self, tree_name):
|
def create_name(self, tree_name):
|
||||||
definition = tree_name.get_definition()
|
definition = tree_name.get_definition()
|
||||||
if definition and definition.type == 'param' and definition.name == tree_name:
|
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)
|
func = self.create_value(funcdef)
|
||||||
return AnonymousParamName(func, tree_name)
|
return AnonymousParamName(func, tree_name)
|
||||||
else:
|
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
|
# function and get inferred in the value before the function. So
|
||||||
# make sure to exclude the function/class name.
|
# make sure to exclude the function/class name.
|
||||||
if name_or_none is not None:
|
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
|
lambdef = None
|
||||||
if ancestor == 'lambdef':
|
if ancestor == 'lambdef':
|
||||||
# For lambdas it's even more complicated since parts will
|
# For lambdas it's even more complicated since parts will
|
||||||
# be inferred later.
|
# be inferred later.
|
||||||
lambdef = ancestor
|
lambdef = ancestor
|
||||||
ancestor = search_ancestor(name_or_none, 'funcdef', 'classdef')
|
ancestor = name_or_none.search_ancestor('funcdef', 'classdef')
|
||||||
if ancestor is not None:
|
if ancestor is not None:
|
||||||
colon = ancestor.children[-2]
|
colon = ancestor.children[-2]
|
||||||
if position is not None and position < colon.start_pos:
|
if position is not None and position < colon.start_pos:
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ from abc import abstractmethod
|
|||||||
from typing import List, MutableMapping, Type
|
from typing import List, MutableMapping, Type
|
||||||
import weakref
|
import weakref
|
||||||
|
|
||||||
from parso.tree import search_ancestor
|
|
||||||
from parso.python.tree import Name, UsedNamesMapping
|
from parso.python.tree import Name, UsedNamesMapping
|
||||||
|
|
||||||
from jedi.inference import flow_analysis
|
from jedi.inference import flow_analysis
|
||||||
@@ -181,7 +180,7 @@ class _FunctionExecutionFilter(ParserTreeFilter):
|
|||||||
@to_list
|
@to_list
|
||||||
def _convert_names(self, names):
|
def _convert_names(self, names):
|
||||||
for name in 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,
|
# 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
|
# because those are not definitions and never make it to this
|
||||||
# point.
|
# 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.
|
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 parso.python.tree import Name
|
||||||
|
|
||||||
from jedi import settings
|
from jedi import settings
|
||||||
@@ -76,7 +75,7 @@ def check_flow_information(value, flow, search_name, pos):
|
|||||||
])
|
])
|
||||||
|
|
||||||
for name in names:
|
for name in names:
|
||||||
ass = search_ancestor(name, 'assert_stmt')
|
ass = name.search_ancestor('assert_stmt')
|
||||||
if ass is not None:
|
if ass is not None:
|
||||||
result = _check_isinstance_type(value, ass.assertion, search_name)
|
result = _check_isinstance_type(value, ass.assertion, search_name)
|
||||||
if result is not None:
|
if result is not None:
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import os
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from parso.python import tree
|
from parso.python import tree
|
||||||
from parso.tree import search_ancestor
|
|
||||||
|
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
from jedi import settings
|
from jedi import settings
|
||||||
@@ -95,7 +94,7 @@ def goto_import(context, tree_name):
|
|||||||
|
|
||||||
|
|
||||||
def _prepare_infer_import(module_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)
|
import_path = import_node.get_path_for_name(tree_name)
|
||||||
from_import_name = None
|
from_import_name = None
|
||||||
try:
|
try:
|
||||||
@@ -549,7 +548,7 @@ def load_namespace_from_path(inference_state, folder_io):
|
|||||||
|
|
||||||
|
|
||||||
def follow_error_node_imports_if_possible(context, name):
|
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:
|
if error_node is not None:
|
||||||
# Get the first command start of a started simple_stmt. The error
|
# Get the first command start of a started simple_stmt. The error
|
||||||
# node is sometimes a small_stmt and sometimes a simple_stmt. Check
|
# 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 inspect import Parameter
|
||||||
from typing import Optional, Tuple
|
from typing import Optional, Tuple
|
||||||
|
|
||||||
from parso.tree import search_ancestor
|
|
||||||
|
|
||||||
from jedi.parser_utils import find_statement_documentation, clean_scope_docstring
|
from jedi.parser_utils import find_statement_documentation, clean_scope_docstring
|
||||||
from jedi.inference.utils import unite
|
from jedi.inference.utils import unite
|
||||||
from jedi.inference.base_value import ValueSet, NO_VALUES
|
from jedi.inference.base_value import ValueSet, NO_VALUES
|
||||||
@@ -112,7 +110,7 @@ class AbstractTreeName(AbstractNameDefinition):
|
|||||||
self.tree_name = tree_name
|
self.tree_name = tree_name
|
||||||
|
|
||||||
def get_qualified_names(self, include_module_names=False):
|
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
|
# 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.
|
# 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
|
# 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)
|
values = infer_call_of_leaf(context, name, cut_own_trailer=True)
|
||||||
return values.goto(name, name_context=context)
|
return values.goto(name, name_context=context)
|
||||||
else:
|
else:
|
||||||
stmt = search_ancestor(
|
stmt = name.search_ancestor('expr_stmt', 'lambdef') or name
|
||||||
name, 'expr_stmt', 'lambdef'
|
|
||||||
) or name
|
|
||||||
if stmt.type == 'lambdef':
|
if stmt.type == 'lambdef':
|
||||||
stmt = name
|
stmt = name
|
||||||
return context.goto(name, position=stmt.start_pos)
|
return context.goto(name, position=stmt.start_pos)
|
||||||
|
|
||||||
def is_import(self):
|
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
|
return imp is not None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -451,7 +447,7 @@ class _ActualTreeParamName(BaseTreeParamName):
|
|||||||
self.function_value = function_value
|
self.function_value = function_value
|
||||||
|
|
||||||
def _get_param_node(self):
|
def _get_param_node(self):
|
||||||
return search_ancestor(self.tree_name, 'param')
|
return self.tree_name.search_ancestor('param')
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def annotation_node(self):
|
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 inspect import Parameter
|
||||||
|
|
||||||
from parso import tree
|
|
||||||
|
|
||||||
from jedi.inference.utils import to_list
|
from jedi.inference.utils import to_list
|
||||||
from jedi.inference.names import ParamNameWrapper
|
from jedi.inference.names import ParamNameWrapper
|
||||||
from jedi.inference.helpers import is_big_annoying_library
|
from jedi.inference.helpers import is_big_annoying_library
|
||||||
|
|
||||||
|
|
||||||
def _iter_nodes_for_param(param_name):
|
def _iter_nodes_for_param(param_name):
|
||||||
from parso.python.tree import search_ancestor
|
|
||||||
from jedi.inference.arguments import TreeArguments
|
from jedi.inference.arguments import TreeArguments
|
||||||
|
|
||||||
execution_context = param_name.parent_context
|
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
|
# 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,
|
# the specific scope we're evaluating within (i.e: module or function,
|
||||||
# etc.).
|
# 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()
|
module_node = function_node.get_root_node()
|
||||||
start = function_node.children[-1].start_pos
|
start = function_node.children[-1].start_pos
|
||||||
end = function_node.children[-1].end_pos
|
end = function_node.children[-1].end_pos
|
||||||
@@ -38,7 +35,7 @@ def _iter_nodes_for_param(param_name):
|
|||||||
argument = name.parent
|
argument = name.parent
|
||||||
if argument.type == 'argument' \
|
if argument.type == 'argument' \
|
||||||
and argument.children[0] == '*' * param_name.star_count:
|
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
|
if trailer is not None: # Make sure we're in a function
|
||||||
context = execution_context.create_context(trailer)
|
context = execution_context.create_context(trailer)
|
||||||
if _goes_to_param_name(param_name, context, name):
|
if _goes_to_param_name(param_name, context, name):
|
||||||
|
|||||||
@@ -290,7 +290,7 @@ def infer_atom(context, atom):
|
|||||||
state = context.inference_state
|
state = context.inference_state
|
||||||
if atom.type == 'name':
|
if atom.type == 'name':
|
||||||
# This is the first global lookup.
|
# 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 stmt.type == 'if_stmt':
|
||||||
if not any(n.start_pos <= atom.start_pos < n.end_pos for n in stmt.get_test_nodes()):
|
if not any(n.start_pos <= atom.start_pos < n.end_pos for n in stmt.get_test_nodes()):
|
||||||
stmt = atom
|
stmt = atom
|
||||||
@@ -436,7 +436,7 @@ def _infer_expr_stmt(context, stmt, seek_name=None):
|
|||||||
else:
|
else:
|
||||||
operator = copy.copy(first_operator)
|
operator = copy.copy(first_operator)
|
||||||
operator.value = operator.value[:-1]
|
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 \
|
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):
|
and parser_utils.for_stmt_defines_one_name(for_stmt):
|
||||||
# Iterate through result and add the values, that's possible
|
# 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):
|
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:
|
if ancestor is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|||||||
@@ -262,8 +262,8 @@ class BaseFunctionExecutionContext(ValueContext, TreeContextMixin):
|
|||||||
@recursion.execution_recursion_decorator(default=iter([]))
|
@recursion.execution_recursion_decorator(default=iter([]))
|
||||||
def get_yield_lazy_values(self, is_async=False):
|
def get_yield_lazy_values(self, is_async=False):
|
||||||
# TODO: if is_async, wrap yield statements in Awaitable/async_generator_asend
|
# TODO: if is_async, wrap yield statements in Awaitable/async_generator_asend
|
||||||
for_parents = [(y, tree.search_ancestor(y, 'for_stmt', 'funcdef',
|
for_parents = [(y, y.search_ancestor('for_stmt', 'funcdef',
|
||||||
'while_stmt', 'if_stmt'))
|
'while_stmt', 'if_stmt'))
|
||||||
for y in get_yield_exprs(self.inference_state, self.tree_node)]
|
for y in get_yield_exprs(self.inference_state, self.tree_node)]
|
||||||
|
|
||||||
# Calculate if the yields are placed within the same for loop.
|
# Calculate if the yields are placed within the same for loop.
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
from abc import abstractproperty
|
from abc import abstractproperty
|
||||||
|
|
||||||
from parso.tree import search_ancestor
|
|
||||||
|
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
from jedi import settings
|
from jedi import settings
|
||||||
from jedi.inference import compiled
|
from jedi.inference import compiled
|
||||||
@@ -229,7 +227,7 @@ class _BaseTreeInstance(AbstractInstanceValue):
|
|||||||
new = node
|
new = node
|
||||||
while True:
|
while True:
|
||||||
func_node = new
|
func_node = new
|
||||||
new = search_ancestor(new, 'funcdef', 'classdef')
|
new = new.search_ancestor('funcdef', 'classdef')
|
||||||
if class_context.tree_node is new:
|
if class_context.tree_node is new:
|
||||||
func = FunctionValue.from_context(class_context, func_node)
|
func = FunctionValue.from_context(class_context, func_node)
|
||||||
bound_method = BoundMethod(self, class_context, func)
|
bound_method = BoundMethod(self, class_context, func)
|
||||||
@@ -498,7 +496,7 @@ class SelfName(TreeNameDefinition):
|
|||||||
return self._instance
|
return self._instance
|
||||||
|
|
||||||
def infer(self):
|
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 is not None:
|
||||||
if stmt.children[1].type == "annassign":
|
if stmt.children[1].type == "annassign":
|
||||||
from jedi.inference.gradual.annotation import infer_annotation
|
from jedi.inference.gradual.annotation import infer_annotation
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import sys
|
|||||||
from typing import List
|
from typing import List
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from parso.tree import search_ancestor
|
|
||||||
from jedi.inference.cache import inference_state_method_cache
|
from jedi.inference.cache import inference_state_method_cache
|
||||||
from jedi.inference.imports import goto_import, load_module_from_path
|
from jedi.inference.imports import goto_import, load_module_from_path
|
||||||
from jedi.inference.filters import ParserTreeFilter
|
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.
|
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
|
if funcdef is None: # A lambda
|
||||||
return False, False
|
return False, False
|
||||||
decorators = funcdef.get_decorators()
|
decorators = funcdef.get_decorators()
|
||||||
|
|||||||
Reference in New Issue
Block a user