context -> value

This commit is contained in:
Dave Halter
2019-08-15 01:23:06 +02:00
parent 9e23f4d67b
commit ad4f546aca
68 changed files with 1931 additions and 1931 deletions
+67 -67
View File
@@ -76,10 +76,10 @@ from jedi.inference.cache import infer_state_function_cache
from jedi.inference import helpers
from jedi.inference.names import TreeNameDefinition, ParamName
from jedi.inference.base_value import ContextualizedName, ContextualizedNode, \
ContextSet, NO_CONTEXTS, iterate_contexts
from jedi.inference.context import ClassContext, FunctionContext, \
ContextSet, NO_CONTEXTS, iterate_values
from jedi.inference.value import ClassContext, FunctionContext, \
AnonymousInstance, BoundMethod
from jedi.inference.context.iterable import CompForContext
from jedi.inference.value.iterable import CompForContext
from jedi.inference.syntax_tree import infer_trailer, infer_expr_stmt, \
infer_node, check_tuple_assignments
from jedi.plugins import plugin_manager
@@ -111,21 +111,21 @@ class InferState(object):
self.reset_recursion_limitations()
self.allow_different_encoding = True
def import_module(self, import_names, parent_module_context=None,
def import_module(self, import_names, parent_module_value=None,
sys_path=None, prefer_stubs=True):
if sys_path is None:
sys_path = self.get_sys_path()
return imports.import_module(self, import_names, parent_module_context,
return imports.import_module(self, import_names, parent_module_value,
sys_path, prefer_stubs=prefer_stubs)
@staticmethod
@plugin_manager.decorate()
def execute(context, arguments):
debug.dbg('execute: %s %s', context, arguments)
def execute(value, arguments):
debug.dbg('execute: %s %s', value, arguments)
with debug.increase_indent_cm():
context_set = context.py__call__(arguments=arguments)
debug.dbg('execute result: %s in %s', context_set, context)
return context_set
value_set = value.py__call__(arguments=arguments)
debug.dbg('execute result: %s in %s', value_set, value)
return value_set
@property
@infer_state_function_cache()
@@ -150,9 +150,9 @@ class InferState(object):
"""Convenience function"""
return self.project._get_sys_path(self, environment=self.environment, **kwargs)
def infer_element(self, context, element):
if isinstance(context, CompForContext):
return infer_node(context, element)
def infer_element(self, value, element):
if isinstance(value, CompForContext):
return infer_node(value, element)
if_stmt = element
while if_stmt is not None:
@@ -162,7 +162,7 @@ class InferState(object):
if parser_utils.is_scope(if_stmt):
if_stmt = None
break
predefined_if_name_dict = context.predefined_names.get(if_stmt)
predefined_if_name_dict = value.predefined_names.get(if_stmt)
# TODO there's a lot of issues with this one. We actually should do
# this in a different way. Caching should only be active in certain
# cases and this all sucks.
@@ -171,7 +171,7 @@ class InferState(object):
if_stmt_test = if_stmt.children[1]
name_dicts = [{}]
# If we already did a check, we don't want to do it again -> If
# context.predefined_names is filled, we stop.
# value.predefined_names is filled, we stop.
# We don't want to check the if stmt itself, it's just about
# the content.
if element.start_pos > if_stmt_test.end_pos:
@@ -182,7 +182,7 @@ class InferState(object):
str_element_names = [e.value for e in element_names]
if any(i.value in str_element_names for i in if_names):
for if_name in if_names:
definitions = self.goto_definitions(context, if_name)
definitions = self.goto_definitions(value, if_name)
# Every name that has multiple different definitions
# causes the complexity to rise. The complexity should
# never fall below 1.
@@ -210,65 +210,65 @@ class InferState(object):
if len(name_dicts) > 1:
result = NO_CONTEXTS
for name_dict in name_dicts:
with helpers.predefine_names(context, if_stmt, name_dict):
result |= infer_node(context, element)
with helpers.predefine_names(value, if_stmt, name_dict):
result |= infer_node(value, element)
return result
else:
return self._infer_element_if_inferred(context, element)
return self._infer_element_if_inferred(value, element)
else:
if predefined_if_name_dict:
return infer_node(context, element)
return infer_node(value, element)
else:
return self._infer_element_if_inferred(context, element)
return self._infer_element_if_inferred(value, element)
def _infer_element_if_inferred(self, context, element):
def _infer_element_if_inferred(self, value, element):
"""
TODO This function is temporary: Merge with infer_element.
"""
parent = element
while parent is not None:
parent = parent.parent
predefined_if_name_dict = context.predefined_names.get(parent)
predefined_if_name_dict = value.predefined_names.get(parent)
if predefined_if_name_dict is not None:
return infer_node(context, element)
return self._infer_element_cached(context, element)
return infer_node(value, element)
return self._infer_element_cached(value, element)
@infer_state_function_cache(default=NO_CONTEXTS)
def _infer_element_cached(self, context, element):
return infer_node(context, element)
def _infer_element_cached(self, value, element):
return infer_node(value, element)
def goto_definitions(self, context, name):
def goto_definitions(self, value, name):
def_ = name.get_definition(import_name_always=True)
if def_ is not None:
type_ = def_.type
is_classdef = type_ == 'classdef'
if is_classdef or type_ == 'funcdef':
if is_classdef:
c = ClassContext(self, context, name.parent)
c = ClassContext(self, value, name.parent)
else:
c = FunctionContext.from_context(context, name.parent)
c = FunctionContext.from_value(value, name.parent)
return ContextSet([c])
if type_ == 'expr_stmt':
is_simple_name = name.parent.type not in ('power', 'trailer')
if is_simple_name:
return infer_expr_stmt(context, def_, name)
return infer_expr_stmt(value, def_, name)
if type_ == 'for_stmt':
container_types = context.infer_node(def_.children[3])
cn = ContextualizedNode(context, def_.children[3])
for_types = iterate_contexts(container_types, cn)
c_node = ContextualizedName(context, name)
container_types = value.infer_node(def_.children[3])
cn = ContextualizedNode(value, def_.children[3])
for_types = iterate_values(container_types, cn)
c_node = ContextualizedName(value, name)
return check_tuple_assignments(self, c_node, for_types)
if type_ in ('import_from', 'import_name'):
return imports.infer_import(context, name)
return imports.infer_import(value, name)
else:
result = self._follow_error_node_imports_if_possible(context, name)
result = self._follow_error_node_imports_if_possible(value, name)
if result is not None:
return result
return helpers.infer_call_of_leaf(context, name)
return helpers.infer_call_of_leaf(value, name)
def _follow_error_node_imports_if_possible(self, context, name):
def _follow_error_node_imports_if_possible(self, value, name):
error_node = tree.search_ancestor(name, 'error_node')
if error_node is not None:
# Get the first command start of a started simple_stmt. The error
@@ -292,10 +292,10 @@ class InferState(object):
is_import_from=is_import_from,
until_node=name,
)
return imports.Importer(self, names, context.get_root_context(), level).follow()
return imports.Importer(self, names, value.get_root_value(), level).follow()
return None
def goto(self, context, name):
def goto(self, value, name):
definition = name.get_definition(import_name_always=True)
if definition is not None:
type_ = definition.type
@@ -304,18 +304,18 @@ class InferState(object):
# a name it's something you can "goto" again.
is_simple_name = name.parent.type not in ('power', 'trailer')
if is_simple_name:
return [TreeNameDefinition(context, name)]
return [TreeNameDefinition(value, name)]
elif type_ == 'param':
return [ParamName(context, name)]
return [ParamName(value, name)]
elif type_ in ('import_from', 'import_name'):
module_names = imports.infer_import(context, name, is_goto=True)
module_names = imports.infer_import(value, name, is_goto=True)
return module_names
else:
return [TreeNameDefinition(context, name)]
return [TreeNameDefinition(value, name)]
else:
contexts = self._follow_error_node_imports_if_possible(context, name)
if contexts is not None:
return [context.name for context in contexts]
values = self._follow_error_node_imports_if_possible(value, name)
if values is not None:
return [value.name for value in values]
par = name.parent
node_type = par.type
@@ -326,18 +326,18 @@ class InferState(object):
trailer = trailer.parent
if trailer.type != 'classdef':
if trailer.type == 'decorator':
context_set = context.infer_node(trailer.children[1])
value_set = value.infer_node(trailer.children[1])
else:
i = trailer.parent.children.index(trailer)
to_infer = trailer.parent.children[:i]
if to_infer[0] == 'await':
to_infer.pop(0)
context_set = context.infer_node(to_infer[0])
value_set = value.infer_node(to_infer[0])
for trailer in to_infer[1:]:
context_set = infer_trailer(context, context_set, trailer)
value_set = infer_trailer(value, value_set, trailer)
param_names = []
for context in context_set:
for signature in context.get_signatures():
for value in value_set:
for signature in value.get_signatures():
for param_name in signature.get_param_names():
if param_name.string_name == name.value:
param_names.append(param_name)
@@ -347,28 +347,28 @@ class InferState(object):
if index > 0:
new_dotted = helpers.deep_ast_copy(par)
new_dotted.children[index - 1:] = []
values = context.infer_node(new_dotted)
values = value.infer_node(new_dotted)
return unite(
value.py__getattribute__(name, name_context=context, is_goto=True)
value.py__getattribute__(name, name_value=value, is_goto=True)
for value in values
)
if node_type == 'trailer' and par.children[0] == '.':
values = helpers.infer_call_of_leaf(context, name, cut_own_trailer=True)
return values.py__getattribute__(name, name_context=context, is_goto=True)
values = helpers.infer_call_of_leaf(value, name, cut_own_trailer=True)
return values.py__getattribute__(name, name_value=value, is_goto=True)
else:
stmt = tree.search_ancestor(
name, 'expr_stmt', 'lambdef'
) or name
if stmt.type == 'lambdef':
stmt = name
return context.py__getattribute__(
return value.py__getattribute__(
name,
position=stmt.start_pos,
search_global=True, is_goto=True
)
def create_context(self, base_value, node, node_is_context=False, node_is_object=False):
def create_value(self, base_value, node, node_is_value=False, node_is_object=False):
def parent_scope(node):
while True:
node = node.parent
@@ -390,13 +390,13 @@ class InferState(object):
is_funcdef = scope_node.type in ('funcdef', 'lambdef')
parent_scope = parser_utils.get_parent_scope(scope_node)
parent_context = from_scope_node(parent_scope)
parent_value = from_scope_node(parent_scope)
if is_funcdef:
func = FunctionContext.from_context(parent_context, scope_node)
if parent_context.is_class():
func = FunctionContext.from_value(parent_value, scope_node)
if parent_value.is_class():
instance = AnonymousInstance(
self, parent_context.parent_context, parent_context)
self, parent_value.parent_value, parent_value)
func = BoundMethod(
instance=instance,
function=func
@@ -406,16 +406,16 @@ class InferState(object):
return func.get_function_execution()
return func
elif scope_node.type == 'classdef':
return ClassContext(self, parent_context, scope_node)
return ClassContext(self, parent_value, scope_node)
elif scope_node.type in ('comp_for', 'sync_comp_for'):
if node.start_pos >= scope_node.children[-1].start_pos:
return parent_context
return CompForContext.from_comp_for(parent_context, scope_node)
return parent_value
return CompForContext.from_comp_for(parent_value, scope_node)
raise Exception("There's a scope that was not managed.")
base_node = base_value.tree_node
if node_is_context and parser_utils.is_scope(node):
if node_is_value and parser_utils.is_scope(node):
scope_node = node
else:
scope_node = parent_scope(node)