You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tell whether a field must be given a value and whether a required field keeps its default in one place each - #5912
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.
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.
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 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.
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: 5
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
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
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
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
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.
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
⚠️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.
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
This PR is now available in the latest release. See the release notes for details.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary by CodeRabbit