Refactor params and what execution contexts need

This commit is contained in:
Dave Halter
2019-09-01 14:14:42 +02:00
parent 59f26ad6ab
commit edb17b8e7c
13 changed files with 142 additions and 123 deletions
+8 -32
View File
@@ -69,8 +69,8 @@ class FunctionMixin(object):
return ValueSet([BoundMethod(instance, self)])
def get_param_names(self):
function_execution = self.as_context()
return [ParamName(function_execution, param.name)
arguments = AnonymousArguments()
return [ParamName(self, param.name, arguments)
for param in self.tree_node.get_params()]
@property
@@ -282,29 +282,11 @@ class FunctionExecutionContext(ValueContext, TreeContextMixin):
)
def get_filters(self, until_position=None, origin_scope=None):
yield self.function_execution_filter(self,
until_position=until_position,
origin_scope=origin_scope)
yield FunctionExecutionFilter(
self, self._value, until_position=until_position, origin_scope=origin_scope)
def get_executed_param_names_and_issues(self):
return self.var_args.get_executed_param_names_and_issues(self)
def matches_signature(self):
executed_param_names, issues = self.get_executed_param_names_and_issues()
if issues:
return False
matches = all(executed_param_name.matches_signature()
for executed_param_name in executed_param_names)
if debug.enable_notice:
signature = parser_utils.get_call_signature(self.tree_node)
if matches:
debug.dbg("Overloading match: %s@%s (%s)",
signature, self.tree_node.start_pos[0], self.var_args, color='BLUE')
else:
debug.dbg("Overloading no match: %s@%s (%s)",
signature, self.tree_node.start_pos[0], self.var_args, color='BLUE')
return matches
return self.var_args.get_executed_param_names_and_issues(self._value)
def infer(self):
"""
@@ -355,18 +337,12 @@ class OverloadedFunctionValue(FunctionMixin, ValueWrapper):
def py__call__(self, arguments):
debug.dbg("Execute overloaded function %s", self._wrapped_value, color='BLUE')
function_executions = []
value_set = NO_VALUES
matched = False
for f in self._overloaded_functions:
function_execution = f.as_context(arguments)
for signature in self.get_signatures():
function_execution = signature.value.as_context(arguments)
function_executions.append(function_execution)
if function_execution.matches_signature():
matched = True
if signature.matches_signature(arguments):
return function_execution.infer()
if matched:
return value_set
if self.inference_state.is_analysis:
# In this case we want precision.
return NO_VALUES
+16 -15
View File
@@ -22,8 +22,9 @@ from jedi.parser_utils import get_parent_scope
class InstanceExecutedParamName(ParamName):
def __init__(self, instance, execution_context, tree_param):
super(InstanceExecutedParamName, self).__init__(execution_context, tree_param.name)
def __init__(self, instance, function_value, tree_param):
super(InstanceExecutedParamName, self).__init__(
function_value, tree_param.name, arguments=None)
self._instance = instance
def infer(self):
@@ -37,22 +38,22 @@ class AnonymousInstanceArguments(AnonymousArguments):
def __init__(self, instance):
self._instance = instance
def get_executed_param_names_and_issues(self, execution_context):
def get_executed_param_names_and_issues(self, function_value):
from jedi.inference.dynamic_params import search_param_names
tree_params = execution_context.tree_node.get_params()
tree_params = function_value.tree_node.get_params()
if not tree_params:
return [], []
self_param = InstanceExecutedParamName(
self._instance, execution_context, tree_params[0])
self._instance, function_value, tree_params[0])
if len(tree_params) == 1:
# If the only param is self, we don't need to try to find
# executions of this function, we have all the params already.
return [self_param], []
executed_param_names = list(search_param_names(
execution_context.inference_state,
execution_context,
execution_context.tree_node
function_value.inference_state,
function_value,
function_value.tree_node
))
executed_param_names[0] = self_param
return executed_param_names, []
@@ -289,16 +290,16 @@ class TreeInstance(AbstractInstanceValue):
from jedi.inference.gradual.annotation import py__annotations__, \
infer_type_vars_for_execution
args = InstanceArguments(self, self.var_args)
for signature in self.class_value.py__getattribute__('__init__').get_signatures():
# Just take the first result, it should always be one, because we
# control the typeshed code.
bound_method = BoundMethod(self, signature.value)
execution = bound_method.as_context(self.var_args)
if not execution.matches_signature():
if not signature.matches_signature(args):
# First check if the signature even matches, if not we don't
# need to infer anything.
continue
bound_method = BoundMethod(self, signature.value)
execution = bound_method.as_context(self.var_args)
all_annotations = py__annotations__(execution.tree_node)
type_var_dict = infer_type_vars_for_execution(execution, all_annotations)
if type_var_dict:
@@ -574,8 +575,8 @@ class InstanceArguments(TreeArgumentsWrapper):
for values in self._wrapped_arguments.unpack(func):
yield values
def get_executed_param_names_and_issues(self, execution_context):
def get_executed_param_names_and_issues(self, function_value):
if isinstance(self._wrapped_arguments, AnonymousInstanceArguments):
return self._wrapped_arguments.get_executed_param_names_and_issues(execution_context)
return self._wrapped_arguments.get_executed_param_names_and_issues(function_value)
return super(InstanceArguments, self).get_executed_param_names_and_issues(execution_context)
return super(InstanceArguments, self).get_executed_param_names_and_issues(function_value)
+3
View File
@@ -561,6 +561,9 @@ class FakeDict(_DictMixin, Sequence):
def exact_key_items(self):
return self._dct.items()
def __repr__(self):
return '<%s: %s>' % (self.__class__.__name__, self._dct)
class MergedArray(Sequence):
def __init__(self, inference_state, arrays):