Push much looping and merging of infering type vars into ValueSet

This commit is contained in:
Peter Law
2020-03-18 21:44:18 +00:00
parent 3c7621049c
commit f68d65ed59
4 changed files with 54 additions and 49 deletions

View File

@@ -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([])

View File

@@ -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

View File

@@ -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

View File

@@ -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