diff --git a/sdks/python/apache_beam/typehints/typehints.py b/sdks/python/apache_beam/typehints/typehints.py index 487fb78d26bb..5cbb41e4d664 100644 --- a/sdks/python/apache_beam/typehints/typehints.py +++ b/sdks/python/apache_beam/typehints/typehints.py @@ -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 diff --git a/sdks/python/apache_beam/typehints/typehints_test.py b/sdks/python/apache_beam/typehints/typehints_test.py index 532da8917f8c..7e2c390de320 100644 --- a/sdks/python/apache_beam/typehints/typehints_test.py +++ b/sdks/python/apache_beam/typehints/typehints_test.py @@ -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): @@ -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): @@ -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):