Skip to content

Fix type overrides for base refs#3589

Merged
koxudaxi merged 1 commit into
mainfrom
fix/type-overrides-base-refs
Jul 7, 2026
Merged

Fix type overrides for base refs#3589
koxudaxi merged 1 commit into
mainfrom
fix/type-overrides-base-refs

Conversation

@koxudaxi

@koxudaxi koxudaxi commented Jul 7, 2026

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

  • New Features
    • Enhanced --type-overrides model-level support, including replacement of referenced types in field annotations and allOf-inherited base classes.
    • Improved model-level handling for nested dictionary key types.
  • Bug Fixes
    • Generated code now correctly skips overridden models while ensuring referenced types remain importable, including for base-class scenarios.
  • Documentation
    • Updated CLI reference to clarify model-level “Override Formats” semantics and reference-replacement behavior.
  • Tests
    • Added/updated integration tests and golden fixtures for model-level base-class and dict-key overrides.

@coderabbitai

coderabbitai Bot commented Jul 7, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 5fcef507-05f3-401b-9c8f-9ced8117de97

📥 Commits

Reviewing files that changed from the base of the PR and between 9edf7a4 and 892b169.

⛔ Files ignored due to path filters (3)
  • docs/llms-full.txt is excluded by none and included by none
  • tests/data/jsonschema/type_overrides_base_class.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/jsonschema/type_overrides_dict_key.json is excluded by !tests/data/**/*.json and included by none
📒 Files selected for processing (5)
  • docs/cli-reference/typing-customization.md
  • src/datamodel_code_generator/parser/base.py
  • tests/data/expected/main/type_overrides_base_class.py
  • tests/data/expected/main/type_overrides_dict_key.py
  • tests/main/test_main_general.py
✅ Files skipped from review due to trivial changes (1)
  • docs/cli-reference/typing-customization.md
🚧 Files skipped from review as they are similar to previous changes (3)
  • tests/data/expected/main/type_overrides_dict_key.py
  • tests/main/test_main_general.py
  • src/datamodel_code_generator/parser/base.py

📝 Walkthrough

Walkthrough

This PR updates model-level --type-overrides handling so overrides propagate through field annotations, allOf base classes, and dict keys. It also adds matching fixtures, integration tests, and CLI documentation updates.

Changes

Model-level Override Propagation

Layer / File(s) Summary
Override application
src/datamodel_code_generator/parser/base.py
Parser now separates model-level override imports, filters matching models, and recursively applies overrides to fields, base classes, and nested data types while updating imports.
Fixtures and integration tests
tests/data/expected/main/type_overrides_base_class.py, tests/data/expected/main/type_overrides_dict_key.py, tests/main/test_main_general.py
New golden fixtures and integration tests cover model-level overrides for base-class replacement and dict-key replacement.
CLI docs
docs/cli-reference/typing-customization.md
The --type-overrides reference now describes model-level override replacement behavior in fields and inheritance bases.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: fixing type override handling for base references.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/type-overrides-base-refs

Warning

Tools execution failed with the following error:

Failed to run tools: 13 INTERNAL: Received RST_STREAM with code 2 (Internal server error)


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

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

📚 Docs Preview: https://pr-3589.datamodel-code-generator.pages.dev

@koxudaxi koxudaxi force-pushed the fix/type-overrides-base-refs branch from 2a9746a to a0f4fe1 Compare July 7, 2026 16:33
@codspeed-hq

codspeed-hq Bot commented Jul 7, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 53.28%

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 13 improved benchmarks
⏩ 98 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime test_perf_aws_style_openapi_pydantic_v2 2 s 1.2 s +56.91%
WallTime test_perf_openapi_large 3 s 1.9 s +55.96%
WallTime test_perf_deep_nested 5.3 s 3.4 s +55.74%
WallTime test_perf_complex_refs 2 s 1.3 s +55.64%
WallTime test_perf_kubernetes_style_pydantic_v2 2.6 s 1.7 s +54.86%
WallTime test_perf_graphql_style_pydantic_v2 808.3 ms 522.4 ms +54.71%
WallTime test_perf_stripe_style_pydantic_v2 2 s 1.3 s +53.67%
WallTime test_perf_large_models_pydantic_v2 3.6 s 2.3 s +53.5%
WallTime test_perf_multiple_files_input 3.5 s 2.3 s +53.35%
WallTime test_perf_all_options_enabled 5.4 s 3.6 s +52.55%
WallTime test_perf_duplicate_names 1,036.9 ms 685.5 ms +51.28%
WallTime test_perf_large_models_pydantic_v2_noformat 882.2 ms 597.2 ms +47.71%
WallTime test_perf_large_models_pydantic_v2_builtin 940.6 ms 639.6 ms +47.07%

Tip

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


Comparing fix/type-overrides-base-refs (892b169) with main (be80b48)

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
docs/cli-reference/typing-customization.md (1)

2816-2839: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Document dict-key propagation too.

These updated descriptions still omit that model-level overrides are also applied recursively to dict keys. That behavior is part of the current implementation, so the docs now underdescribe the CLI contract.

♻️ Suggested doc tweak
-| `{"ModelName": "package.Type"}` | Model-level: Skip generation; replace field and inheritance refs |
+| `{"ModelName": "package.Type"}` | Model-level: Skip generation; replace field, inheritance, and dict-key refs |
@@
-    References to that model are replaced in field annotations and `allOf` inheritance base classes.
+    References to that model are replaced in field annotations, `allOf` inheritance base classes, and dict keys.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/cli-reference/typing-customization.md` around lines 2816 - 2839, Update
the type-overrides documentation in the CLI reference to mention that
model-level overrides are applied recursively to dict keys as well as field
annotations and allOf inheritance. Keep the existing explanation in the
type-overrides section and the “Model-level overrides skip generation” note, but
extend the wording so the behavior of dict-key propagation is explicitly
described alongside the model-level override examples.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/main/test_main_general.py`:
- Around line 1614-1646: The test `test_type_overrides_model_level_base_class`
leaves the temporary `my_app` package imported, which can leak into later tests
and affect imports. Update the test setup/teardown so the `my_app` entries are
removed from `sys.modules` after `run_main_and_assert` completes, ensuring this
test does not reuse stale modules when `test_main_jsonschema` or other tests
create the same package name.

---

Nitpick comments:
In `@docs/cli-reference/typing-customization.md`:
- Around line 2816-2839: Update the type-overrides documentation in the CLI
reference to mention that model-level overrides are applied recursively to dict
keys as well as field annotations and allOf inheritance. Keep the existing
explanation in the type-overrides section and the “Model-level overrides skip
generation” note, but extend the wording so the behavior of dict-key propagation
is explicitly described alongside the model-level override examples.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 50db6942-c807-4901-b99d-b8b6a001dff5

📥 Commits

Reviewing files that changed from the base of the PR and between be80b48 and 2a9746a.

⛔ Files ignored due to path filters (2)
  • tests/data/jsonschema/type_overrides_base_class.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/jsonschema/type_overrides_dict_key.json is excluded by !tests/data/**/*.json and included by none
📒 Files selected for processing (5)
  • docs/cli-reference/typing-customization.md
  • src/datamodel_code_generator/parser/base.py
  • tests/data/expected/main/type_overrides_base_class.py
  • tests/data/expected/main/type_overrides_dict_key.py
  • tests/main/test_main_general.py

Comment thread tests/main/test_main_general.py
@koxudaxi koxudaxi force-pushed the fix/type-overrides-base-refs branch 2 times, most recently from c93048f to 9edf7a4 Compare July 7, 2026 16:42
@codecov

codecov Bot commented Jul 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (be80b48) to head (892b169).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #3589   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          164       164           
  Lines        35243     35287   +44     
  Branches      4063      4075   +12     
=========================================
+ Hits         35243     35287   +44     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@koxudaxi koxudaxi force-pushed the fix/type-overrides-base-refs branch from 9edf7a4 to 892b169 Compare July 7, 2026 16:57
@koxudaxi koxudaxi merged commit 025e33e into main Jul 7, 2026
62 checks passed
@koxudaxi koxudaxi deleted the fix/type-overrides-base-refs branch July 7, 2026 17:08
@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: PR #3589 fixes the existing opt-in --type-overrides feature. Previously, model-level overrides (keys without a dot) only replaced references in field annotations; references in allOf inheritance base classes and dict keys were left pointing at the skipped/removed model, producing broken generated code (a dangling reference to a class that is no longer generated and not imported). This PR extends the replacement to those positions too. No CLI options, Python API signatures, enum values, classes, or parameters were removed (deleted lines are internal implementation refactors plus a doc-table wording tweak). No default behavior changes for users who don't use --type-overrides, no Python versions dropped, and no error-handling changes. The only output change is confined to an already opt-in path where the prior output was non-functional, so this is a corrective bug fix rather than a breaking change to working setups.


This analysis was performed by Claude Code Action

@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

🎉 Released in 0.68.1

This PR is now available in the latest release. See the release notes for details.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant