Skip to content

Commit

Permalink
Move builtin protocol whitelist to mapping instead of list
Browse files Browse the repository at this point in the history
and rename _PROTO_WHITELIST to _PROTO_ALLOWLIST

taken from python/cpython#15647 and python/cpython#21825
  • Loading branch information
YouJiacheng authored Mar 28, 2023
1 parent 37909ec commit 5716455
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)}')
Expand Down

0 comments on commit 5716455

Please sign in to comment.