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
7 changes: 7 additions & 0 deletions sdks/python/apache_beam/typehints/typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,13 @@ def normalize(x, none_as_type=False):
return type(None)
elif x in _KNOWN_PRIMITIVE_TYPES:
return _KNOWN_PRIMITIVE_TYPES[x]
elif sys.version_info >= (3, 9) and isinstance(x, types.GenericAlias):
# TODO(https://github.com/apache/beam/issues/23366): handle PEP 585
# generic type hints properly
raise TypeError(
'PEP 585 generic type hints like %s are not yet supported, '
'use typing module containers instead. See equivalents listed '
'at https://docs.python.org/3/library/typing.html' % x)
elif getattr(x, '__module__', None) == 'typing':
# Avoid circular imports
from apache_beam.typehints import native_type_compatibility
Expand Down
33 changes: 33 additions & 0 deletions sdks/python/apache_beam/typehints/typehints_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,17 @@ def test_type_check_invalid_composite_type_arbitrary_length(self):
"was received.",
e.exception.args[0])

def test_normalize_with_builtin_tuple(self):
if sys.version_info >= (3, 9):
with self.assertRaises(TypeError) as e:
typehints.normalize(tuple[int, int], False)

self.assertEqual(
'PEP 585 generic type hints like tuple[int, int] are not yet '
'supported, use typing module containers instead. See equivalents '
'listed at https://docs.python.org/3/library/typing.html',
e.exception.args[0])


class ListHintTestCase(TypeHintTestCase):
def test_getitem_invalid_composite_type_param(self):
Expand Down Expand Up @@ -582,6 +593,17 @@ def test_enforce_list_type_constraint_invalid_composite_type(self):
'instead received an instance of type str.',
e.exception.args[0])

def test_normalize_with_builtin_list(self):
if sys.version_info >= (3, 9):
with self.assertRaises(TypeError) as e:
typehints.normalize(list[int], False)

self.assertEqual(
'PEP 585 generic type hints like list[int] are not yet supported, '
'use typing module containers instead. See equivalents listed '
'at https://docs.python.org/3/library/typing.html',
e.exception.args[0])


class KVHintTestCase(TypeHintTestCase):
def test_getitem_param_must_be_tuple(self):
Expand Down Expand Up @@ -717,6 +739,17 @@ def test_match_type_variables(self):
},
hint.match_type_variables(typehints.Dict[int, str]))

def test_normalize_with_builtin_dict(self):
if sys.version_info >= (3, 9):
with self.assertRaises(TypeError) as e:
typehints.normalize(dict[int, str], False)

self.assertEqual(
'PEP 585 generic type hints like dict[int, str] are not yet '
'supported, use typing module containers instead. See equivalents '
'listed at https://docs.python.org/3/library/typing.html',
e.exception.args[0])


class BaseSetHintTest:
class CommonTests(TypeHintTestCase):
Expand Down