From 96132587b75137263ef4ffd000f328e01c948c25 Mon Sep 17 00:00:00 2001 From: Peter Law Date: Sat, 7 Mar 2020 17:35:29 +0000 Subject: [PATCH] Clarify generic tuple inference This hoist a loop invariant conditional check outside the loop making it clearer and one branch more obviously similar to the general type handling. --- jedi/inference/gradual/annotation.py | 56 ++++++++++++++-------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/jedi/inference/gradual/annotation.py b/jedi/inference/gradual/annotation.py index 54e0b00f..699a14f1 100644 --- a/jedi/inference/gradual/annotation.py +++ b/jedi/inference/gradual/annotation.py @@ -445,37 +445,37 @@ def _infer_type_vars(annotation_value, value_set, is_class_value=False): ) ) elif annotation_name == 'Tuple': - # TODO: this logic is pretty similar to the general logic below, can - # we combine them? + annotation_generics = annotation_value.get_generics() + tuple_annotation, = annotation_value.execute_annotation() + # TODO: is can we avoid using this private method? + if tuple_annotation._is_homogenous(): + # 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, + _infer_type_vars( + nested_annotation_value, + value_set.merge_types_of_iterate(), + ), + ) + else: + # The parameter annotation has only explicit type parameters + # (e.g: `Tuple[T]`, `Tuple[T, U]`, `Tuple[T, U, V]`, etc.) so we + # treat the incoming values as needing to match the annotation + # exactly, just as we would for non-tuple annotations. - for element in value_set: - py_class = element.get_annotated_class_object() - if not isinstance(py_class, GenericClass): - py_class = element + # TODO: this logic is pretty similar to the general logic below, can + # we combine them? - if not isinstance(py_class, DefineGenericBase): - continue + for element in value_set: + py_class = element.get_annotated_class_object() + if not isinstance(py_class, GenericClass): + py_class = element - annotation_generics = annotation_value.get_generics() - tuple_annotation, = annotation_value.execute_annotation() - # TODO: is can we avoid using this private method? - if tuple_annotation._is_homogenous(): - # 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, - _infer_type_vars( - nested_annotation_value, - value_set.merge_types_of_iterate(), - ), - ) - else: - # The parameter annotation has only explicit type parameters - # (e.g: `Tuple[T]`, `Tuple[T, U]`, `Tuple[T, U, V]`, etc.) so we - # treat the incoming values as needing to match the annotation - # exactly, just as we would for non-tuple annotations. + if not isinstance(py_class, DefineGenericBase): + continue actual_generics = py_class.get_generics() merge_pairwise_generics(annotation_generics, actual_generics)