Cannot subclass from Protocol and Enum at the same time.
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
Bug description:
x-ref: https://github.com/python/cpython/issues/119946#issuecomment-4418882668
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
- gh-149830
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with Protocol.init_subclass and Enum member creation, using the reproducer in the issue to observe their ordering. Review linked PR gh-149830 for the current work. Done means the Protocol-and-Enum combination no longer raises the unexpected instantiation error and the relevant tests pass.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100