From 84a3877e58f6a7bb73aa9ded34daa248ec7cb94a Mon Sep 17 00:00:00 2001 From: Jack McCluskey Date: Thu, 12 Jan 2023 16:06:16 +0000 Subject: [PATCH 01/10] Add explicit warning for PEP 585 type hints --- sdks/python/apache_beam/typehints/native_type_compatibility.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sdks/python/apache_beam/typehints/native_type_compatibility.py b/sdks/python/apache_beam/typehints/native_type_compatibility.py index 153b9d4b4588..650afeba1558 100644 --- a/sdks/python/apache_beam/typehints/native_type_compatibility.py +++ b/sdks/python/apache_beam/typehints/native_type_compatibility.py @@ -209,6 +209,8 @@ def convert_to_beam_type(typ): return typehints.Any elif getattr(typ, '__module__', None) != 'typing': # Only translate types from the typing module. + # TODO(https://github.com/apache/beam/issues/23366): Update logic to properly handle built-in generic type hints + _LOGGER.warning('PEP 585 typehints are not currently supported, use typing module containers instead.') return typ type_map = [ From 2cb848b1d1abbab89e09f169344eb0efadc5bd4d Mon Sep 17 00:00:00 2001 From: Jack McCluskey Date: Thu, 12 Jan 2023 16:15:16 +0000 Subject: [PATCH 02/10] formatting --- .../python/apache_beam/typehints/native_type_compatibility.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sdks/python/apache_beam/typehints/native_type_compatibility.py b/sdks/python/apache_beam/typehints/native_type_compatibility.py index 650afeba1558..036c26f37216 100644 --- a/sdks/python/apache_beam/typehints/native_type_compatibility.py +++ b/sdks/python/apache_beam/typehints/native_type_compatibility.py @@ -210,7 +210,9 @@ def convert_to_beam_type(typ): elif getattr(typ, '__module__', None) != 'typing': # Only translate types from the typing module. # TODO(https://github.com/apache/beam/issues/23366): Update logic to properly handle built-in generic type hints - _LOGGER.warning('PEP 585 typehints are not currently supported, use typing module containers instead.') + _LOGGER.warning( + 'PEP 585 typehints are not currently supported, use typing module containers instead.' + ) return typ type_map = [ From 63e4352973497f8b1d112920c189195e0fe9da91 Mon Sep 17 00:00:00 2001 From: Jack McCluskey Date: Thu, 12 Jan 2023 16:27:33 +0000 Subject: [PATCH 03/10] linting --- .../apache_beam/typehints/native_type_compatibility.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sdks/python/apache_beam/typehints/native_type_compatibility.py b/sdks/python/apache_beam/typehints/native_type_compatibility.py index 036c26f37216..e754d77d2367 100644 --- a/sdks/python/apache_beam/typehints/native_type_compatibility.py +++ b/sdks/python/apache_beam/typehints/native_type_compatibility.py @@ -209,10 +209,11 @@ def convert_to_beam_type(typ): return typehints.Any elif getattr(typ, '__module__', None) != 'typing': # Only translate types from the typing module. - # TODO(https://github.com/apache/beam/issues/23366): Update logic to properly handle built-in generic type hints + # TODO(https://github.com/apache/beam/issues/23366): Update logic to + # properly handle built-in generic type hints _LOGGER.warning( - 'PEP 585 typehints are not currently supported, use typing module containers instead.' - ) + 'PEP 585 typehints are not currently supported, use typing module ' + + 'containers instead.') return typ type_map = [ From ff55b5e37a31f72617b0b76cb0cdf3cb6f94a1d9 Mon Sep 17 00:00:00 2001 From: Jack McCluskey Date: Thu, 12 Jan 2023 16:40:46 +0000 Subject: [PATCH 04/10] make logging statement more explicit --- sdks/python/apache_beam/typehints/native_type_compatibility.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdks/python/apache_beam/typehints/native_type_compatibility.py b/sdks/python/apache_beam/typehints/native_type_compatibility.py index e754d77d2367..fc426ff7e154 100644 --- a/sdks/python/apache_beam/typehints/native_type_compatibility.py +++ b/sdks/python/apache_beam/typehints/native_type_compatibility.py @@ -213,7 +213,7 @@ def convert_to_beam_type(typ): # properly handle built-in generic type hints _LOGGER.warning( 'PEP 585 typehints are not currently supported, use typing module ' + - 'containers instead.') + 'containers for %s instead.', typ) return typ type_map = [ From 3f1f577cc100e17b004aed3a5b02b8fa817e784d Mon Sep 17 00:00:00 2001 From: Jack McCluskey Date: Thu, 12 Jan 2023 16:45:11 +0000 Subject: [PATCH 05/10] more formatting --- sdks/python/apache_beam/typehints/native_type_compatibility.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sdks/python/apache_beam/typehints/native_type_compatibility.py b/sdks/python/apache_beam/typehints/native_type_compatibility.py index fc426ff7e154..338838253c8d 100644 --- a/sdks/python/apache_beam/typehints/native_type_compatibility.py +++ b/sdks/python/apache_beam/typehints/native_type_compatibility.py @@ -213,7 +213,8 @@ def convert_to_beam_type(typ): # properly handle built-in generic type hints _LOGGER.warning( 'PEP 585 typehints are not currently supported, use typing module ' + - 'containers for %s instead.', typ) + 'containers for %s instead.', + typ) return typ type_map = [ From 1b9ceac76ab1357fbba747ddd3c203202246c269 Mon Sep 17 00:00:00 2001 From: Jack McCluskey Date: Thu, 12 Jan 2023 18:10:40 +0000 Subject: [PATCH 06/10] move message to normalize(), throw TypeError --- .../typehints/native_type_compatibility.py | 6 ---- .../python/apache_beam/typehints/typehints.py | 5 ++++ .../apache_beam/typehints/typehints_test.py | 30 +++++++++++++++++++ 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/sdks/python/apache_beam/typehints/native_type_compatibility.py b/sdks/python/apache_beam/typehints/native_type_compatibility.py index 338838253c8d..153b9d4b4588 100644 --- a/sdks/python/apache_beam/typehints/native_type_compatibility.py +++ b/sdks/python/apache_beam/typehints/native_type_compatibility.py @@ -209,12 +209,6 @@ def convert_to_beam_type(typ): return typehints.Any elif getattr(typ, '__module__', None) != 'typing': # Only translate types from the typing module. - # TODO(https://github.com/apache/beam/issues/23366): Update logic to - # properly handle built-in generic type hints - _LOGGER.warning( - 'PEP 585 typehints are not currently supported, use typing module ' + - 'containers for %s instead.', - typ) return typ type_map = [ diff --git a/sdks/python/apache_beam/typehints/typehints.py b/sdks/python/apache_beam/typehints/typehints.py index 487fb78d26bb..f5da692f7e72 100644 --- a/sdks/python/apache_beam/typehints/typehints.py +++ b/sdks/python/apache_beam/typehints/typehints.py @@ -1190,6 +1190,11 @@ def normalize(x, none_as_type=False): return type(None) elif x in _KNOWN_PRIMITIVE_TYPES: return _KNOWN_PRIMITIVE_TYPES[x] + elif 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.' % 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..112beca4d6ad 100644 --- a/sdks/python/apache_beam/typehints/typehints_test.py +++ b/sdks/python/apache_beam/typehints/typehints_test.py @@ -523,6 +523,16 @@ 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.', + e.exception.args[0]) + class ListHintTestCase(TypeHintTestCase): def test_getitem_invalid_composite_type_param(self): @@ -582,6 +592,16 @@ 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.', + e.exception.args[0]) + class KVHintTestCase(TypeHintTestCase): def test_getitem_param_must_be_tuple(self): @@ -717,6 +737,16 @@ 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.', + e.exception.args[0]) + class BaseSetHintTest: class CommonTests(TypeHintTestCase): From bdd1ba1fb2ab04f05f088ae3f78a378ee308916f Mon Sep 17 00:00:00 2001 From: Jack McCluskey Date: Thu, 12 Jan 2023 18:15:42 +0000 Subject: [PATCH 07/10] restrict generic check to 3.9 and above --- sdks/python/apache_beam/typehints/typehints.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sdks/python/apache_beam/typehints/typehints.py b/sdks/python/apache_beam/typehints/typehints.py index f5da692f7e72..ba2f058e4332 100644 --- a/sdks/python/apache_beam/typehints/typehints.py +++ b/sdks/python/apache_beam/typehints/typehints.py @@ -1190,8 +1190,9 @@ def normalize(x, none_as_type=False): return type(None) elif x in _KNOWN_PRIMITIVE_TYPES: return _KNOWN_PRIMITIVE_TYPES[x] - elif isinstance(x, types.GenericAlias): - # TODO(https://github.com/apache/beam/issues/23366): handle PEP 585 generic type hints properly + 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.' % x) From 91eefc21b5af4235cb5e61cd00cc39e6a10d72c8 Mon Sep 17 00:00:00 2001 From: Jack McCluskey Date: Tue, 17 Jan 2023 19:40:50 +0000 Subject: [PATCH 08/10] add link to typing documentation --- sdks/python/apache_beam/typehints/typehints.py | 3 ++- sdks/python/apache_beam/typehints/typehints_test.py | 9 ++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/sdks/python/apache_beam/typehints/typehints.py b/sdks/python/apache_beam/typehints/typehints.py index ba2f058e4332..5cbb41e4d664 100644 --- a/sdks/python/apache_beam/typehints/typehints.py +++ b/sdks/python/apache_beam/typehints/typehints.py @@ -1195,7 +1195,8 @@ def normalize(x, none_as_type=False): # generic type hints properly raise TypeError( 'PEP 585 generic type hints like %s are not yet supported, ' - 'use typing module containers instead.' % x) + '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 112beca4d6ad..d4cdd4381aeb 100644 --- a/sdks/python/apache_beam/typehints/typehints_test.py +++ b/sdks/python/apache_beam/typehints/typehints_test.py @@ -530,7 +530,8 @@ def test_normalize_with_builtin_tuple(self): self.assertEqual( 'PEP 585 generic type hints like tuple[int, int] are not yet ' - 'supported, use typing module containers instead.', + 'use typing module containers instead. See equivalents listed ' + 'at https://docs.python.org/3/library/typing.html', e.exception.args[0]) @@ -599,7 +600,8 @@ def test_normalize_with_builtin_list(self): self.assertEqual( 'PEP 585 generic type hints like list[int] are not yet supported, ' - 'use typing module containers instead.', + 'use typing module containers instead. See equivalents listed ' + 'at https://docs.python.org/3/library/typing.html', e.exception.args[0]) @@ -744,7 +746,8 @@ def test_normalize_with_builtin_dict(self): self.assertEqual( 'PEP 585 generic type hints like dict[int, str] are not yet ' - 'supported, use typing module containers instead.', + 'supported, use typing module containers instead. See equivalents listed ' + 'at https://docs.python.org/3/library/typing.html', e.exception.args[0]) From 83141520fd6853700630594b87a7b79222df7ba6 Mon Sep 17 00:00:00 2001 From: Jack McCluskey Date: Tue, 17 Jan 2023 19:56:50 +0000 Subject: [PATCH 09/10] linting --- sdks/python/apache_beam/typehints/typehints_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdks/python/apache_beam/typehints/typehints_test.py b/sdks/python/apache_beam/typehints/typehints_test.py index d4cdd4381aeb..9900921f1b3e 100644 --- a/sdks/python/apache_beam/typehints/typehints_test.py +++ b/sdks/python/apache_beam/typehints/typehints_test.py @@ -746,8 +746,8 @@ def test_normalize_with_builtin_dict(self): 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', + 'supported, use typing module containers instead. See equivalents ' + 'listed at https://docs.python.org/3/library/typing.html', e.exception.args[0]) From 221b9ce93fa58e6360df6f7afd6e59e44a9a87af Mon Sep 17 00:00:00 2001 From: Jack McCluskey Date: Tue, 17 Jan 2023 20:20:07 +0000 Subject: [PATCH 10/10] fix incorrect testing string --- sdks/python/apache_beam/typehints/typehints_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdks/python/apache_beam/typehints/typehints_test.py b/sdks/python/apache_beam/typehints/typehints_test.py index 9900921f1b3e..7e2c390de320 100644 --- a/sdks/python/apache_beam/typehints/typehints_test.py +++ b/sdks/python/apache_beam/typehints/typehints_test.py @@ -530,8 +530,8 @@ def test_normalize_with_builtin_tuple(self): self.assertEqual( 'PEP 585 generic type hints like tuple[int, int] are not yet ' - 'use typing module containers instead. See equivalents listed ' - 'at https://docs.python.org/3/library/typing.html', + 'supported, use typing module containers instead. See equivalents ' + 'listed at https://docs.python.org/3/library/typing.html', e.exception.args[0])