Skip to content

Commit

Permalink
Establish list to collection compatibility (#29417)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmccluskey authored Nov 13, 2023
1 parent 5e7c27c commit 618d7a8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 8 additions & 4 deletions sdks/python/apache_beam/typehints/typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,13 +1038,13 @@ def __repr__(self):
@staticmethod
def _is_subclass_constraint(sub):
return isinstance(
sub, (
sub,
(
CollectionTypeConstraint,
FrozenSetTypeConstraint,
SetTypeConstraint))
SetTypeConstraint,
ListConstraint))

# TODO(https://github.com/apache/beam/issues/29135): allow for consistency
# with Mapping types
def _consistent_with_check_(self, sub):
if self._is_subclass_constraint(sub):
return is_consistent_with(sub.inner_type, self.inner_type)
Expand All @@ -1058,6 +1058,10 @@ def _consistent_with_check_(self, sub):
return all(
is_consistent_with(elem, self.inner_type)
for elem in sub.tuple_types)
# TODO(https://github.com/apache/beam/issues/29135): allow for
# consistency checks with Mapping types
elif isinstance(sub, DictConstraint):
return True
elif not isinstance(sub, TypeConstraint):
if getattr(sub, '__origin__', None) is not None and getattr(
sub, '__args__', None) is not None:
Expand Down
2 changes: 2 additions & 0 deletions sdks/python/apache_beam/typehints/typehints_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,13 +874,15 @@ def test_type_constraint_compatibility(self):
typehints.Collection[typehints.Any], typehints.Collection[int])
self.assertCompatible(typehints.Collection[int], typehints.Tuple[int])
self.assertCompatible(typehints.Any, typehints.Collection[str])
self.assertCompatible(typehints.Collection[str], typehints.List[str])

def test_one_way_compatibility(self):
self.assertNotCompatible(typehints.Set[int], typehints.Collection[int])
self.assertNotCompatible(
typehints.FrozenSet[int], typehints.Collection[int])
self.assertNotCompatible(typehints.Tuple[int], typehints.Collection[int])
self.assertNotCompatible(typehints.Collection[int], typehints.Iterable[int])
self.assertNotCompatible(typehints.List[str], typehints.Collection[str])

def test_getitem_invalid_composite_type_param(self):
with self.assertRaises(TypeError) as e:
Expand Down

0 comments on commit 618d7a8

Please sign in to comment.