Skip to content

Commit ac6bf25

Browse files
committed
feat: add emit_str_enum config option
1 parent 2900e24 commit ac6bf25

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

internal/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type Config struct {
77
Package string `json:"package"`
88
Out string `json:"out"`
99
EmitPydanticModels bool `json:"emit_pydantic_models"`
10+
EmitStrEnum bool `json:"emit_str_enum"`
1011
QueryParameterLimit *int32 `json:"query_parameter_limit"`
1112
InflectionExcludeTableNames []string `json:"inflection_exclude_table_names"`
1213
}

internal/gen.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -681,12 +681,19 @@ func buildModelsTree(ctx *pyTmplCtx, i *importer) *pyast.Node {
681681
mod.Body = append(mod.Body, buildImportGroup(std), buildImportGroup(pkg))
682682

683683
for _, e := range ctx.Enums {
684+
bases := []*pyast.Node{
685+
poet.Name("str"),
686+
poet.Attribute(poet.Name("enum"), "Enum"),
687+
}
688+
if i.C.EmitStrEnum {
689+
// override the bases to emit enum.StrEnum (only support in Python >=3.11)
690+
bases = []*pyast.Node{
691+
poet.Attribute(poet.Name("enum"), "StrEnum"),
692+
}
693+
}
684694
def := &pyast.ClassDef{
685-
Name: e.Name,
686-
Bases: []*pyast.Node{
687-
poet.Name("str"),
688-
poet.Attribute(poet.Name("enum"), "Enum"),
689-
},
695+
Name: e.Name,
696+
Bases: bases,
690697
}
691698
if e.Comment != "" {
692699
def.Body = append(def.Body, &pyast.Node{

0 commit comments

Comments
 (0)