python / python/cpython

Cannot define Enum class with a mixin class derived from ABC

Đang mở
#119,946 15 bình luận 1 reaction 1 người được giao Xem trên GitHub

@ethanfurman đang làm issue này rồi.

Từ ngày 2/6/2024.

stdlib type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.