From 5e990d9206c71aef1b65f197a2e4b4087e299199 Mon Sep 17 00:00:00 2001 From: Peter Law Date: Sun, 23 Feb 2020 00:27:30 +0000 Subject: [PATCH] Support passing through values for non-annotated tuples --- jedi/inference/value/iterable.py | 5 +++++ .../pep0484_generic_passthroughs.py | 21 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/jedi/inference/value/iterable.py b/jedi/inference/value/iterable.py index 236ea851..d1545b63 100644 --- a/jedi/inference/value/iterable.py +++ b/jedi/inference/value/iterable.py @@ -329,6 +329,11 @@ class SequenceLiteralValue(Sequence): self.array_type = SequenceLiteralValue.mapping[atom.children[0]] """The builtin name of the array (list, set, tuple or dict).""" + def _get_generics(self): + if self.array_type == u'tuple': + return tuple(x.infer().py__class__() for x in self.py__iter__()) + return super(SequenceLiteralValue, self)._get_generics() + def py__simple_getitem__(self, index): """Here the index is an int/str. Raises IndexError/KeyError.""" if isinstance(index, slice): diff --git a/test/completion/pep0484_generic_passthroughs.py b/test/completion/pep0484_generic_passthroughs.py index bd13f926..c8a7db7e 100644 --- a/test/completion/pep0484_generic_passthroughs.py +++ b/test/completion/pep0484_generic_passthroughs.py @@ -2,6 +2,7 @@ from typing import Any, Iterable, List, Tuple, TypeVar T = TypeVar('T') +U = TypeVar('U') TList = TypeVar('TList', bound=List[Any]) untyped_list_str = ['abc', 'def'] @@ -10,6 +11,9 @@ typed_list_str = ['abc', 'def'] # type: List[str] untyped_tuple_str = ('abc',) typed_tuple_str = ('abc',) # type: Tuple[str] +untyped_tuple_str_int = ('abc', 4) +typed_tuple_str_int = ('abc', 4) # type: Tuple[str, int] + def untyped_passthrough(x): return x @@ -20,6 +24,9 @@ def typed_list_generic_passthrough(x: List[T]) -> List[T]: def typed_tuple_generic_passthrough(x: Tuple[T]) -> Tuple[T]: return x +def typed_multi_typed_tuple_generic_passthrough(x: Tuple[T, U]) -> Tuple[U, T]: + return x[1], x[0] + def typed_iterable_generic_passthrough(x: Iterable[T]) -> Iterable[T]: return x @@ -66,6 +73,20 @@ for h in typed_tuple_generic_passthrough(typed_tuple_str): h +out_untyped = typed_multi_typed_tuple_generic_passthrough(untyped_tuple_str_int) +#? int() +out_untyped[0] +#? str() +out_untyped[1] + + +out_typed = typed_multi_typed_tuple_generic_passthrough(typed_tuple_str_int) +#? int() +out_typed[0] +#? str() +out_typed[1] + + for n in typed_fully_generic_passthrough(untyped_list_str): #? str() n