Skip to content

Commit 93aa342

Browse files
authored
Update routing of types into convert_to_beam_type in normalize() (#34103)
* Update routing of types into convert_to_beam_type in normalize() * fix typo * add some quick test cases
1 parent 10a9f22 commit 93aa342

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

sdks/python/apache_beam/typehints/typehints.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,14 +1309,14 @@ def normalize(x, none_as_type=False):
13091309
# Avoid circular imports
13101310
from apache_beam.typehints import native_type_compatibility
13111311

1312-
if isinstance(x, types.GenericAlias):
1313-
x = native_type_compatibility.convert_builtin_to_typing(x)
1314-
13151312
if none_as_type and x is None:
13161313
return type(None)
1314+
# Convert bare builtin types to correct type hints directly
13171315
elif x in _KNOWN_PRIMITIVE_TYPES:
13181316
return _KNOWN_PRIMITIVE_TYPES[x]
1319-
elif getattr(x, '__module__', None) == 'typing':
1317+
elif getattr(x, '__module__',
1318+
None) in ('typing', 'collections.abc') or getattr(
1319+
x, '__origin__', None) in _KNOWN_PRIMITIVE_TYPES:
13201320
beam_type = native_type_compatibility.convert_to_beam_type(x)
13211321
if beam_type != x:
13221322
# We were able to do the conversion.

sdks/python/apache_beam/typehints/typehints_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,12 @@ def test_getitem_invalid_composite_type_param(self):
898898
'an instance of int.',
899899
e.exception.args[0])
900900

901+
def test_normalize(self):
902+
expected_beam_type = typehints.Sequence[int]
903+
converted_beam_type = typehints.normalize(
904+
collections.abc.Sequence[int], False)
905+
self.assertEqual(converted_beam_type, expected_beam_type)
906+
901907

902908
class IterableHintTestCase(TypeHintTestCase):
903909
def test_getitem_invalid_composite_type_param(self):
@@ -983,6 +989,12 @@ def test_type_check_violation_valid_composite_type(self):
983989
l = ([[1, 2], [3, 4, 5]])
984990
self.assertIsNone(hint.type_check(l))
985991

992+
def test_normalize(self):
993+
expected_beam_type = typehints.Iterable[str]
994+
converted_beam_type = typehints.normalize(
995+
collections.abc.Iterable[str], False)
996+
self.assertEqual(converted_beam_type, expected_beam_type)
997+
986998

987999
class TestGeneratorWrapper(TypeHintTestCase):
9881000
def test_functions_as_regular_generator(self):

0 commit comments

Comments
 (0)