Skip to content

Add option to sort generated model classes alphabetically #3087

@vdusek

Description

@vdusek

When regenerating models from an OpenAPI spec where schema definitions have shifted position (e.g. reorganized, alphabetized, or different serialization order from the docs toolchain), the output diff can be hundreds of lines of pure class reordering — zero semantic changes. This makes PRs harder to review, inflates git blame, and causes unnecessary merge conflicts.

Proposal

Add a CLI option (e.g. --sort-classes-alphabetically) that sorts generated class definitions in a deterministic alphabetical order, independent of the spec's internal ordering.

Since base-class expressions in class Foo(Bar): are evaluated at runtime (even with from __future__ import annotations), a naive alphabetical sort would break. The sort would need to use topological ordering with alphabetical tie-breaking so that:

  • Base classes always precede their subclasses
  • Within each dependency level, classes are ordered A–Z
  • The output is fully deterministic regardless of input order

Note: --keep-model-order preserves spec order, but there is currently no option to enforce a canonical alphabetical order decoupled from the spec.

Use case

Any project that commits generated models to version control and regenerates them periodically. A deterministic output order eliminates noise diffs and keeps PRs focused on actual schema changes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions