mirror of
https://github.com/davidhalter/jedi.git
synced 2026-05-18 14:29:40 +08:00
Ensure variadic tuples (Tuple[T, ...]) behave like sequences
This commit is contained in:
@@ -385,8 +385,6 @@ def _infer_type_vars(annotation_value, value_set, is_class_value=False):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
elif annotation_name == 'Tuple':
|
elif annotation_name == 'Tuple':
|
||||||
# TODO: check that this works both for fixed and variadic tuples
|
|
||||||
# (and maybe for combiantions of those).
|
|
||||||
# TODO: this logic is pretty similar to the general logic below, can
|
# TODO: this logic is pretty similar to the general logic below, can
|
||||||
# we combine them?
|
# we combine them?
|
||||||
|
|
||||||
@@ -399,20 +397,32 @@ def _infer_type_vars(annotation_value, value_set, is_class_value=False):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
annotation_generics = annotation_value.get_generics()
|
annotation_generics = annotation_value.get_generics()
|
||||||
actual_generics = py_class.get_generics()
|
tuple_annotation, = annotation_value.execute_annotation()
|
||||||
for annotation_generics_set, actual_generic_set in zip(annotation_generics, actual_generics):
|
# TODO: is can we avoid using this private method?
|
||||||
for nested_annotation_value in annotation_generics_set:
|
if tuple_annotation._is_homogenous():
|
||||||
|
for nested_annotation_value in annotation_generics[0]:
|
||||||
_merge_type_var_dicts(
|
_merge_type_var_dicts(
|
||||||
type_var_dict,
|
type_var_dict,
|
||||||
_infer_type_vars(
|
_infer_type_vars(
|
||||||
nested_annotation_value,
|
nested_annotation_value,
|
||||||
actual_generic_set,
|
value_set.merge_types_of_iterate(),
|
||||||
# This is a note to ourselves that we
|
|
||||||
# have already converted the instance
|
|
||||||
# representation to its class.
|
|
||||||
is_class_value=True,
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
actual_generics = py_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,
|
||||||
|
_infer_type_vars(
|
||||||
|
nested_annotation_value,
|
||||||
|
actual_generic_set,
|
||||||
|
# This is a note to ourselves that we
|
||||||
|
# have already converted the instance
|
||||||
|
# representation to its class.
|
||||||
|
is_class_value=True,
|
||||||
|
),
|
||||||
|
)
|
||||||
elif isinstance(annotation_value, GenericClass):
|
elif isinstance(annotation_value, GenericClass):
|
||||||
if annotation_name == 'Iterable' and not is_class_value:
|
if annotation_name == 'Iterable' and not is_class_value:
|
||||||
given = annotation_value.get_generics()
|
given = annotation_value.get_generics()
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
# python >= 3.4
|
# python >= 3.4
|
||||||
from typing import Any, Iterable, List, Tuple, TypeVar
|
from typing import Any, Iterable, List, Sequence, Tuple, TypeVar, Union
|
||||||
|
|
||||||
T = TypeVar('T')
|
T = TypeVar('T')
|
||||||
U = TypeVar('U')
|
U = TypeVar('U')
|
||||||
@@ -14,6 +14,9 @@ typed_tuple_str = ('abc',) # type: Tuple[str]
|
|||||||
untyped_tuple_str_int = ('abc', 4)
|
untyped_tuple_str_int = ('abc', 4)
|
||||||
typed_tuple_str_int = ('abc', 4) # type: Tuple[str, int]
|
typed_tuple_str_int = ('abc', 4) # type: Tuple[str, int]
|
||||||
|
|
||||||
|
variadic_tuple_str = ('abc',) # type: Tuple[str, ...]
|
||||||
|
variadic_tuple_str_int = ('abc', 4) # type: Tuple[Union[str, int], ...]
|
||||||
|
|
||||||
|
|
||||||
def untyped_passthrough(x):
|
def untyped_passthrough(x):
|
||||||
return x
|
return x
|
||||||
@@ -27,6 +30,9 @@ def typed_tuple_generic_passthrough(x: Tuple[T]) -> Tuple[T]:
|
|||||||
def typed_multi_typed_tuple_generic_passthrough(x: Tuple[T, U]) -> Tuple[U, T]:
|
def typed_multi_typed_tuple_generic_passthrough(x: Tuple[T, U]) -> Tuple[U, T]:
|
||||||
return x[1], x[0]
|
return x[1], x[0]
|
||||||
|
|
||||||
|
def typed_variadic_tuple_generic_passthrough(x: Tuple[T, ...]) -> Sequence[T]:
|
||||||
|
return x
|
||||||
|
|
||||||
def typed_iterable_generic_passthrough(x: Iterable[T]) -> Iterable[T]:
|
def typed_iterable_generic_passthrough(x: Iterable[T]) -> Iterable[T]:
|
||||||
return x
|
return x
|
||||||
|
|
||||||
@@ -87,6 +93,23 @@ out_typed[0]
|
|||||||
out_typed[1]
|
out_typed[1]
|
||||||
|
|
||||||
|
|
||||||
|
for j in typed_variadic_tuple_generic_passthrough(untyped_tuple_str_int):
|
||||||
|
#? str() int()
|
||||||
|
j
|
||||||
|
|
||||||
|
for k in typed_variadic_tuple_generic_passthrough(typed_tuple_str_int):
|
||||||
|
#? str() int()
|
||||||
|
k
|
||||||
|
|
||||||
|
for l in typed_variadic_tuple_generic_passthrough(variadic_tuple_str):
|
||||||
|
#? str()
|
||||||
|
l
|
||||||
|
|
||||||
|
for m in typed_variadic_tuple_generic_passthrough(variadic_tuple_str_int):
|
||||||
|
#? str() int()
|
||||||
|
m
|
||||||
|
|
||||||
|
|
||||||
for n in typed_fully_generic_passthrough(untyped_list_str):
|
for n in typed_fully_generic_passthrough(untyped_list_str):
|
||||||
#? str()
|
#? str()
|
||||||
n
|
n
|
||||||
|
|||||||
Reference in New Issue
Block a user