From f68d65ed59a6b81a6e3d06aa7b63c642765fd2df Mon Sep 17 00:00:00 2001 From: Peter Law Date: Wed, 18 Mar 2020 21:44:18 +0000 Subject: [PATCH] Push much looping and merging of infering type vars into ValueSet --- jedi/inference/base_value.py | 12 ++++++++ jedi/inference/gradual/annotation.py | 37 ++++++++++++------------- jedi/inference/gradual/base.py | 13 ++++----- jedi/inference/gradual/typing.py | 41 +++++++++++++--------------- 4 files changed, 54 insertions(+), 49 deletions(-) diff --git a/jedi/inference/base_value.py b/jedi/inference/base_value.py index 05134773..89cec757 100644 --- a/jedi/inference/base_value.py +++ b/jedi/inference/base_value.py @@ -437,6 +437,18 @@ class ValueSet(BaseValueSet): s = 'Optional[%s]' % s return s + def infer_type_vars(self, value_set, is_class_value=False): + # Circular + from jedi.inference.gradual.annotation import merge_type_var_dicts + + type_var_dict = {} + for value in self._set: + merge_type_var_dicts( + type_var_dict, + value.infer_type_vars(value_set, is_class_value), + ) + return type_var_dict + NO_VALUES = ValueSet([]) diff --git a/jedi/inference/gradual/annotation.py b/jedi/inference/gradual/annotation.py index 13c84295..eb6c0872 100644 --- a/jedi/inference/gradual/annotation.py +++ b/jedi/inference/gradual/annotation.py @@ -268,11 +268,10 @@ def infer_type_vars_for_execution(function, arguments, annotation_dict): elif kind is Parameter.VAR_KEYWORD: # TODO _dict_values is not public. actual_value_set = actual_value_set.try_merge('_dict_values') - for ann in annotation_value_set: - merge_type_var_dicts( - annotation_variable_results, - ann.infer_type_vars(actual_value_set), - ) + merge_type_var_dicts( + annotation_variable_results, + annotation_value_set.infer_type_vars(actual_value_set), + ) return annotation_variable_results @@ -301,11 +300,10 @@ def infer_type_vars_for_callable(arguments, lazy_params): callable_param_values = lazy_callable_param.infer() # Infer unknown type var actual_value_set = lazy_value.infer() - for v in callable_param_values: - merge_type_var_dicts( - annotation_variable_results, - v.infer_type_vars(actual_value_set), - ) + merge_type_var_dicts( + annotation_variable_results, + callable_param_values.infer_type_vars(actual_value_set), + ) return annotation_variable_results @@ -362,16 +360,15 @@ def merge_pairwise_generics(annotation_value, annotated_argument_class): actual_generics = annotated_argument_class.get_generics() for annotation_generics_set, actual_generic_set in zip(annotation_generics, actual_generics): - for nested_annotation_value in annotation_generics_set: - merge_type_var_dicts( - type_var_dict, - nested_annotation_value.infer_type_vars( - actual_generic_set, - # This is a note to ourselves that we have already - # converted the instance representation to its class. - is_class_value=True, - ), - ) + merge_type_var_dicts( + type_var_dict, + annotation_generics_set.infer_type_vars( + actual_generic_set, + # This is a note to ourselves that we have already + # converted the instance representation to its class. + is_class_value=True, + ), + ) return type_var_dict diff --git a/jedi/inference/gradual/base.py b/jedi/inference/gradual/base.py index e8678587..6a3081d4 100644 --- a/jedi/inference/gradual/base.py +++ b/jedi/inference/gradual/base.py @@ -209,13 +209,12 @@ class GenericClass(ClassMixin, DefineGenericBase): if annotation_name == 'Iterable' and not is_class_value: given = self.get_generics() if given: - for nested_annotation_value in given[0]: - merge_type_var_dicts( - type_var_dict, - nested_annotation_value.infer_type_vars( - value_set.merge_types_of_iterate(), - ), - ) + merge_type_var_dicts( + type_var_dict, + given[0].infer_type_vars( + value_set.merge_types_of_iterate(), + ), + ) else: # Note: we need to handle the MRO _in order_, so we need to extract # the elements from the set first, then handle them, even if we put diff --git a/jedi/inference/gradual/typing.py b/jedi/inference/gradual/typing.py index 5e8ae3db..08695f2a 100644 --- a/jedi/inference/gradual/typing.py +++ b/jedi/inference/gradual/typing.py @@ -200,25 +200,23 @@ class TypingClassValueWithIndex(_TypingClassMixin, TypingValueWithIndex): ) else: - for nested_annotation_value in given[0]: - merge_type_var_dicts( - type_var_dict, - nested_annotation_value.infer_type_vars( - value_set, - is_class_value=True, - ), - ) + merge_type_var_dicts( + type_var_dict, + given[0].infer_type_vars( + value_set, + is_class_value=True, + ), + ) elif annotation_name == 'Callable': given = self.get_generics() if len(given) == 2: - for nested_annotation_value in given[1]: - merge_type_var_dicts( - type_var_dict, - nested_annotation_value.infer_type_vars( - value_set.execute_annotation(), - ), - ) + merge_type_var_dicts( + type_var_dict, + given[1].infer_type_vars( + value_set.execute_annotation(), + ), + ) elif annotation_name == 'Tuple': annotation_generics = self.get_generics() @@ -228,13 +226,12 @@ class TypingClassValueWithIndex(_TypingClassMixin, TypingValueWithIndex): # The parameter annotation is of the form `Tuple[T, ...]`, # so we treat the incoming tuple like a iterable sequence # rather than a positional container of elements. - for nested_annotation_value in annotation_generics[0]: - merge_type_var_dicts( - type_var_dict, - nested_annotation_value.infer_type_vars( - value_set.merge_types_of_iterate(), - ), - ) + merge_type_var_dicts( + type_var_dict, + annotation_generics[0].infer_type_vars( + value_set.merge_types_of_iterate(), + ), + ) else: # The parameter annotation has only explicit type parameters