Skip to content
Sarj Code Standards

no-enum-literal-comparison

Compare enum values using named members.

Why

Raw comparisons bypass the enum declaration. A typo or change to an enum member can silently leave a comparison or match arm out of sync.

Fix

Use named enum members in comparisons, membership checks, and match arms. Parse raw input at its boundary when it needs to participate in enum dispatch. When a raw field is compared with a member's .value, declare the field with the enum type and compare members.

Examples

Before — flagged A raw string repeats an enum value
case.py
from enum import StrEnum
class Action(StrEnum):
BUILD = "build"
def is_build(action: Action) -> bool:
return action == "build"
After — preferred A named enum member anchors the comparison
case.py
from enum import StrEnum
class Action(StrEnum):
BUILD = "build"
def is_build(action: Action) -> bool:
return action == Action.BUILD