This is an issue when a class inherits from Enum and is also decorated with @dataclass.
The @dataclass decorator and Enum classes are incompatible and should not be used together.
The @dataclass decorator automatically generates special methods like __init__, __repr__, and
__eq__ for regular classes. It modifies the class definition to add these methods based on the class attributes.
However, Enum classes use a special metaclass called EnumMeta that controls how enum members are created and behave. Enum
members are not regular instances - they are singleton constants that are created when the class is defined.
When you try to combine these two features, several problems occur:
@dataclass decorator tries to modify the class in ways that conflict with the Enum metaclass__init__ generation from @dataclass interferes with how Enum members are instantiatedTypeError because the modifications are incompatibleEnums are designed to represent a fixed set of named constants, while dataclasses are meant to hold data with potentially varying values. These are fundamentally different purposes that should not be mixed.
This code pattern will cause a TypeError at runtime when Python attempts to create the class. The application will fail to start or
the module will fail to import, preventing the code from running at all.
This is a critical reliability issue that blocks normal program execution.
Remove the @dataclass decorator from the Enum class. Enums work perfectly fine without it and have their own mechanisms for defining
members.
from dataclasses import dataclass
from enum import Enum
@dataclass # Noncompliant
class Status(Enum):
PENDING = 1
APPROVED = 2
REJECTED = 3
from enum import Enum
class Status(Enum):
PENDING = 1
APPROVED = 2
REJECTED = 3