1. Use StrEnum for string enums

Use StrEnum instead of loose string constants when the values are still meant to behave like strings.

That keeps the benefits of an enum type without losing the ergonomics of string values at boundaries such as JSON, logs, and config files. It also reduces typo-prone comparisons against free-floating literals.

Note

Python 3.11+

1.1. Don’t do this

1STATUS_DRAFT = 'draft'
2STATUS_PUBLISHED = 'published'

1.2. Do this

1from enum import StrEnum
2
3class Status(StrEnum):
4    DRAFT = 'draft'
5    PUBLISHED = 'published'