Skip to content

Resolve a preset once for generate(), targets and dynamic generation - #6864

Merged
Koudai Aono (koxudaxi) merged 1 commit into
read-json-kinds-and-equality-from-one-rulefrom
resolve-a-preset-once-for-every-entry
Nov 15, 2026
Merged

Koudai Aono (koxudaxi) merged 1 commit into
read-json-kinds-and-equality-from-one-rulefrom
resolve-a-preset-once-for-every-entry

Conversation

@koxudaxi

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

Copy link
Copy Markdown
Member

Fixes: #6851

Summary by CodeRabbit

  • Bug Fixes
    • Server and client generation now report incompatible preset and target-version combinations with the same Error type as model-only generation. The error message and command-line behavior are unchanged.
  • Documentation
    • Clarified the order in which generation options are resolved, including how presets and implied settings affect generation and recommendation runs.
    • Added migration guidance for handling preset conflicts in target-generation runs.

@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change introduces shared, ordered option-resolution stages and applies them across target generation, facade preparation, dynamic generation, and recommendation trials. Server and client target runs now raise Error for preset and target-version conflicts.

Changes: Option resolution and generation

Layer / File(s) Summary
Shared option resolver and facade preparation
src/datamodel_code_generator/__init__.py, docs/architecture.md
The package adds ordered stages for preset, implied target, missing-sentinel, and implied-model settings. Facade preparation uses the shared resolver and checks outputs afterward. The architecture documentation describes the stages and their use.
Target generation option resolution
src/datamodel_code_generator/_api_generation.py, tests/main/test_option_stage_order.py, docs/migration/changes.md
Target generation resolves options through the shared stages and checks compatibility against the effective configuration. The API test and migration note describe preset conflicts raising Error instead of PresetError.
Preset resolution in generation and recommendation
src/datamodel_code_generator/dynamic.py, src/datamodel_code_generator/_recommend/__init__.py
Dynamic generation resolves the preset stage through the shared resolver. Recommendation planning stores preset updates and passes them to candidate generation and comparison.

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

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Minimal · up to de844

Server and client generation now raise the same Error as model-only runs when a preset conflicts with the target Python version, as the linked issue requests. The change is documented and tested, and no concrete merge-blocking defect remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title says a preset is resolved once across generate(), target generation, and dynamic generation. The raw summary shows this change: a shared staged resolver (_resolve_run_options) is used by _pr…
Linked Issues check Passed Objective [#6851]: generate() with generate_server or generate_client must raise datamodel_code_generator.Error, with the same message as the model-only run, when preset does not match target_python_v…
Out of Scope Changes check Passed The current PR changes are connected to the title objective, Resolve a preset once for generate(), targets and dynamic generation. (1) init.py: the staged resolver (preset, implied target, missing…
Full details: Title check

Explanation

The title says a preset is resolved once across generate(), target generation, and dynamic generation. The raw summary shows this change: a shared staged resolver (_resolve_run_options) is used by _prepare_generate_facade_config, prepare_target, and dynamic.py, and _recommend uses _resolve_preset once in _plan. This matches the main change and the linked issue #6851 objective of consistent preset error behavior.

Full details: Linked Issues check

Explanation

Objective [#6851]: generate() with generate_server or generate_client must raise datamodel_code_generator.Error, with the same message as the model-only run, when preset does not match target_python_version. Evidence from the current PR summary and head de8443b: (1) _api_generation.py:prepare_target now resolves target settings through the shared _resolve_run_options staged resolver, which applies the preset stage. This replaces the direct _given_fields path that called preset._resolve_config_preset without the PresetError-to-Error conversion. (2) The removed _prepare_generate_facade_config path and the inline preset resolution in init.py are consolidated into the same resolver used by the model-only path. (3) tests/main/test_option_stage_order.py now expects Error, not PresetError, for the server-target preset conflict. (4) The migration note in docs/migration/changes.md records that the error class is Error and the message is unchanged. Stack objective: this objective depends only on the current PR. No stack member is required for the server and client path. Limitation: the summary does not state the preset-stage wrapping line directly. The conclusion relies on the summary and the updated test expectation, not on a direct read of the resolver body.

Full details: Out of Scope Changes check

Explanation

The current PR changes are connected to the title objective, Resolve a preset once for generate(), targets and dynamic generation. (1) init.py: the staged resolver (preset, implied target, missing-sentinel, implied model) is the shared path for the fix. (2) _api_generation.py: prepare_target uses that path, which is the code path named in #6851. (3) _recommend/init.py and dynamic.py: these use the same single preset resolution (_resolve_preset, and resolving only the preset run option), which the title requires. (4) tests/main/test_option_stage_order.py: the expectation change follows directly from the fix. (5) docs/architecture.md and docs/migration/changes.md: these document the changed stage order and the new Error behavior. No change was found that is demonstrably unrelated to preset resolution. The missing-sentinel and implied-model updates are part of the staged resolver refactor. Their behavior was not verified against tests in this review, so the assessment rests on the summary.

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 f23be31 and de8443b.

Files ignored due to path filters: 4

  • docs/data/migration.json is excluded by none and included by none
  • docs/data/migration/unreleased/target-preset-error-class.toml is excluded by none and included by none
  • docs/llms-full.txt is excluded by none and included by none
  • tests/data/expected/main/option_stage_order/api_target_preset_error_before_sentinel_check.txt is excluded by !tests/data/**/*.txt and included by none
Files selected for processing: 7

  • docs/architecture.md
  • docs/migration/changes.md
  • src/datamodel_code_generator/__init__.py
  • src/datamodel_code_generator/_api_generation.py
  • src/datamodel_code_generator/_recommend/__init__.py
  • src/datamodel_code_generator/dynamic.py
  • tests/main/test_option_stage_order.py

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

@koxudaxi
Koudai Aono (koxudaxi) marked this pull request as ready for review November 15, 2026 10:05
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 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-15T10:06:16.194840Z de8443b 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 15, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 26.58%

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

⚠️ 45 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

⚡ 45 improved benchmarks
✅ 8 untouched benchmarks
⏩ 98 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_perf_unique_items_nested_model_validation 2.5 ms 1.6 ms +56.85%
⚡ test_perf_unique_items_runtime_scalar_validation 2.4 ms 1.5 ms +54.82%
⚡ test_perf_graphql_style_pydantic_v2 627 ms 427.9 ms +46.53%
⚡ test_perf_simple_pydantic_v2_field_construction 8.8 ms 6.1 ms +44.7%
⚡ test_perf_pydantic_field_name_resolution 1.9 ms 1.3 ms +43.27%
⚡ test_perf_aws_style_openapi_pydantic_v2 1.8 s 1.2 s +43.15%
⚡ test_perf_pattern_properties_adapter_reuse 3 ms 2.1 ms +39.35%
⚡ test_perf_stripe_style_pydantic_v2 1.8 s 1.3 s +38%
⚡ test_perf_load_pyproject_config[mapping-None] 145.9 µs 107.4 µs +35.9%
⚡ test_perf_large_models_pydantic_v2 2.7 s 2 s +33.44%
⚡ test_perf_extreme_large_schema 11.3 s 8.5 s +33.22%
⚡ test_perf_load_pyproject_config[mapping-multiple] 158.2 µs 118.9 µs +33.01%
⚡ test_perf_unique_items_runtime_nested_object_validation 5.7 ms 4.3 ms +32.2%
⚡ test_perf_load_pyproject_config[config-multiple] 157.3 µs 119.1 µs +32.05%
⚡ test_perf_duplicate_names 788.6 ms 598 ms +31.88%
⚡ test_perf_empty_pydantic_v2_field_render_plans 27.7 ms 21.1 ms +31.34%
⚡ test_perf_unique_items_collapsed_builtin 1.5 s 1.2 s +30.76%
⚡ test_perf_client_calls[async] 27.8 ms 21.5 ms +29.17%
⚡ test_perf_client_calls[sync] 24.9 ms 19.4 ms +28.62%
⚡ test_perf_load_pyproject_config[config-None] 142.7 µs 111 µs +28.57%
... ... ... ... ...

ℹ️ 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 resolve-a-preset-once-for-every-entry (de8443b) with read-json-kinds-and-equality-from-one-rule (f23be31)

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. ↩

@codecov

codecov Bot commented Nov 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@koxudaxi
Koudai Aono (koxudaxi) merged commit b31eb13 into main Nov 15, 2026
118 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the resolve-a-preset-once-for-every-entry branch November 15, 2026 19:16
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: Breaking changes detected

Reasoning: The PR moves option resolution into one staged helper, _resolve_run_options, which runs the preset, implied-target, sentinel and implied-model stages in that order. It also adds a shared _resolve_preset that turns PresetError into Error. For generate() model-only runs and for dynamic generation, the stages, their order and the error mapping stay as they were. For the experimental generate_server and generate_client targets, which 0.96.0 already has, the preset used to be resolved through the removed _given_fields without catching PresetError, so a preset that did not fit raised PresetError, which is not an Error subclass. It now raises Error. The updated expected test output and the migration entry marked action required both confirm this, so code catching PresetError must change, which makes it an error handling breaking change for an experimental feature. The target checks now read the resolved config rather than the config before the preset, which only removes wrong refusals. The recommender now resolves its preset once instead of twice and gives the trial the same updates. Neither changes released behaviour in a breaking way. In the behaviour diff gate every cell is both-fail, with no accepted or violation cells, so default generated output does not change. No CLI options, public Python names or templates are removed or changed.

Content for Release Notes

Error Handling Changes

  • Server and client targets raise Error for a preset that does not fit the target Python version (experimental) - When generate() runs with the experimental generate_server or generate_client target and its preset does not match target_python_version, it now raises datamodel_code_generator.Error with the same message a model-only run gives. Before, it raised datamodel_code_generator.preset.PresetError, which is not a subclass of Error, so code that caught PresetError around a target run no longer catches it and should catch datamodel_code_generator.Error instead. The command line is unchanged. (Resolve a preset once for generate(), targets and dynamic generation #6864)

This analysis was produced by the release draft workflow

@github-actions

Copy link
Copy Markdown
Contributor

🎉 Released in 0.97.0

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.

generate() with a server or client target raises PresetError instead of Error for a preset that does not match the target Python version

1 participant