This is an issue when a class inherits from Enum and is also decorated with @dataclass.

Why is this an issue?

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:

Enums 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.

What is the potential impact?

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.

How to fix it

Remove the @dataclass decorator from the Enum class. Enums work perfectly fine without it and have their own mechanisms for defining members.

Code examples

Noncompliant code example

from dataclasses import dataclass
from enum import Enum

@dataclass  # Noncompliant
class Status(Enum):
    PENDING = 1
    APPROVED = 2
    REJECTED = 3

Compliant solution

from enum import Enum

class Status(Enum):
    PENDING = 1
    APPROVED = 2
    REJECTED = 3

Resources

Documentation