Skip to content

Python: emit legacy-compatible model helpers once per package #24729

Description

@Abhinav0905

Description

The Python generator's compatibleWithPythonLegacy mode currently inlines the same legacy model conversion helpers into every generated generic, anyOf, and oneOf model. Kubernetes Python enables this mode for both its synchronous and asynchronous clients, resulting in the same helper implementation being emitted across roughly 1,538 model files (about 3.4 MB of duplicated source).

This was traced from kubernetes-client/python#2677, where a repository approver agreed with fixing the duplication upstream rather than editing generated Kubernetes client files.

Proposed change

  • Emit the legacy model conversion helpers once in a private generated package module for full client generation when compatibleWithPythonLegacy=true.
  • Import those helpers from model_generic.mustache, model_anyof.mustache, and model_oneof.mustache.
  • Reuse the shared identity helper from api_client.mustache where applicable.
  • Preserve the existing private helper names, function signatures, serialization behavior, and generated public API.
  • Preserve self-contained model-only generation by retaining the current inline helpers when supporting files are not generated.
  • Add focused generator tests and update the existing legacy-compatible Python sample.

This would not add another generator option. Projects that do not enable compatibleWithPythonLegacy would be unchanged.

Validation

The implementation verifies generated imports and model-only fallback behavior with focused generator tests. The regenerated legacy-compatible Python sample passes all 11 runtime tests and reduces its generated package from 151,102 to 140,230 bytes (7.20%) while reducing seven helper copies to one. After an upstream release/commit is available, Kubernetes Python can update its pinned generator and regenerate both clients in a separate PR.

Downstream context: kubernetes-client/python#2677

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions