Skip to content

Cannot subclass from Protocol and Enum at the same time. #149828

Description

@randolf-scholz

Bug report

Bug description:

x-ref: #119946 (comment)

When trying to create an Enum of classes that implement a certain Protocol, an unexpected 'Protocols cannot be instantiated' error is raised.

from enum import Enum, EnumType
from typing import Protocol

class Example(Protocol):
    def method(self) -> None: ...

class Impl(Example):
    def method(self) -> None: ...

Impl()  # OK

class CompatEnumType(type(Protocol), EnumType): ...

class ProtoEnum(Example, Enum, metaclass=CompatEnumType):
    impl = Impl()  # ERROR 'Protocols cannot be instantiated'

I have debugged this a bit and the issue is that _is_protocol is only set during Protocol.__init_subclass__, but Enum instantiates its members before this method is called.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on May 15, 2026
  2. picnixz commented on May 15, 2026

    @picnixz
    Member

    Closing as a dup of 119946 actually (since your PR targets another issue)

  3. randolf-scholz commented on May 15, 2026

    @randolf-scholz
    ContributorAuthor

    @picnixz Huh? This is a variant but different from that issue. My PR only fixes the variant described in #119946 (comment)

    #119946 itself is solved by @sobolevn 's workaround (#119946 (comment))

    EDIT: Oh dang I copied the wrong issue number in the PR title. The blurp-it uses #149828 though.

  4. randolf-scholz commented on May 15, 2026

    @randolf-scholz
    ContributorAuthor

    I fixed the incorrect issue number.

  5. picnixz commented on May 15, 2026

    @picnixz
    Member

    Ah ok! so I reopen this one!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions