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.
Definition
Section titled “Definition”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.
Evidence
Section titled “Evidence”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.
Exceptions
Section titled “Exceptions”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.
Examples
Section titled “Examples”Bad
`Color.RED.value`, `status.value` when `status: Status`, and `[item.value for item in Status]`.
Goodstr(Color.RED), int(HttpCode.OK), and record.value for an ordinary model field.
References
Section titled “References”- Cites “The Python Standard Library”,
StrEnum. Open reference - Cites “The Python Standard Library”,
IntEnum. Open reference - Cites “The Python Standard Library”, enum. Open reference