ClassEnumDeclaration
Create a Python enum using the class-based syntax.
This generates enums using the class Name(Enum): syntax with member definitions inside the class body. Supports various member value styles including auto-generated values, explicit values, and custom base types.
import { ClassEnumDeclaration } from "@alloy-js/python";
<ClassEnumDeclaration auto baseType={"Enum" | "IntEnum" | "StrEnum" | "Flag" | "IntFlag"} decorators={Children[]} doc={Children} members={Array<EnumMemberProps>} />import { ClassEnumDeclaration } from "@alloy-js/python/stc";
ClassEnumDeclaration({ auto: boolean, baseType: "Enum" | "IntEnum" | "StrEnum" | "Flag" | "IntFlag", decorators: Children[], doc: Children, members: Array<EnumMemberProps>,}).children(children)| auto | optional boolean | Indicates that the enum members should be auto-generated. |
| baseType | optional “Enum” | “IntEnum” | “StrEnum” | “Flag” | “IntFlag” | The base type of the enum. One of: ‘Enum’, ‘IntEnum’, ‘StrEnum’, ‘Flag’, ‘IntFlag’. Defaults to ‘Enum’. |
| decorators | optional Children[] | Decorators rendered above class <Name>(<BaseType>):. See [unresolved link] for the source-order and falsy skip semantics. Not available on functional-enum syntax (Name = Enum(...)), which is a variable assignment rather than a class definition. |
| doc | optional Children | Optional docstring for the enum. |
| members | optional Array<EnumMemberProps> | Members of the enum as an array of objects. |
Examples
Section titled “Examples”<ClassEnumDeclaration
name="Direction"
members={[
{ name: "NORTH" },
{ name: "SOUTH" },
{ name: "EAST" },
{ name: "WEST" }
]}
/>
renders to:
class Direction(Enum):
NORTH = "NORTH"
SOUTH = "SOUTH"
EAST = "EAST"
WEST = "WEST"
With explicit values:
<ClassEnumDeclaration
name="Status"
members={[
{ name: "PENDING", value: 1 },
{ name: "ACTIVE", value: 2 },
{ name: "INACTIVE", value: 3 }
]}
/>
renders to:
class Status(Enum):
PENDING = 1
ACTIVE = 2
INACTIVE = 3
With auto() values:
<ClassEnumDeclaration
name="Color"
style="auto"
members={[
{ name: "RED" },
{ name: "GREEN" },
{ name: "BLUE" }
]}
/>
renders to:
class Color(Enum):
RED = auto()
GREEN = auto()
BLUE = auto()