Skip to content

Share the builtin formatter's block layouts, predicates and pyproject lookup, and define its constants once - #5519

Merged
Koudai Aono (koxudaxi) merged 1 commit into
dry-r40from
dry-r32
Oct 29, 2026
Merged

Koudai Aono (koxudaxi) merged 1 commit into
dry-r40from
dry-r32

Conversation

@koxudaxi

@koxudaxi Koudai Aono (koxudaxi) commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Bug Fixes
    • Formatter settings now handle explicit line-length values consistently and locate project configuration in the current or parent directories.
  • Refactor
    • Improved consistency across generated-code formatting, including imports, annotations, type aliases, and multiline expressions.

@coderabbitai

coderabbitai Bot commented Oct 29, 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 b7f93a5 and 0b8abf1.

Files selected for processing: 6
  • src/datamodel_code_generator/_builtin_formatter.py
  • src/datamodel_code_generator/_format_types.py
  • src/datamodel_code_generator/format.py
  • src/datamodel_code_generator/util.py
  • src/datamodel_code_generator/watch_dependencies.py
  • tests/test_format.py

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

Walkthrough

The formatter now shares formatting constants, project configuration lookup, and layout helpers. Formatting logic for generated Python constructs and output assembly has been reorganized around these shared helpers.

Changes: Built-in formatter

Layer / File(s) Summary
Shared formatting defaults and configuration
src/datamodel_code_generator/_format_types.py, src/datamodel_code_generator/_builtin_formatter.py, src/datamodel_code_generator/format.py, src/datamodel_code_generator/util.py, src/datamodel_code_generator/watch_dependencies.py, tests/test_format.py
Formatting defaults and project-file lookup are shared across formatter and dependency-watcher code. Formatter configuration delegates line-length validation and tool-config loading to shared helpers. Tests check constant identity, validation, and missing configuration.
Literal, call, and annotation layouts
src/datamodel_code_generator/_builtin_formatter.py
Shared helpers format literals, calls, unions, subscripts, and annotated expressions. Multiline expressions and line-length checks are handled in these formatting paths.
Generated assignments, aliases, and class bases
src/datamodel_code_generator/_builtin_formatter.py
Annotation assignments, function signatures, type aliases, TypedDict calls, and supported RootModel bases use shared formatting helpers.
Replacement collection and formatted output
src/datamodel_code_generator/_builtin_formatter.py
Import grouping, source-line replacements, top-level spacing, import/body joining, and identifier restoration use consolidated helpers.

Priority: Low

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Refactor

Merge Risk: Minimal · up to 0b8ab

The formatter consolidation has no established merge-blocking regression. The lambda issue predates this change and is not worsened by it; merge after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 26.32% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and specifically summarizes the main changes: sharing formatter layouts and predicates, centralizing pyproject lookup, and defining constants once.
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 26.32% which is insufficient. 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 57 functions across 6 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches: 💡 1
📝 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

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.

@chatgpt-codex-connector

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 🔄 Running since 2026-10-29T11:54:30.952165Z 13f578b PR opened
ℹ️ 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 Oct 29, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 32.04%

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

⚠️ Base and head ran on different runner types

Some benchmarks with significant performance changes were compared across different runner types, so the changes might not come from the code. Run base and head on the same runner type, or use CodSpeed Macro Runners for more stable results.

Learn more about Macro Runners

Open the report in CodSpeed to investigate

❌ 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_client_calls[sync] 24 ms 42.5 ms -43.61%
❌ test_perf_simple_msgspec_unset_field_rendering 305.1 ms 529.7 ms -42.4%
❌ test_perf_client_calls[async] 27.2 ms 46.3 ms -41.36%
❌ test_perf_recommend_options_full_kubernetes_style 12.5 s 20.5 s -38.85%
❌ test_perf_load_pyproject_config[mapping-multiple] 236 µs 383 µs -38.37%
❌ test_perf_scalar_root_collapsed_builtin 128.9 ms 208.8 ms -38.28%
❌ test_perf_load_pyproject_config[mapping-None] 231.5 µs 373.6 µs -38.05%
❌ test_perf_load_pyproject_config[config-multiple] 238.2 µs 384.3 µs -38.02%
❌ test_perf_load_pyproject_config[config-None] 228 µs 364.9 µs -37.51%
❌ test_perf_large_models_single_module_stdout 975.4 ms 1,539 ms -36.62%
❌ test_perf_target_render[fastapi-large] 4.2 s 6.6 s -36.1%
❌ test_perf_large_models_msgspec 1.3 s 2 s -35.64%
❌ test_perf_large_models_pydantic_v2_builtin 831.5 ms 1,287.1 ms -35.4%
❌ test_perf_large_models_pydantic_v2_builtin_double_quotes 854.6 ms 1,321 ms -35.3%
❌ test_perf_large_models_dataclass_builtin 892.4 ms 1,375.7 ms -35.13%
❌ test_perf_local_reference_file_resolution 84.2 ms 128 ms -34.2%
❌ test_perf_target_render[client-large] 5.2 s 7.9 s -33.82%
❌ test_perf_multiple_files_input 2.6 s 4 s -33.79%
❌ test_perf_large_models_pydantic_v2_noformat 834.4 ms 1,259.6 ms -33.76%
❌ test_perf_false_reference_validation 171.6 ms 258.8 ms -33.69%
... ... ... ... ...

ℹ️ 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 dry-r32 (0b8abf1) with dry-r40 (b7f93a5)

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 Oct 29, 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 62acde4 into main Oct 29, 2026
163 of 164 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the dry-r32 branch October 29, 2026 13:02
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR is an internal refactor of the built-in formatter. It moves the shared formatting constants such as DEFAULT_LINE_LENGTH, DEFAULT_KNOWN_FIRST_PARTY and STRING_PREFIX_PATTERN into the private _format_types module. The public format module still exposes them under the same names and values. The pyproject.toml lookup moves into util._find_pyproject_toml, and the private builtin formatter and watch_dependencies modules import it under their old names. Repeated layout code is pulled into small helpers like _nested_lines, _item_lines, _block, _joined_block, _parenthesized, _is_bit_or, _is_multiline and _replacement. Line-length validation and tool-config loading are shared through _check_builtin_line_length and _load_builtin_tool_config, which keep the same error message and the same fallback to an empty tool config. The rewritten formatting functions produce the same line layouts, trailing commas and indentation as the code they replace, so the generated output should not change. No CLI options, public API, templates, supported Python versions or error behaviour are removed or changed. The only removal is LONG_TARGET_PREFIX_LENGTH from the private _builtin_formatter module, and it is still available from format and _format_types. The behaviour diff gate shows accepted cells, but these compare against the last release and describe allOf schemas turning from BaseModel into RootModel or alias output. That kind of change cannot come from a formatter refactor and does not appear anywhere in this diff, so it belongs to other changes since the release. The remaining cells are both-fail or crash-fix, which do not change default behaviour.


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