From 57164559199e276096d7419c97521588b28aeaff Mon Sep 17 00:00:00 2001 From: YouJiacheng <1503679330@qq.com> Date: Tue, 28 Mar 2023 19:28:38 +0800 Subject: [PATCH 1/4] Move builtin protocol whitelist to mapping instead of list and rename _PROTO_WHITELIST to _PROTO_ALLOWLIST taken from https://github.com/python/cpython/pull/15647 and https://github.com/python/cpython/pull/21825 --- src/typing_extensions.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/typing_extensions.py b/src/typing_extensions.py index b29e37cb..9166d46a 100644 --- a/src/typing_extensions.py +++ b/src/typing_extensions.py @@ -389,10 +389,13 @@ def clear_overloads(): TYPE_CHECKING = typing.TYPE_CHECKING -_PROTO_WHITELIST = ['Callable', 'Awaitable', - 'Iterable', 'Iterator', 'AsyncIterable', 'AsyncIterator', - 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible', - 'ContextManager', 'AsyncContextManager'] +_PROTO_ALLOWLIST = { + 'collections.abc': [ + 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable', + 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible', + ], + 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'], +} def _get_protocol_attrs(cls): @@ -608,8 +611,8 @@ def _proto_hook(other): # Check consistency of bases. for base in cls.__bases__: if not (base in (object, typing.Generic) or - base.__module__ == 'collections.abc' and - base.__name__ in _PROTO_WHITELIST or + base.__module__ in _PROTO_ALLOWLIST and + base.__name__ in _PROTO_ALLOWLIST[base.__module__] or isinstance(base, _ProtocolMeta) and base._is_protocol): raise TypeError('Protocols can only inherit from other' f' protocols, got {repr(base)}') From b7f0aba1fc96d4351fd0215d34835d87ed261adf Mon Sep 17 00:00:00 2001 From: YouJiacheng <1503679330@qq.com> Date: Tue, 28 Mar 2023 19:39:19 +0800 Subject: [PATCH 2/4] Add test taken from https://github.com/python/cpython/pull/15647 and https://github.com/python/cpython/pull/21823 --- src/test_typing_extensions.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test_typing_extensions.py b/src/test_typing_extensions.py index b4e21d63..6352603d 100644 --- a/src/test_typing_extensions.py +++ b/src/test_typing_extensions.py @@ -1909,6 +1909,14 @@ def close(self): self.assertIsSubclass(B, Custom) self.assertNotIsSubclass(A, Custom) + + def test_builtin_protocol_allowlist(self): + with self.assertRaises(TypeError): + class CustomProtocol(TestCase, Protocol): + pass + + class CustomContextManager(typing.ContextManager, Protocol): + pass def test_no_init_same_for_different_protocol_implementations(self): class CustomProtocolWithoutInitA(Protocol): From 166ed93a6a1ecb5f590f7969be3edd6a8ea499ef Mon Sep 17 00:00:00 2001 From: YouJiacheng <1503679330@qq.com> Date: Sat, 1 Apr 2023 18:31:27 +0800 Subject: [PATCH 3/4] fix linting failure blank line contains whitespace Co-authored-by: Alex Waygood --- src/test_typing_extensions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test_typing_extensions.py b/src/test_typing_extensions.py index 6352603d..50f75c38 100644 --- a/src/test_typing_extensions.py +++ b/src/test_typing_extensions.py @@ -1909,7 +1909,6 @@ def close(self): self.assertIsSubclass(B, Custom) self.assertNotIsSubclass(A, Custom) - def test_builtin_protocol_allowlist(self): with self.assertRaises(TypeError): class CustomProtocol(TestCase, Protocol): From 5f323ee32aff3241dc69440535dedb83f3e62e6c Mon Sep 17 00:00:00 2001 From: YouJiacheng <1503679330@qq.com> Date: Sat, 1 Apr 2023 18:42:26 +0800 Subject: [PATCH 4/4] Add empty line back Co-authored-by: Alex Waygood --- src/test_typing_extensions.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test_typing_extensions.py b/src/test_typing_extensions.py index 50f75c38..ab0cc256 100644 --- a/src/test_typing_extensions.py +++ b/src/test_typing_extensions.py @@ -1909,6 +1909,7 @@ def close(self): self.assertIsSubclass(B, Custom) self.assertNotIsSubclass(A, Custom) + def test_builtin_protocol_allowlist(self): with self.assertRaises(TypeError): class CustomProtocol(TestCase, Protocol):