1
0
forked from VimPlug/jedi

execution_value -> execution_context

This commit is contained in:
Dave Halter
2019-08-17 17:13:29 +02:00
parent 680388a7e8
commit a9b1de7060
9 changed files with 76 additions and 77 deletions

View File

@@ -144,8 +144,8 @@ class _AbstractArgumentsMixin(object):
def unpack(self, funcdef=None): def unpack(self, funcdef=None):
raise NotImplementedError raise NotImplementedError
def get_executed_params_and_issues(self, execution_value): def get_executed_params_and_issues(self, execution_context):
return get_executed_params_and_issues(execution_value, self) return get_executed_params_and_issues(execution_context, self)
def get_calling_nodes(self): def get_calling_nodes(self):
return [] return []
@@ -158,12 +158,12 @@ class AbstractArguments(_AbstractArgumentsMixin):
class AnonymousArguments(AbstractArguments): class AnonymousArguments(AbstractArguments):
def get_executed_params_and_issues(self, execution_value): def get_executed_params_and_issues(self, execution_context):
from jedi.inference.dynamic import search_params from jedi.inference.dynamic import search_params
return search_params( return search_params(
execution_value.inference_state, execution_context.inference_state,
execution_value, execution_context,
execution_value.tree_node execution_context.tree_node
), [] ), []
def __repr__(self): def __repr__(self):

View File

@@ -27,7 +27,6 @@ class AbstractContext(object):
def goto(self, name_or_str, position): def goto(self, name_or_str, position):
from jedi.inference import finder from jedi.inference import finder
f = finder.NameFinder(self.inference_state, self, self, name_or_str, position) f = finder.NameFinder(self.inference_state, self, self, name_or_str, position)
raise NotImplementedError('this does not seem to be correct')
filters = f.get_global_filters() filters = f.get_global_filters()
return f.filter_name(filters) return f.filter_name(filters)

View File

@@ -227,12 +227,12 @@ def _infer_for_statement_string(module_value, string):
module_value, module_value,
funcdef funcdef
) )
func_execution_value = function_value.get_function_execution() func_execution_context = function_value.get_function_execution()
# Use the module of the param. # Use the module of the param.
# TODO this module is not the module of the param in case of a function # TODO this module is not the module of the param in case of a function
# call. In that case it's the module of the function call. # call. In that case it's the module of the function call.
# stuffed with content from a function call. # stuffed with content from a function call.
return list(_execute_types_in_stmt(func_execution_value, stmt)) return list(_execute_types_in_stmt(func_execution_context, stmt))
def _execute_types_in_stmt(module_value, stmt): def _execute_types_in_stmt(module_value, stmt):
@@ -268,7 +268,7 @@ def _execute_array_values(inference_state, array):
@inference_state_method_cache() @inference_state_method_cache()
def infer_param(execution_value, param): def infer_param(execution_context, param):
from jedi.inference.value.instance import InstanceArguments from jedi.inference.value.instance import InstanceArguments
from jedi.inference.value import FunctionExecutionContext from jedi.inference.value import FunctionExecutionContext
@@ -278,16 +278,16 @@ def infer_param(execution_value, param):
for param_str in _search_param_in_docstr(docstring, param.name.value) for param_str in _search_param_in_docstr(docstring, param.name.value)
for p in _infer_for_statement_string(module_context, param_str) for p in _infer_for_statement_string(module_context, param_str)
) )
module_context = execution_value.get_root_context() module_context = execution_context.get_root_context()
func = param.get_parent_function() func = param.get_parent_function()
if func.type == 'lambdef': if func.type == 'lambdef':
return NO_VALUES return NO_VALUES
types = infer_docstring(execution_value.py__doc__()) types = infer_docstring(execution_context.py__doc__())
if isinstance(execution_value, FunctionExecutionContext) \ if isinstance(execution_context, FunctionExecutionContext) \
and isinstance(execution_value.var_args, InstanceArguments) \ and isinstance(execution_context.var_args, InstanceArguments) \
and execution_value.function_value.py__name__() == '__init__': and execution_context.function_value.py__name__() == '__init__':
class_value = execution_value.var_args.instance.class_value class_value = execution_context.var_args.instance.class_value
types |= infer_docstring(class_value.py__doc__()) types |= infer_docstring(class_value.py__doc__())
debug.dbg('Found param types for docstring: %s', types, color='BLUE') debug.dbg('Found param types for docstring: %s', types, color='BLUE')

View File

@@ -54,7 +54,7 @@ class DynamicExecutedParams(object):
@debug.increase_indent @debug.increase_indent
def search_params(inference_state, execution_value, funcdef): def search_params(inference_state, execution_context, funcdef):
""" """
A dynamic search for param values. If you try to complete a type: A dynamic search for param values. If you try to complete a type:
@@ -68,28 +68,28 @@ def search_params(inference_state, execution_value, funcdef):
is. is.
""" """
if not settings.dynamic_params: if not settings.dynamic_params:
return create_default_params(execution_value, funcdef) return create_default_params(execution_context, funcdef)
inference_state.dynamic_params_depth += 1 inference_state.dynamic_params_depth += 1
try: try:
path = execution_value.get_root_context().py__file__() path = execution_context.get_root_context().py__file__()
if path is not None and is_stdlib_path(path): if path is not None and is_stdlib_path(path):
# We don't want to search for usages in the stdlib. Usually people # We don't want to search for usages in the stdlib. Usually people
# don't work with it (except if you are a core maintainer, sorry). # don't work with it (except if you are a core maintainer, sorry).
# This makes everything slower. Just disable it and run the tests, # This makes everything slower. Just disable it and run the tests,
# you will see the slowdown, especially in 3.6. # you will see the slowdown, especially in 3.6.
return create_default_params(execution_value, funcdef) return create_default_params(execution_context, funcdef)
if funcdef.type == 'lambdef': if funcdef.type == 'lambdef':
string_name = _get_lambda_name(funcdef) string_name = _get_lambda_name(funcdef)
if string_name is None: if string_name is None:
return create_default_params(execution_value, funcdef) return create_default_params(execution_context, funcdef)
else: else:
string_name = funcdef.name.value string_name = funcdef.name.value
debug.dbg('Dynamic param search in %s.', string_name, color='MAGENTA') debug.dbg('Dynamic param search in %s.', string_name, color='MAGENTA')
try: try:
module_context = execution_value.get_root_context() module_context = execution_context.get_root_context()
function_executions = _search_function_executions( function_executions = _search_function_executions(
inference_state, inference_state,
module_context, module_context,
@@ -105,7 +105,7 @@ def search_params(inference_state, execution_value, funcdef):
for executed_params in zipped_params] for executed_params in zipped_params]
# Inferes the ExecutedParams to types. # Inferes the ExecutedParams to types.
else: else:
return create_default_params(execution_value, funcdef) return create_default_params(execution_context, funcdef)
finally: finally:
debug.dbg('Dynamic param result finished', color='MAGENTA') debug.dbg('Dynamic param result finished', color='MAGENTA')
return params return params
@@ -218,11 +218,11 @@ def _check_name_for_execution(inference_state, value, compare_node, name, traile
if nodes == [compare_node]: if nodes == [compare_node]:
# Found a decorator. # Found a decorator.
module_value = value.get_root_context() module_value = value.get_root_context()
execution_value = next(create_func_excs()) execution_context = next(create_func_excs())
for name, trailer in _get_possible_nodes(module_context, params[0].string_name): for name, trailer in _get_possible_nodes(module_context, params[0].string_name):
if value_node.start_pos < name.start_pos < value_node.end_pos: if value_node.start_pos < name.start_pos < value_node.end_pos:
raise NotImplementedError raise NotImplementedError
random_value = inference_state.create_context(execution_value, name) random_value = inference_state.create_context(execution_context, name)
iterator = _check_name_for_execution( iterator = _check_name_for_execution(
inference_state, inference_state,
random_value, random_value,

View File

@@ -107,9 +107,9 @@ def _split_comment_param_declaration(decl_text):
@inference_state_method_cache() @inference_state_method_cache()
def infer_param(execution_value, param): def infer_param(execution_context, param):
values = _infer_param(execution_value, param) values = _infer_param(execution_context, param)
inference_state = execution_value.inference_state inference_state = execution_context.inference_state
if param.star_count == 1: if param.star_count == 1:
tuple_ = builtin_from_name(inference_state, 'tuple') tuple_ = builtin_from_name(inference_state, 'tuple')
return ValueSet([GenericClass( return ValueSet([GenericClass(
@@ -126,7 +126,7 @@ def infer_param(execution_value, param):
return values return values
def _infer_param(execution_value, param): def _infer_param(execution_context, param):
""" """
Infers the type of a function parameter, using type annotations. Infers the type of a function parameter, using type annotations.
""" """
@@ -159,7 +159,7 @@ def _infer_param(execution_value, param):
params_comments, all_params params_comments, all_params
) )
from jedi.inference.value.instance import InstanceArguments from jedi.inference.value.instance import InstanceArguments
if isinstance(execution_value.var_args, InstanceArguments): if isinstance(execution_context.var_args, InstanceArguments):
if index == 0: if index == 0:
# Assume it's self, which is already handled # Assume it's self, which is already handled
return NO_VALUES return NO_VALUES
@@ -169,11 +169,11 @@ def _infer_param(execution_value, param):
param_comment = params_comments[index] param_comment = params_comments[index]
return _infer_annotation_string( return _infer_annotation_string(
execution_value.function_value.get_default_param_value(), execution_context.function_value.get_default_param_value(),
param_comment param_comment
) )
# Annotations are like default params and resolve in the same way. # Annotations are like default params and resolve in the same way.
value = execution_value.function_value.get_default_param_value() value = execution_context.function_value.get_default_param_value()
return infer_annotation(value, annotation) return infer_annotation(value, annotation)
@@ -191,16 +191,16 @@ def py__annotations__(funcdef):
@inference_state_method_cache() @inference_state_method_cache()
def infer_return_types(function_execution_value): def infer_return_types(function_execution_context):
""" """
Infers the type of a function's return value, Infers the type of a function's return value,
according to type annotations. according to type annotations.
""" """
all_annotations = py__annotations__(function_execution_value.tree_node) all_annotations = py__annotations__(function_execution_context.tree_node)
annotation = all_annotations.get("return", None) annotation = all_annotations.get("return", None)
if annotation is None: if annotation is None:
# If there is no Python 3-type annotation, look for a Python 2-type annotation # If there is no Python 3-type annotation, look for a Python 2-type annotation
node = function_execution_value.tree_node node = function_execution_context.tree_node
comment = parser_utils.get_following_comment_same_line(node) comment = parser_utils.get_following_comment_same_line(node)
if comment is None: if comment is None:
return NO_VALUES return NO_VALUES
@@ -210,19 +210,19 @@ def infer_return_types(function_execution_value):
return NO_VALUES return NO_VALUES
return _infer_annotation_string( return _infer_annotation_string(
function_execution_value.function_value.get_default_param_value(), function_execution_context.function_value.get_default_param_value(),
match.group(1).strip() match.group(1).strip()
).execute_annotation() ).execute_annotation()
if annotation is None: if annotation is None:
return NO_VALUES return NO_VALUES
value = function_execution_value.function_value.get_default_param_value() value = function_execution_context.function_value.get_default_param_value()
unknown_type_vars = list(find_unknown_type_vars(value, annotation)) unknown_type_vars = list(find_unknown_type_vars(value, annotation))
annotation_values = infer_annotation(value, annotation) annotation_values = infer_annotation(value, annotation)
if not unknown_type_vars: if not unknown_type_vars:
return annotation_values.execute_annotation() return annotation_values.execute_annotation()
type_var_dict = infer_type_vars_for_execution(function_execution_value, all_annotations) type_var_dict = infer_type_vars_for_execution(function_execution_context, all_annotations)
return ValueSet.from_sets( return ValueSet.from_sets(
ann.define_generics(type_var_dict) ann.define_generics(type_var_dict)
@@ -231,7 +231,7 @@ def infer_return_types(function_execution_value):
).execute_annotation() ).execute_annotation()
def infer_type_vars_for_execution(execution_value, annotation_dict): def infer_type_vars_for_execution(execution_context, annotation_dict):
""" """
Some functions use type vars that are not defined by the class, but rather Some functions use type vars that are not defined by the class, but rather
only defined in the function. See for example `iter`. In those cases we only defined in the function. See for example `iter`. In those cases we
@@ -241,10 +241,10 @@ def infer_type_vars_for_execution(execution_value, annotation_dict):
2. Infer type vars with the execution state we have. 2. Infer type vars with the execution state we have.
3. Return the union of all type vars that have been found. 3. Return the union of all type vars that have been found.
""" """
value = execution_value.function_value.get_default_param_value() value = execution_context.function_value.get_default_param_value()
annotation_variable_results = {} annotation_variable_results = {}
executed_params, _ = execution_value.get_executed_params_and_issues() executed_params, _ = execution_context.get_executed_params_and_issues()
for executed_param in executed_params: for executed_param in executed_params:
try: try:
annotation_node = annotation_dict[executed_param.string_name] annotation_node = annotation_dict[executed_param.string_name]

View File

@@ -19,8 +19,8 @@ def _add_argument_issue(error_name, lazy_value, message):
class ExecutedParam(object): class ExecutedParam(object):
"""Fake a param and give it values.""" """Fake a param and give it values."""
def __init__(self, execution_value, param_node, lazy_value, is_default=False): def __init__(self, execution_context, param_node, lazy_value, is_default=False):
self._execution_value = execution_value self._execution_context = execution_context
self._param_node = param_node self._param_node = param_node
self._lazy_value = lazy_value self._lazy_value = lazy_value
self.string_name = param_node.name.value self.string_name = param_node.name.value
@@ -28,11 +28,11 @@ class ExecutedParam(object):
def infer_annotations(self): def infer_annotations(self):
from jedi.inference.gradual.annotation import infer_param from jedi.inference.gradual.annotation import infer_param
return infer_param(self._execution_value, self._param_node) return infer_param(self._execution_context, self._param_node)
def infer(self, use_hints=True): def infer(self, use_hints=True):
if use_hints: if use_hints:
doc_params = docstrings.infer_param(self._execution_value, self._param_node) doc_params = docstrings.infer_param(self._execution_context, self._param_node)
ann = self.infer_annotations().execute_annotation() ann = self.infer_annotations().execute_annotation()
if ann or doc_params: if ann or doc_params:
return ann | doc_params return ann | doc_params
@@ -59,13 +59,13 @@ class ExecutedParam(object):
@property @property
def var_args(self): def var_args(self):
return self._execution_value.var_args return self._execution_context.var_args
def __repr__(self): def __repr__(self):
return '<%s: %s>' % (self.__class__.__name__, self.string_name) return '<%s: %s>' % (self.__class__.__name__, self.string_name)
def get_executed_params_and_issues(execution_value, arguments): def get_executed_params_and_issues(execution_context, arguments):
def too_many_args(argument): def too_many_args(argument):
m = _error_argument_count(funcdef, len(unpacked_va)) m = _error_argument_count(funcdef, len(unpacked_va))
# Just report an error for the first param that is not needed (like # Just report an error for the first param that is not needed (like
@@ -85,11 +85,11 @@ def get_executed_params_and_issues(execution_value, arguments):
issues = [] # List[Optional[analysis issue]] issues = [] # List[Optional[analysis issue]]
result_params = [] result_params = []
param_dict = {} param_dict = {}
funcdef = execution_value.tree_node funcdef = execution_context.tree_node
# Default params are part of the value where the function was defined. # Default params are part of the value where the function was defined.
# This means that they might have access on class variables that the # This means that they might have access on class variables that the
# function itself doesn't have. # function itself doesn't have.
default_param_value = execution_value.function_value.get_default_param_value() default_param_value = execution_context.function_value.get_default_param_value()
for param in funcdef.get_params(): for param in funcdef.get_params():
param_dict[param.name.value] = param param_dict[param.name.value] = param
@@ -125,7 +125,7 @@ def get_executed_params_and_issues(execution_value, arguments):
valueualized_node.node, message=m) valueualized_node.node, message=m)
) )
else: else:
keys_used[key] = ExecutedParam(execution_value, key_param, argument) keys_used[key] = ExecutedParam(execution_context, key_param, argument)
key, argument = next(var_arg_iterator, (None, None)) key, argument = next(var_arg_iterator, (None, None))
try: try:
@@ -145,13 +145,13 @@ def get_executed_params_and_issues(execution_value, arguments):
var_arg_iterator.push_back((key, argument)) var_arg_iterator.push_back((key, argument))
break break
lazy_value_list.append(argument) lazy_value_list.append(argument)
seq = iterable.FakeSequence(execution_value.inference_state, u'tuple', lazy_value_list) seq = iterable.FakeSequence(execution_context.inference_state, u'tuple', lazy_value_list)
result_arg = LazyKnownValue(seq) result_arg = LazyKnownValue(seq)
elif param.star_count == 2: elif param.star_count == 2:
if argument is not None: if argument is not None:
too_many_args(argument) too_many_args(argument)
# **kwargs param # **kwargs param
dct = iterable.FakeDict(execution_value.inference_state, dict(non_matching_keys)) dct = iterable.FakeDict(execution_context.inference_state, dict(non_matching_keys))
result_arg = LazyKnownValue(dct) result_arg = LazyKnownValue(dct)
non_matching_keys = {} non_matching_keys = {}
else: else:
@@ -178,7 +178,7 @@ def get_executed_params_and_issues(execution_value, arguments):
result_arg = argument result_arg = argument
result_params.append(ExecutedParam( result_params.append(ExecutedParam(
execution_value, param, result_arg, execution_context, param, result_arg,
is_default=is_default is_default=is_default
)) ))
if not isinstance(result_arg, LazyUnknownValue): if not isinstance(result_arg, LazyUnknownValue):
@@ -232,22 +232,22 @@ def _error_argument_count(funcdef, actual_count):
% (funcdef.name, before, len(params), actual_count)) % (funcdef.name, before, len(params), actual_count))
def _create_default_param(execution_value, param): def _create_default_param(execution_context, param):
if param.star_count == 1: if param.star_count == 1:
result_arg = LazyKnownValue( result_arg = LazyKnownValue(
iterable.FakeSequence(execution_value.inference_state, u'tuple', []) iterable.FakeSequence(execution_context.inference_state, u'tuple', [])
) )
elif param.star_count == 2: elif param.star_count == 2:
result_arg = LazyKnownValue( result_arg = LazyKnownValue(
iterable.FakeDict(execution_value.inference_state, {}) iterable.FakeDict(execution_context.inference_state, {})
) )
elif param.default is None: elif param.default is None:
result_arg = LazyUnknownValue() result_arg = LazyUnknownValue()
else: else:
result_arg = LazyTreeValue(execution_value.parent_context, param.default) result_arg = LazyTreeValue(execution_context.parent_context, param.default)
return ExecutedParam(execution_value, param, result_arg) return ExecutedParam(execution_context, param, result_arg)
def create_default_params(execution_value, funcdef): def create_default_params(execution_context, funcdef):
return [_create_default_param(execution_value, p) return [_create_default_param(execution_context, p)
for p in funcdef.get_params()] for p in funcdef.get_params()]

View File

@@ -20,8 +20,8 @@ def _iter_nodes_for_param(param_name):
from parso.python.tree import search_ancestor from parso.python.tree import search_ancestor
from jedi.inference.arguments import TreeArguments from jedi.inference.arguments import TreeArguments
execution_value = param_name.parent_context execution_context = param_name.parent_context
function_node = execution_value.tree_node function_node = execution_context.tree_node
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
@@ -36,12 +36,12 @@ def _iter_nodes_for_param(param_name):
trailer = search_ancestor(argument, 'trailer') trailer = search_ancestor(argument, '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
raise NotImplementedError raise NotImplementedError
context = execution_value.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):
values = _to_callables(context, trailer) values = _to_callables(context, trailer)
args = TreeArguments.create_cached( args = TreeArguments.create_cached(
execution_value.inference_state, execution_context.inference_state,
context=context, context=context,
argument_node=trailer.children[1], argument_node=trailer.children[1],
trailer=trailer, trailer=trailer,

View File

@@ -38,9 +38,9 @@ class AnonymousInstanceArguments(AnonymousArguments):
def __init__(self, instance): def __init__(self, instance):
self._instance = instance self._instance = instance
def get_executed_params_and_issues(self, execution_value): def get_executed_params_and_issues(self, execution_context):
from jedi.inference.dynamic import search_params from jedi.inference.dynamic import search_params
tree_params = execution_value.tree_node.get_params() tree_params = execution_context.tree_node.get_params()
if not tree_params: if not tree_params:
return [], [] return [], []
@@ -50,9 +50,9 @@ class AnonymousInstanceArguments(AnonymousArguments):
# executions of this function, we have all the params already. # executions of this function, we have all the params already.
return [self_param], [] return [self_param], []
executed_params = list(search_params( executed_params = list(search_params(
execution_value.inference_state, execution_context.inference_state,
execution_value, execution_context,
execution_value.tree_node execution_context.tree_node
)) ))
executed_params[0] = self_param executed_params[0] = self_param
return executed_params, [] return executed_params, []
@@ -520,8 +520,8 @@ class InstanceArguments(TreeArgumentsWrapper):
for values in self._wrapped_arguments.unpack(func): for values in self._wrapped_arguments.unpack(func):
yield values yield values
def get_executed_params_and_issues(self, execution_value): def get_executed_params_and_issues(self, execution_context):
if isinstance(self._wrapped_arguments, AnonymousInstanceArguments): if isinstance(self._wrapped_arguments, AnonymousInstanceArguments):
return self._wrapped_arguments.get_executed_params_and_issues(execution_value) return self._wrapped_arguments.get_executed_params_and_issues(execution_context)
return super(InstanceArguments, self).get_executed_params_and_issues(execution_value) return super(InstanceArguments, self).get_executed_params_and_issues(execution_context)

View File

@@ -97,18 +97,18 @@ class GeneratorBase(LazyAttributeOverwrite, IterableMixin):
class Generator(GeneratorBase): class Generator(GeneratorBase):
"""Handling of `yield` functions.""" """Handling of `yield` functions."""
def __init__(self, inference_state, func_execution_value): def __init__(self, inference_state, func_execution_context):
super(Generator, self).__init__(inference_state) super(Generator, self).__init__(inference_state)
self._func_execution_value = func_execution_value self._func_execution_context = func_execution_context
def py__iter__(self, valueualized_node=None): def py__iter__(self, valueualized_node=None):
return self._func_execution_value.get_yield_lazy_values() return self._func_execution_context.get_yield_lazy_values()
def py__stop_iteration_returns(self): def py__stop_iteration_returns(self):
return self._func_execution_value.get_return_values() return self._func_execution_context.get_return_values()
def __repr__(self): def __repr__(self):
return "<%s of %s>" % (type(self).__name__, self._func_execution_value) return "<%s of %s>" % (type(self).__name__, self._func_execution_context)
class CompForValue(TreeValue): class CompForValue(TreeValue):