Skip to content

Tell whether a field must be given a value and whether a required field keeps its default in one place each - #5912

Merged
Koudai Aono (koxudaxi) merged 2 commits into
next10-1from
next10-2
Nov 1, 2026
Merged

Koudai Aono (koxudaxi) merged 2 commits into
next10-1from
next10-2

Conversation

@koxudaxi

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

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of required fields with defaults across generated models, including constructor requirements, field ordering, and default assignment.
    • Required fields now retain their defaults consistently when the option to apply defaults to required fields is enabled.
  • Documentation
    • Clarified how defaults for required fields affect assignment classification and constructor requirements.

@coderabbitai

coderabbitai Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between d229fc0 and 2ec463e.

Files ignored due to path filters: 1
  • docs/llms-full.txt is excluded by none and included by none
Files selected for processing: 14
  • docs/architecture.md
  • src/datamodel_code_generator/model/_decisions/assignment.py
  • src/datamodel_code_generator/model/base.py
  • src/datamodel_code_generator/model/dataclass.py
  • src/datamodel_code_generator/model/msgspec.py
  • src/datamodel_code_generator/model/pydantic_base.py
  • src/datamodel_code_generator/model/pydantic_v2/dataclass.py
  • src/datamodel_code_generator/parser/_field_rows.py
  • src/datamodel_code_generator/parser/_passes/_shared.py
  • src/datamodel_code_generator/parser/_passes/plans.py
  • src/datamodel_code_generator/parser/_passes/render_prep.py
  • src/datamodel_code_generator/parser/_rw_split.py
  • src/datamodel_code_generator/parser/base.py
  • src/datamodel_code_generator/parser/jsonschema.py

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

Walkthrough

The change adds shared predicates for required-value and retained-default decisions. Parser and model code use them across field construction, inherited-field handling, constructor ordering, assignment classification, and rendering. The architecture documentation describes the predicates and their use.

Changes: Required fields and defaults

Layer / File(s) Summary
Shared decision predicates
src/datamodel_code_generator/parser/_field_rows.py, src/datamodel_code_generator/model/_decisions/assignment.py, docs/architecture.md
Adds uses_default_with_required and requires_value to centralize the respective conditions. The architecture documentation describes these decisions and where they are read.
Parser default-state handling
src/datamodel_code_generator/parser/_passes/_shared.py, src/datamodel_code_generator/parser/_passes/plans.py, src/datamodel_code_generator/parser/_rw_split.py, src/datamodel_code_generator/parser/base.py, src/datamodel_code_generator/parser/jsonschema.py
Parser field-building and inherited-field paths use uses_default_with_required for required-field defaults. Constructor conflict detection uses requires_value.
Model required-value decisions
src/datamodel_code_generator/model/base.py, src/datamodel_code_generator/model/dataclass.py, src/datamodel_code_generator/model/msgspec.py, src/datamodel_code_generator/model/pydantic_base.py, src/datamodel_code_generator/model/pydantic_v2/dataclass.py, src/datamodel_code_generator/parser/_passes/_shared.py, src/datamodel_code_generator/parser/_passes/render_prep.py
Model field classification, constructor ordering, default handling, and rendering use requires_value instead of inline required/default checks. Parser render preparation also uses the predicate when deciding whether a default applies.

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

Change: Refactor

Possibly related issues

Possibly related PRs

Merge Risk: Minimal · up to 2ec46

The refactor consolidates field-requiredness and default decisions without an identified behavior regression. No blocking issue remains identified; merge after normal checks pass.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title accurately summarizes the main change: centralizing the decisions for required field values and retained defaults. It is specific and clear enough for review history.
Docstring Coverage Passed Docstring coverage is 90.91% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentati…
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.
Full details: Docstring Coverage

Explanation

Docstring coverage is 90.91% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 22 functions across 13 files. (1 skipped: 1 unsupported.)

Finishing Touches
Generate docstrings
  • Commit to this branch
  • Create a new PR
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

@koxudaxi
Koudai Aono (koxudaxi) marked this pull request as ready for review November 1, 2026 11:48
@koxudaxi Koudai Aono (koxudaxi) changed the title next10 2 Tell whether a field must be given a value and whether a required field keeps its default in one place each Nov 1, 2026
@chatgpt-codex-connector

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

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-01T11:49:30.519460Z 2ec463e 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

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

@codspeed

codspeed Bot commented Nov 1, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 33.07%

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

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

❌ 52 regressed benchmarks
⏩ 98 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_perf_load_pyproject_config[mapping-None] 219.9 µs 368.9 µs -40.4%
❌ test_perf_load_pyproject_config[config-None] 217 µs 363 µs -40.23%
❌ test_perf_load_pyproject_config[mapping-multiple] 230.7 µs 382.6 µs -39.7%
❌ test_perf_load_pyproject_config[config-multiple] 230 µs 380.6 µs -39.58%
❌ test_perf_pattern_properties_adapter_reuse 2.5 ms 4.1 ms -39.09%
❌ test_perf_client_calls[sync] 26.7 ms 42.8 ms -37.53%
❌ test_perf_client_calls[async] 29.3 ms 46.7 ms -37.28%
❌ test_perf_recommend_options_full_kubernetes_style 11.4 s 17.9 s -36.13%
❌ test_perf_deep_nested 3.8 s 5.8 s -35.36%
❌ test_perf_extreme_large_schema 11.1 s 17.1 s -35.21%
❌ test_perf_scalar_root_collapsed_builtin 139.2 ms 213.8 ms -34.87%
❌ test_perf_kubernetes_style_pydantic_v2 1.9 s 2.9 s -34.81%
❌ test_perf_local_reference_file_resolution 91.3 ms 139.9 ms -34.74%
❌ test_perf_false_reference_validation 185.1 ms 283.6 ms -34.73%
❌ test_perf_ordinary_array_constraint_generation 1.4 s 2.1 s -34.58%
❌ test_perf_stripe_style_pydantic_v2 1.5 s 2.2 s -33.86%
❌ test_perf_target_render[fastapi-large] 4.3 s 6.5 s -33.72%
❌ test_perf_large_models_pydantic_v2 2.6 s 3.9 s -33.64%
❌ test_perf_large_models_typed_dict[legacy-requiredness] 828.3 ms 1,246.5 ms -33.55%
❌ test_perf_complex_refs 1.5 s 2.2 s -33.51%
... ... ... ... ...

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

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing next10-2 (2ec463e) with next10-1 (d229fc0)

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 1, 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 1b8df02 into main Nov 1, 2026
167 of 169 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the next10-2 branch November 1, 2026 16:17
@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR is an internal refactor that gives two repeated expressions a name each, with no change in behaviour. The new helper requires_value in model/_decisions/assignment.py returns field.required and not field.use_default_with_required. It replaces every inline copy of that expression in model/base.py, dataclass.py, msgspec.py, pydantic_base.py, pydantic_v2/dataclass.py, parser/_passes/_shared.py and render_prep.py. The new helper uses_default_with_required in parser/_field_rows.py returns required and use_default and has_default. It replaces the inline computations in parser/base.py _effective_default_state, jsonschema.py _parse_object_common_part, _rw_split.py, _passes/_shared.py apply_inherited_field_default and _passes/plans.py adopt. At every call site that previously left out the required check, required is already True: those branches either set field.required to True just before or run under the guard required and not field.required. So each result is logically the same as before. No CLI options, public API, template variables, defaults or error handling are removed or changed. The other edits are documentation updates in docs/architecture.md and docs/llms-full.txt. The behaviour diff gate did not run, and nothing in the diff points to a change in generated output.


This analysis was produced by the release draft workflow

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

🎉 Released in 0.88.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.

1 participant