Cannot subclass from Protocol and Enum at the same time.
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 77.2k
- Forks
- 35.9k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit Protocol.init_subclass und der Erstellung von Enum-Mitgliedern und verwende den Reproducer im Issue, um ihre Reihenfolge zu beobachten. Sieh dir den verlinkten PR gh-149830 zur aktuellen Arbeit an. Als erledigt gilt, dass die Kombination aus Protocol und Enum nicht mehr den unerwarteten Instanziierungsfehler auslöst und die relevanten Tests erfolgreich sind.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- backend
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100