Cannot define Enum class with a mixin class derived from ABC
オープン
@ethanfurman がすでに取り組んでいます。
2024年6月2日 から。
stdlib
type-bug
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 36k
- PR マージ指標
- PR 指標を取得中
説明
Bug report
Bug description:
from abc import ABC, abstractmethod
from enum import Enum
class HasColor(ABC):
@abstractmethod
def color(self):
...
class Flowers(HasColor, Enum):
ROSES = 1
VIOLETS = 2
def color(self):
match self:
case self.ROSES:
return 'red'
case self.VIOLETS:
return 'blue'
This results in:
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
There is a work-around by metaclass hacking. But these two basic Python features are expected to Just Work together in a simple, intuitive, and pythonic way.
The fix should be very simple - make EnumType a subclass of ABCMeta. But it's slightly more complicated than that, so that the _simple_enum decorator and _test_simple_enum continue to work as expected. See the provided patch.
CPython versions tested on:
3.12, 3.13
Operating systems tested on:
macOS
Linked PRs
- gh-119947
- gh-149830
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
評価
この issue はまだ評価されていません。