Skip to content

Merge every class that inherits an enum into the enum - #6779

Closed
Koudai Aono (koxudaxi) wants to merge 1 commit into
fix-mapped-allof-value-keywordsfrom
merge-inherited-enum-classes
Closed

Koudai Aono (koxudaxi) wants to merge 1 commit into
fix-mapped-allof-value-keywordsfrom
merge-inherited-enum-classes

Conversation

@koxudaxi

@koxudaxi Koudai Aono (koxudaxi) commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Fixes: #6773

Summary by CodeRabbit

  • Bug Fixes
    • Enum inheritance is now handled for models that define their own fields, improving generated models for schemas that combine enum and object branches.
  • Tests
    • Added coverage for enum branches within combined JSON Schema definitions across supported output formats.

@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The inherited-enum pass now processes models with their own fields. A new JSON Schema regression test checks generated models for three backends, including Pydantic enum-value validation.

Changes: Inherited Enum Merging

Layer / File(s) Summary
Expand inherited-enum merge scope
src/datamodel_code_generator/parser/_passes/modules.py, src/datamodel_code_generator/model/_decisions/enum_kind.py, src/datamodel_code_generator/model/enum.py, docs/architecture.md
The pass no longer skips models with fields before checking their enum bases. Documentation now describes enum merging for models generally, rather than only field-less models.
Add JSON Schema regression coverage
tests/main/jsonschema/test_main_jsonschema.py, tests/main/jsonschema/test_main_jsonschema.refs, tests/data/expected/main/jsonschema/main_jsonschema/allof_any_of_enum_branch_object_keywords*
A regression test checks generated output for dataclasses, msgspec, and Pydantic v2. The Pydantic test also checks acceptance of listed enum values and rejection of an unlisted value.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

Possibly related PRs

Merge Risk: Minimal · up to 562e4

This change fixes the generated class that inherited from both Enum and BaseModel, which caused a metaclass conflict on import. It adds regression coverage for three output backends. No merge-blocking risk is evident.

Pre-merge checks: Passed 3 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Linked Issues check Inconclusive Objective [#6773]: the generated module must import for an allOf over anyOf of an object $ref and an enum, with required beside it. Enum branches must not inherit BaseModel.

Stack status: Addressed o…
Review the stack members #6775 to #6778 with current evidence. Confirm that the merged stack, at head 562e4d2, generates an importable module for the issue input. Confirm that the test input file matches the issue task.json content. Conf…
Passed Passed checks: 3 passed

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title states the main change: the enum-merge pass now merges every class that inherits an enum, not only field-less classes. The raw summary shows that extract_inherited_enum no longer skips model…
Out of Scope Changes check Passed All changes introduced by #6779 relate to the fix for #6773. The modules.py change widens extract_inherited_enum to models with fields. The enum.py and enum_kind.py docstrings and `docs/archit…
Full details: Title check

Explanation

The title states the main change: the enum-merge pass now merges every class that inherits an enum, not only field-less classes. The raw summary shows that extract_inherited_enum no longer skips models with fields, and the linked issue #6773 concerns a class that inherits both Enum and BaseModel. The title is short, specific, and matches the changeset.

Full details: Out of Scope Changes check

Explanation

All changes introduced by #6779 relate to the fix for #6773. The modules.py change widens extract_inherited_enum to models with fields. The enum.py and enum_kind.py docstrings and docs/architecture.md describe that same behavior. The new regression test in test_main_jsonschema.py, its .refs snapshot, and the three generated expected output files cover the issue case across the three output model types. No change is unrelated to the issue. The excluded test input JSON is not assessed for scope. Its content is not reviewed.

Full details: Linked Issues check

Explanation

Objective [#6773]: the generated module must import for an allOf over anyOf of an object $ref and an enum, with required beside it. Enum branches must not inherit BaseModel.

Stack status: Addressed or Partially addressed cannot be confirmed. PR #6775, #6776, #6777, and #6778 are uncertain and have no current review evidence. The current PR base (70dbc6c) is the head of #6778, so the code that the current PR changes depends on that uncertain stack.

Current PR evidence (#6779, head 562e4d2): the summary states that extract_inherited_enum in src/datamodel_code_generator/parser/_passes/modules.py no longer skips models with fields. It now merges inherited enums for those models. The expected snapshots for pydantic_v2.BaseModel, msgspec.Struct, and dataclasses.dataclass show Result4 as a plain Enum (Result4(Enum)), and Result3(Detail, Result2) keeps the object branch. This supports the fix for #6773 at the current head.

Unresolved uncertainty: (1) the test input tests/data/jsonschema/allof_any_of_enum_branch_object_keywords.json is excluded by the !tests/data/**/*.json rule, so the test input is not reviewable. (2) The current PR's behavior depends on the uncertain base stack. (3) The summary is a prior model observation. No direct code inspection was made for this assessment. The generated expected output also has no import-level verification in this review.

Resolution

Review the stack members #6775 to #6778 with current evidence. Confirm that the merged stack, at head 562e4d2, generates an importable module for the issue input. Confirm that the test input file matches the issue task.json content. Confirm that the new pytest case runs and passes.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

LanguageTool

docs/architecture.md

LanguageTool checks are incomplete because the per-file request limit of 5 was reached. Remaining text was skipped; findings from completed checks are retained.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 70dbc6c and 562e4d2.

Files ignored due to path filters: 2

  • docs/llms-full.txt is excluded by none and included by none
  • tests/data/jsonschema/allof_any_of_enum_branch_object_keywords.json is excluded by !tests/data/**/*.json and included by none
Files selected for processing: 9

  • docs/architecture.md
  • src/datamodel_code_generator/model/_decisions/enum_kind.py
  • src/datamodel_code_generator/model/enum.py
  • src/datamodel_code_generator/parser/_passes/modules.py
  • tests/data/expected/main/jsonschema/main_jsonschema/allof_any_of_enum_branch_object_keywords[dataclasses.dataclass].py
  • tests/data/expected/main/jsonschema/main_jsonschema/allof_any_of_enum_branch_object_keywords[msgspec.Struct].py
  • tests/data/expected/main/jsonschema/main_jsonschema/allof_any_of_enum_branch_object_keywords[pydantic_v2.BaseModel].py
  • tests/main/jsonschema/test_main_jsonschema.py
  • tests/main/jsonschema/test_main_jsonschema.refs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.

@koxudaxi
Koudai Aono (koxudaxi) marked this pull request as ready for review November 12, 2026 20:40
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T20:41:28.318574Z 562e4d2 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

Copy link
Copy Markdown
Contributor

@codspeed

codspeed Bot commented Nov 12, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 23.21%

2 warnings

⚠️ Walltime results may be unreliable

Some benchmarks used the Walltime instrument outside CodSpeed Macro Runners, which can make the results inconsistent.

Learn more about Macro Runners

See affected benchmarks

⚠️ 50 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 50 improved benchmarks
✅ 3 untouched benchmarks
⏩ 98 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_perf_unique_items_nested_model_validation 2.5 ms 1.7 ms +48.4%
⚡ test_perf_unique_items_runtime_scalar_validation 2.4 ms 1.6 ms +46.64%
⚡ test_perf_pydantic_field_name_resolution 1.9 ms 1.4 ms +42.83%
⚡ test_perf_empty_pydantic_v2_field_render_plans 28.7 ms 21.1 ms +36.04%
⚡ test_perf_simple_pydantic_v2_field_construction 8.6 ms 6.5 ms +32.51%
⚡ test_perf_graphql_style_pydantic_v2 626.5 ms 473.2 ms +32.41%
⚡ test_perf_large_models_pydantic_v2 2.7 s 2 s +32.25%
⚡ test_perf_load_pyproject_config[mapping-None] 145 µs 112.7 µs +28.65%
⚡ test_perf_unique_items_schema_validators 1.6 s 1.3 s +28.09%
⚡ test_perf_extreme_large_schema 11.2 s 8.7 s +27.88%
⚡ test_perf_duplicate_names 776.5 ms 607.7 ms +27.77%
⚡ test_perf_unique_items_collapsed_builtin 1.6 s 1.2 s +27.52%
⚡ test_perf_load_pyproject_config[mapping-multiple] 155.5 µs 122.2 µs +27.28%
⚡ test_perf_client_calls[sync] 25.1 ms 19.8 ms +27.1%
⚡ test_perf_client_calls[async] 28 ms 22 ms +27.05%
⚡ test_perf_aws_style_openapi_pydantic_v2 1.7 s 1.4 s +26.77%
⚡ test_perf_all_options_enabled 4.4 s 3.5 s +26.26%
⚡ test_perf_deep_nested 4.1 s 3.2 s +26.21%
⚡ test_perf_pattern_properties_adapter_reuse 2.9 ms 2.3 ms +25.86%
⚡ test_perf_load_pyproject_config[config-multiple] 155.2 µs 123.3 µs +25.84%
... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing merge-inherited-enum-classes (562e4d2) with main (f16595d)2

Open in CodSpeed

Footnotes

  1. 98 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on fix-mapped-allof-value-keywords (70dbc6c) during the generation of this report, so main (f16595d) was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@codecov

codecov Bot commented Nov 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

allOf over anyOf of a $ref and an enum writes an unimportable class

1 participant