Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions sdks/python/apache_beam/typehints/opcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,19 @@ def binary_true_divide(state, unused_arg):


def binary_subscr(state, unused_arg):
tos = state.stack.pop()
if tos in (str, six.text_type):
out = tos
index = state.stack.pop()
base = state.stack.pop()
if base in (str, six.text_type):
out = base
elif (isinstance(index, Const) and isinstance(index.value, int)
and isinstance(base, typehints.TupleHint.TupleConstraint)):
const_index = index.value
if -len(base.tuple_types) < const_index < len(base.tuple_types):
out = base.tuple_types[const_index]
else:
out = element_type(base)
else:
out = element_type(tos)
out = element_type(base)
state.stack.append(out)


Expand Down Expand Up @@ -193,8 +201,9 @@ def store_subscr(unused_state, unused_args):
# break_loop
# continue_loop
def list_append(state, arg):
new_element_type = Const.unwrap(state.stack.pop())
state.stack[-arg] = List[Union[element_type(state.stack[-arg]),
Const.unwrap(state.stack.pop())]]
new_element_type]]


load_locals = push_value(Dict[str, Any])
Expand Down
20 changes: 20 additions & 0 deletions sdks/python/apache_beam/typehints/trivial_inference_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ def testTuples(self):
self.assertReturnType(
typehints.Tuple[str, int, float], lambda x: (x, 0, 1.0), [str])

def testGetItem(self):
def reverse(ab):
return ab[-1], ab[0]
self.assertReturnType(
typehints.Tuple[typehints.Any, typehints.Any], reverse, [typehints.Any])
self.assertReturnType(
typehints.Tuple[int, float], reverse, [typehints.Tuple[float, int]])
self.assertReturnType(
typehints.Tuple[int, str], reverse, [typehints.Tuple[str, float, int]])
self.assertReturnType(
typehints.Tuple[int, int], reverse, [typehints.List[int]])

def testUnpack(self):
def reverse(a_b):
(a, b) = a_b
Expand Down Expand Up @@ -98,6 +110,14 @@ def testTupleListComprehension(self):
typehints.List[typehints.Union[int, float]],
lambda xs: [x for x in xs],
[typehints.Tuple[int, float]])
self.assertReturnType(
typehints.List[typehints.Tuple[str, int]],
lambda kvs: [(kvs[0], v) for v in kvs[1]],
[typehints.Tuple[str, typehints.Iterable[int]]])
self.assertReturnType(
typehints.List[typehints.Tuple[str, typehints.Union[str, int], int]],
lambda L: [(a, a or b, b) for a, b in L],
[typehints.Iterable[typehints.Tuple[str, int]]])

def testGenerator(self):

Expand Down