mirror of
https://github.com/python/cpython.git
synced 2025-08-08 02:48:55 +00:00
[3.12] gh-105280: Ensure isinstance([], collections.abc.Mapping)
always evaluates to False
(GH-105281) (#105318)
gh-105280: Ensure `isinstance([], collections.abc.Mapping)` always evaluates to `False` (GH-105281)
(cherry picked from commit 08756dbba6
)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
51750269cf
commit
2031238eb6
3 changed files with 38 additions and 8 deletions
|
@ -1777,6 +1777,25 @@ del _pickle_psargs, _pickle_pskwargs
|
|||
class _ProtocolMeta(ABCMeta):
|
||||
# This metaclass is somewhat unfortunate,
|
||||
# but is necessary for several reasons...
|
||||
def __new__(mcls, name, bases, namespace, /, **kwargs):
|
||||
if name == "Protocol" and bases == (Generic,):
|
||||
pass
|
||||
elif Protocol in bases:
|
||||
for base in bases:
|
||||
if not (
|
||||
base in {object, Generic}
|
||||
or base.__name__ in _PROTO_ALLOWLIST.get(base.__module__, [])
|
||||
or (
|
||||
issubclass(base, Generic)
|
||||
and getattr(base, "_is_protocol", False)
|
||||
)
|
||||
):
|
||||
raise TypeError(
|
||||
f"Protocols can only inherit from other protocols, "
|
||||
f"got {base!r}"
|
||||
)
|
||||
return super().__new__(mcls, name, bases, namespace, **kwargs)
|
||||
|
||||
def __init__(cls, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
if getattr(cls, "_is_protocol", False):
|
||||
|
@ -1912,14 +1931,7 @@ class Protocol(Generic, metaclass=_ProtocolMeta):
|
|||
if not cls._is_protocol:
|
||||
return
|
||||
|
||||
# ... otherwise check consistency of bases, and prohibit instantiation.
|
||||
for base in cls.__bases__:
|
||||
if not (base in (object, Generic) or
|
||||
base.__module__ in _PROTO_ALLOWLIST and
|
||||
base.__name__ in _PROTO_ALLOWLIST[base.__module__] or
|
||||
issubclass(base, Generic) and getattr(base, '_is_protocol', False)):
|
||||
raise TypeError('Protocols can only inherit from other'
|
||||
' protocols, got %r' % base)
|
||||
# ... otherwise prohibit instantiation.
|
||||
if cls.__init__ is Protocol.__init__:
|
||||
cls.__init__ = _no_init_or_replace_init
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue