Skip to content

model_config moved to one top class #2501

@Rogalek

Description

@Rogalek

Right now when we use some parameters during model generation (for example --extra-fields=forbid) we will get this:

class ModelA(BaseModel):
    model_config = ConfigDict(
        extra="forbid",
    )
    fieldA: Annotated[
        fieldA, Field(description="The filed name of the field")
    ]


class ModelB(BaseModel):
    model_config = ConfigDict(
        extra="forbid",
    )
    fieldB: Annotated[
        int,
        Field(description="The field B number", ge=1, le=63),
    ]


class ModelC(BaseModel):
    model_config = ConfigDict(
        extra="forbid",
    )
    page: Annotated[
        str,

And this pattern follow for as many openapi models we have.

Could we maybe do something like this? Lets keep the code DRY.

class ProjectBaseModel(BaseModel):
    model_config = ConfigDict(
        extra="forbid",
    )

class ModelA(ProjectBaseModel):
    fieldA: Annotated[
        fieldA, Field(description="The filed name of the field")
    ]

class ModelB(ProjectBaseModel):
    fieldB: Annotated[
        int,
        Field(description="The field B number", ge=1, le=63),
    ]

class ModelC(ProjectBaseModel):
    page: Annotated[
        str,

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