How to implement Enum with mutable members, comparable and hashable by their index

I am writing a class to represent sequential stages of an industrial process, composed of ADMISSION, PROCESSING, QC and DELIVERY stages.

Each stage has a unique, progressive sequence number, a mnemonic name and a field keeping track of the number of instances going through it:

@dataclass
class Stage:
    seq_id:       int
    name:         str
    n_instances:  int

Since stages are well-known and not supposed to change during execution, I decided to gather them in an Enum.
I need said enum to have the following requirements:

enum members need to:

  1. be subclasses of Stage , in order to avoid accessing their value and making them easier to use (akin to IntEnum or StrEnum). In particular:

    1. be comparable by their seq_id (e.g. Stages.DELIVERY > Stages.PROCESSING is true)

    2. be usable as dictionary keys

    3. use name as their __str__ representation.

  2. have immutable, sequential seq_ids from 0 to n based on their declaration order

  3. name is specified at member declaration. Using auto() results in the lower-cased member name

I managed to address point 2 and 3 in my implementation (see below).
How can I implement point 1 (and its subpoints)?

My implementation

My idea is to use Stage instances as the enum members, and use their seq_ids as member values.

  • seq_id is no longer in the Stage class as it is stored directly
    in the enum member’s _value_; I made this choice because I deem
    seq_ids not to have meaning outside the enum.

  • __lt__ , __eq__, __str__ and __hash__ have been implemented
    inside the enum rather than the Stage class for the same reason.

  • Hashing is based on the member’s _value_, which is supposedly
    immutable
    (hashing Stage would’ve been tricky due to its
    mutability)

@dataclass
class Stage:
    #seq_id:       int
    name:         str
    n_instances:  int = 0


#should be superflous due to autonumbering(see below)
@unique
@total_ordering
#req no. 1 (members are subclasses of stage)
class Stages(Stage, Enum):
    
    #req no. 1.1 (ordering)
    def __lt__(self, other):
        if self.__class__ is other.__class__:
            return self.value < other.value
        return NotImplemented
    def __eq__(self, other):
        if self.__class__ is other.__class__:
            return self.value == other.value
        return NotImplemented
    
    #req no. 1.2 (can be dict key)
    def __hash__(self):
        return hash(self.seq_id)
    
    #req no. 1.3
    def __str__(self):
        return self.name


    def __new__(cls, label):
        #autonumbering enum pattern for req no. 2
        value = len(cls.__members__) + 1

        obj = Stage.__new__(cls, label)
        obj._value_ = value

        return obj

    #req no. 3
    @override
    def _generate_next_value_(name, start, count, last_values):
        return name.lower()

    ADMISSION = auto()
    PROCESSING = auto()
    QC = auto()
    DELIVERY = auto()

Unfortunately, this yields the following error:

TypeError: object.__new__() takes exactly one argument (the type to instantiate)

Can you help put me fix my implementation, or put me on the right track?

1

Python’s Enum doesn’t support subclassing directly. Instead, you can create Stage instances as enum members and assign a unique seq_id to each one.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật