Skip to content

PY-ENUM0005 · prefer_enum_conversion

Prefer public conversions over direct standard StrEnum and IntEnum values.

This is a deterministic rule for python. Read its implementation.

Report .value reads only when local syntax proves that the receiver is a standard-behavior StrEnum or IntEnum. Proof may come from a direct local member, a member lookup, a local enum constructor, an unshadowed concrete annotation, or iteration over a known local enum class. Recognize direct and aliased imports from the standard enum module. Use str(member) for a StrEnum and int(member) for an IntEnum. Each proven expression receives a safe UTF-8 byte edit that replaces the complete access. The value is the number of accesses found.

Each finding records the proven enum kind, conversion, source range, and complete replacement. Ordinary objects with a value attribute and enum types imported from application modules are not inferred from spelling. Concrete annotations must name a nonempty local enum class.

Do not report plain Enum, broad base annotations, ambiguous or rebound names, local classes with unknown mixins, or classes that define the relevant __str__ or __int__ conversion. Direct .value access remains appropriate when code deliberately needs a representation that differs from the enum’s public string or integer conversion.

Bad

`Color.RED.value`, `status.value` when `status: Status`, and `[item.value for item in Status]`.
Good

str(Color.RED), int(HttpCode.OK), and record.value for an ordinary model field.