Skip to content

Replace Enum with StrEnum for string-based enums #73

@masoudghah

Description

@masoudghah

Currently, in the Pydantic section, the example defines a string-based enum as:

from enum import Enum

class MusicBand(str, Enum):
    AEROSMITH = "AEROSMITH"
    QUEEN = "QUEEN"
    ACDC = "AC/DC"

A better approach in Python 3.11+ is to use StrEnum, which avoids the need for multiple inheritance (str, Enum) and makes the intent clearer:

from enum import StrEnum

class MusicBand(StrEnum):
    AEROSMITH = "AEROSMITH"
    QUEEN = "QUEEN"
    ACDC = "AC/DC"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions