summaryrefslogtreecommitdiffstats
path: root/Lib/typing.py
diff options
context:
space:
mode:
authorDivij Rajkumar <drajkuma1@gmail.com>2019-09-12 10:13:51 (GMT)
committerIvan Levkivskyi <levkivskyi@gmail.com>2019-09-12 10:13:51 (GMT)
commit692a0dc91597b7fb350383b633dc4d044cbd360e (patch)
tree1efa3a3554b6c35e5c1d647776c08922a34fef92 /Lib/typing.py
parentea683deccc505a78bbbb1eb8c6a88b0835ad5151 (diff)
downloadcpython-692a0dc91597b7fb350383b633dc4d044cbd360e.zip
cpython-692a0dc91597b7fb350383b633dc4d044cbd360e.tar.gz
cpython-692a0dc91597b7fb350383b633dc4d044cbd360e.tar.bz2
bpo-38008: Move builtin protocol whitelist to mapping instead of list (GH-15647)
Fixes https://bugs.python.org/issue38008
Diffstat (limited to 'Lib/typing.py')
-rw-r--r--Lib/typing.py14
1 files changed, 9 insertions, 5 deletions
diff --git a/Lib/typing.py b/Lib/typing.py
index 5f1a0ad..7b07112 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -989,10 +989,13 @@ def _allow_reckless_class_cheks():
return True
-_PROTO_WHITELIST = ['Callable', 'Awaitable',
- 'Iterable', 'Iterator', 'AsyncIterable', 'AsyncIterator',
- 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
- 'ContextManager', 'AsyncContextManager']
+_PROTO_WHITELIST = {
+ 'collections.abc': [
+ 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
+ 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
+ ],
+ 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
+}
class _ProtocolMeta(ABCMeta):
@@ -1105,7 +1108,8 @@ class Protocol(Generic, metaclass=_ProtocolMeta):
# ... otherwise check consistency of bases, and prohibit instantiation.
for base in cls.__bases__:
if not (base in (object, Generic) or
- base.__module__ == 'collections.abc' and base.__name__ in _PROTO_WHITELIST or
+ base.__module__ in _PROTO_WHITELIST and
+ base.__name__ in _PROTO_WHITELIST[base.__module__] or
issubclass(base, Generic) and base._is_protocol):
raise TypeError('Protocols can only inherit from other'
' protocols, got %r' % base)