Support passing through values for non-annotated tuples

This commit is contained in:
Peter Law
2020-02-23 00:27:30 +00:00
parent 80db4dcf56
commit 5e990d9206
2 changed files with 26 additions and 0 deletions

View File

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

View File

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