Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a847f06

Browse files
authored
Restore previous handling of Callables, fix bare collections types (apache#34645)
* Restore previous handling of Callables * fix bare type handling * add parameterized tests
1 parent 56f1aa1 commit a847f06

3 files changed

Lines changed: 75 additions & 1 deletion

File tree

sdks/python/apache_beam/typehints/native_type_compatibility.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ def convert_to_beam_type(typ):
364364
# Only translate primitives and types from collections.abc and typing.
365365
return typ
366366
if (typ_module == 'collections.abc' and
367-
typ.__origin__ not in _CONVERTED_COLLECTIONS):
367+
getattr(typ, '__origin__', typ) not in _CONVERTED_COLLECTIONS):
368368
# TODO(https://github.com/apache/beam/issues/29135):
369369
# Support more collections types
370370
return typ

sdks/python/apache_beam/typehints/native_type_compatibility_test.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,16 @@ def test_convert_to_beam_type_with_collections_types(self):
263263
collections.defaultdict[str, int],
264264
typehints.Dict[str, int]),
265265
('count', collections.Counter[str, int], typehints.Dict[str, int]),
266+
(
267+
'bare callable',
268+
collections.abc.Callable,
269+
collections.abc.Callable,
270+
),
271+
(
272+
'parameterized callable',
273+
collections.abc.Callable[[str], int],
274+
collections.abc.Callable[[str], int],
275+
),
266276
]
267277

268278
for test_case in test_cases:
@@ -369,6 +379,50 @@ def test_convert_bare_types(self):
369379
converted_beam_type = convert_to_beam_type(typing_type)
370380
self.assertEqual(expected_beam_type, converted_beam_type, description)
371381

382+
def test_convert_bare_collections_types(self):
383+
# Conversions for unsubscripted types that have implicit subscripts.
384+
test_cases = [
385+
('bare list', list, typehints.List[typehints.TypeVariable('T')]),
386+
(
387+
'bare dict',
388+
dict,
389+
typehints.Dict[typehints.TypeVariable('KT'),
390+
typehints.TypeVariable('VT')]),
391+
(
392+
'bare tuple',
393+
tuple,
394+
typehints.Tuple[typehints.TypeVariable('T'), ...]),
395+
('bare set', typing.Set, typehints.Set[typehints.TypeVariable('T')]),
396+
(
397+
'bare frozenset',
398+
frozenset,
399+
typehints.FrozenSet[typehints.TypeVariable(
400+
'T', use_name_in_eq=False)]),
401+
(
402+
'bare iterator',
403+
collections.abc.Iterator,
404+
typehints.Iterator[typehints.TypeVariable('T_co')]),
405+
(
406+
'bare iterable',
407+
collections.abc.Iterable,
408+
typehints.Iterable[typehints.TypeVariable('T_co')]),
409+
(
410+
'nested bare',
411+
tuple[collections.abc.Iterator],
412+
typehints.Tuple[typehints.Iterator[typehints.TypeVariable('T_co')]]
413+
),
414+
(
415+
'bare generator',
416+
collections.abc.Generator,
417+
typehints.Generator[typehints.TypeVariable('T_co')]),
418+
]
419+
for test_case in test_cases:
420+
description = test_case[0]
421+
typing_type = test_case[1]
422+
expected_beam_type = test_case[2]
423+
converted_beam_type = convert_to_beam_type(typing_type)
424+
self.assertEqual(expected_beam_type, converted_beam_type, description)
425+
372426
def test_convert_bare_types_fail(self):
373427
# These conversions should fail.
374428
test_cases = [

sdks/python/apache_beam/typehints/typehints_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,26 @@ def test_mix_fails(self):
12501250
self.assertNotCompatible(re.Pattern[str], re.Match[str])
12511251

12521252

1253+
# TODO(https://github.com/apache/beam/issues/34644): implement a
1254+
# CallableTypeHint class and implement test suites for inner type checking
1255+
class CallableTestCase(TypeHintTestCase):
1256+
def test_callable(self):
1257+
self.assertCompatible(collections.abc.Callable, collections.abc.Callable)
1258+
self.assertCompatible(
1259+
collections.abc.Callable[[str], int],
1260+
collections.abc.Callable[[str], int])
1261+
1262+
def test_normalize_to_any_bare(self):
1263+
input = collections.abc.Callable
1264+
output = typehints.normalize(input)
1265+
self.assertEqual(Any, output)
1266+
1267+
def test_normalize_to_any_parameterized(self):
1268+
input = collections.abc.Callable[[str], int]
1269+
output = typehints.normalize(input)
1270+
self.assertEqual(Any, output)
1271+
1272+
12531273
class TakesDecoratorTestCase(TypeHintTestCase):
12541274
def test_must_be_primitive_type_or_constraint(self):
12551275
with self.assertRaises(TypeError) as e:

0 commit comments

Comments
 (0)