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,10 +268,9 @@ 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),
annotation_value_set.infer_type_vars(actual_value_set),
)
return annotation_variable_results
@@ -301,10 +300,9 @@ 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),
callable_param_values.infer_type_vars(actual_value_set),
)
return annotation_variable_results
@@ -362,10 +360,9 @@ 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(
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.

View File

@@ -209,10 +209,9 @@ 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(
given[0].infer_type_vars(
value_set.merge_types_of_iterate(),
),
)

View File

@@ -200,10 +200,9 @@ 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(
given[0].infer_type_vars(
value_set,
is_class_value=True,
),
@@ -212,10 +211,9 @@ class TypingClassValueWithIndex(_TypingClassMixin, TypingValueWithIndex):
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(
given[1].infer_type_vars(
value_set.execute_annotation(),
),
)
@@ -228,10 +226,9 @@ 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(
annotation_generics[0].infer_type_vars(
value_set.merge_types_of_iterate(),
),
)