1
0
forked from VimPlug/jedi

Use function/arguments intead of execution

This commit is contained in:
Dave Halter
2019-09-02 21:50:56 +02:00
parent bdb01c7546
commit 7ad7d22fb0
3 changed files with 14 additions and 16 deletions

View File

@@ -193,16 +193,16 @@ def py__annotations__(funcdef):
@inference_state_method_cache() @inference_state_method_cache()
def infer_return_types(function_execution_context): def infer_return_types(function, arguments):
""" """
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_context.tree_node) all_annotations = py__annotations__(function.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_context.tree_node node = function.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
@@ -212,19 +212,19 @@ def infer_return_types(function_execution_context):
return NO_VALUES return NO_VALUES
return _infer_annotation_string( return _infer_annotation_string(
function_execution_context.function_value.get_default_param_context(), function.get_default_param_context(),
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
context = function_execution_context.function_value.get_default_param_context() context = function.get_default_param_context()
unknown_type_vars = list(find_unknown_type_vars(context, annotation)) unknown_type_vars = list(find_unknown_type_vars(context, annotation))
annotation_values = infer_annotation(context, annotation) annotation_values = infer_annotation(context, 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_context, all_annotations) type_var_dict = infer_type_vars_for_execution(function, arguments, all_annotations)
return ValueSet.from_sets( return ValueSet.from_sets(
ann.define_generics(type_var_dict) ann.define_generics(type_var_dict)
@@ -233,7 +233,7 @@ def infer_return_types(function_execution_context):
).execute_annotation() ).execute_annotation()
def infer_type_vars_for_execution(execution_context, annotation_dict): def infer_type_vars_for_execution(function, arguments, 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
@@ -243,10 +243,10 @@ def infer_type_vars_for_execution(execution_context, 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.
""" """
context = execution_context.function_value.get_default_param_context() context = function.get_default_param_context()
annotation_variable_results = {} annotation_variable_results = {}
executed_param_names, _ = execution_context.get_executed_param_names_and_issues() executed_param_names, _ = arguments.get_executed_param_names_and_issues(function)
for executed_param_name in executed_param_names: for executed_param_name in executed_param_names:
try: try:
annotation_node = annotation_dict[executed_param_name.string_name] annotation_node = annotation_dict[executed_param_name.string_name]

View File

@@ -162,7 +162,6 @@ class MethodValue(FunctionValue):
class FunctionExecutionContext(ValueContext, TreeContextMixin): class FunctionExecutionContext(ValueContext, TreeContextMixin):
def __init__(self, function_value, arguments): def __init__(self, function_value, arguments):
super(FunctionExecutionContext, self).__init__(function_value) super(FunctionExecutionContext, self).__init__(function_value)
self.function_value = function_value
self._arguments = arguments self._arguments = arguments
@inference_state_method_cache(default=NO_VALUES) @inference_state_method_cache(default=NO_VALUES)
@@ -178,12 +177,12 @@ class FunctionExecutionContext(ValueContext, TreeContextMixin):
else: else:
returns = funcdef.iter_return_stmts() returns = funcdef.iter_return_stmts()
from jedi.inference.gradual.annotation import infer_return_types from jedi.inference.gradual.annotation import infer_return_types
value_set = infer_return_types(self) value_set = infer_return_types(self._value, self._arguments)
if value_set: if value_set:
# If there are annotations, prefer them over anything else. # If there are annotations, prefer them over anything else.
# This will make it faster. # This will make it faster.
return value_set return value_set
value_set |= docstrings.infer_return_types(self.function_value) value_set |= docstrings.infer_return_types(self._value)
for r in returns: for r in returns:
check = flow_analysis.reachability_check(self, funcdef, r) check = flow_analysis.reachability_check(self, funcdef, r)

View File

@@ -300,12 +300,11 @@ class TreeInstance(AbstractInstanceValue):
# need to infer anything. # need to infer anything.
continue continue
bound_method = BoundMethod(self, signature.value) bound_method = BoundMethod(self, signature.value)
execution = bound_method.as_context(self.arguments) all_annotations = py__annotations__(signature.value.tree_node)
all_annotations = py__annotations__(execution.tree_node) type_var_dict = infer_type_vars_for_execution(bound_method, args, all_annotations)
type_var_dict = infer_type_vars_for_execution(execution, all_annotations)
if type_var_dict: if type_var_dict:
defined, = self.class_value.define_generics( defined, = self.class_value.define_generics(
infer_type_vars_for_execution(execution, all_annotations), infer_type_vars_for_execution(signature.value, args, all_annotations),
) )
debug.dbg('Inferred instance value as %s', defined, color='BLUE') debug.dbg('Inferred instance value as %s', defined, color='BLUE')
return defined return defined