From b0c7eb9ce8bfe48b11075f7c8eba78bd0adc666b Mon Sep 17 00:00:00 2001 From: Randolf Scholz Date: Thu, 14 May 2026 17:29:07 +0200 Subject: [PATCH 1/2] gh-119946: make Protocol compatible with Enum instantiation --- Lib/test/test_enum.py | 20 ++++++++++++++++++++ Lib/typing.py | 6 +++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index 779457119e8f0e..ad7ab1ab3d4534 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -2478,6 +2478,26 @@ class SomeEnum(Enum): globals()['T'] = T test_pickle_dump_load(self.assertIs, SomeEnum.first) + def test_protocol_mixin_as_enum_member_value(self): + class Example(typing.Protocol): + def method(self) -> None: ... + + class Impl(Example): + def method(self) -> None: ... + + class CompatEnumType(type(typing.Protocol), EnumType): ... + + class ProtoEnum(Example, Enum, metaclass=CompatEnumType): + __qualname__ = 'ProtoEnum' # needed for pickle protocol 4 + impl = Impl() + + self.assertIsInstance(ProtoEnum.impl.value, Impl) + globals()['Example'] = Example + globals()['Impl'] = Impl + globals()['ProtoEnum'] = ProtoEnum + ProtoEnum.__reduce_ex__ = enum.pickle_by_enum_name + test_pickle_dump_load(self.assertIs, ProtoEnum.impl) + def test_duplicate_values_give_unique_enum_items(self): class AutoNumber(Enum): first = () diff --git a/Lib/typing.py b/Lib/typing.py index 130e09be4b9127..7e86c911758c11 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1882,7 +1882,11 @@ def _get_protocol_attrs(cls): def _no_init_or_replace_init(self, *args, **kwargs): cls = type(self) - if cls._is_protocol: + # Determine if this is a protocol or a concrete subclass. + # Note: not using cls._is_protocol here, + # since this may be called before __init_subclass__ is resolved. + # for example when subclassing enum.Enum. + if any(b is Protocol for b in cls.__bases__): raise TypeError('Protocols cannot be instantiated') # Already using a custom `__init__`. No need to calculate correct From 5ab82ba0d607a9639d47d674cd382b68aa296969 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 14 May 2026 15:44:30 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2026-05-14-15-44-25.gh-issue-149828.ADCPIl.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2026-05-14-15-44-25.gh-issue-149828.ADCPIl.rst diff --git a/Misc/NEWS.d/next/Library/2026-05-14-15-44-25.gh-issue-149828.ADCPIl.rst b/Misc/NEWS.d/next/Library/2026-05-14-15-44-25.gh-issue-149828.ADCPIl.rst new file mode 100644 index 00000000000000..ae99d03eed5aca --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-05-14-15-44-25.gh-issue-149828.ADCPIl.rst @@ -0,0 +1 @@ +Fixed an issue that prevented creation of an :class:`enum.Enum` that inherits from a subclass :class:`typing.Protocol`.