Skip to content

Use builtin formatter in CI#3380

Merged
koxudaxi merged 3 commits into
mainfrom
ci-builtin-default-formatters
Jun 12, 2026
Merged

Use builtin formatter in CI#3380
koxudaxi merged 3 commits into
mainfrom
ci-builtin-default-formatters

Conversation

@koxudaxi

@koxudaxi koxudaxi commented Jun 12, 2026

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

  • New Features

    • Formatter: better handling of modern type-alias syntax so formatted output remains correct on older Python runtimes.
  • Bug Fixes

    • Formatter: improved comment detection to avoid treating hash signs inside strings as comments.
  • Tests

    • Added isolation mechanism, new tests and test-flow updates to ensure consistent builtin-formatter behavior across CLI/generation paths and CI environments.

@coderabbitai

coderabbitai Bot commented Jun 12, 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: 158cb65b-16f0-4bf8-8a29-9ac64c4dc025

📥 Commits

Reviewing files that changed from the base of the PR and between f7c08eb and 089c9c0.

📒 Files selected for processing (10)
  • pyproject.toml
  • src/datamodel_code_generator/_builtin_formatter.py
  • tests/conftest.py
  • tests/main/conftest.py
  • tests/main/jsonschema/test_main_jsonschema.py
  • tests/main/openapi/test_main_openapi.py
  • tests/test_conftest_helpers.py
  • tests/test_format.py
  • tests/test_main_kr.py
  • tox.ini
✅ Files skipped from review due to trivial changes (3)
  • tests/main/jsonschema/test_main_jsonschema.py
  • pyproject.toml
  • tests/main/openapi/test_main_openapi.py
🚧 Files skipped from review as they are similar to previous changes (7)
  • tests/test_main_kr.py
  • tests/conftest.py
  • tox.ini
  • tests/test_format.py
  • tests/test_conftest_helpers.py
  • src/datamodel_code_generator/_builtin_formatter.py
  • tests/main/conftest.py

📝 Walkthrough

Walkthrough

Adds token-based inline-comment detection and PEP 695 placeholder parsing to the builtin formatter, and introduces pytest marker/fixture and test-helper wiring to optionally isolate and apply a builtin formatter configuration for generation tests.

Changes

Comment detection and test isolation

Layer / File(s) Summary
Builtin formatter: comment tokens & PEP 695 parsing
src/datamodel_code_generator/_builtin_formatter.py
Add _has_comment_token tokenization helper, replace naive "#" in line checks, add PEP 695 placeholder detection/replacement (_pep695_type_alias_replacement, _collect_pep695_type_alias_replacements), and parse via _parse_builtin_code to support older runtimes.
Pytest marker and autouse fixture
pyproject.toml, tests/conftest.py
Register isolate_builtin_formatter_config marker and add autouse fixture that, when env var DATAMODEL_CODE_GENERATOR_TEST_DEFAULT_FORMATTER=builtin is set and a test is marked, creates a temp pyproject.toml with tool.datamodel-codegen.builtin-format-line-length = 88 and chdirs into it.
Test helpers: conditional builtin formatter wiring
tests/main/conftest.py
Refactor helpers to detect generation commands/config presence, optionally append --formatters builtin, and wrap main/generate test invocations with the temporary builtin-config context.
Apply isolation markers to test cases
tests/main/jsonschema/test_main_jsonschema.py, tests/main/openapi/test_main_openapi.py, tests/test_main_kr.py
Annotate multiple test functions with @pytest.mark.isolate_builtin_formatter_config to scope isolated builtin-config usage per test.
Tests validating the isolation & default injection
tests/test_conftest_helpers.py
Add tests for CLI detection, default formatter CLI injection only for generation commands, temporary config creation/removal, and suppression when pyproject.toml is present.
Comment-handling tests
tests/test_format.py
Add tests ensuring # inside string literals is not treated as an inline comment and that PEP 695 type-alias formatting is handled for older Python versions.
CI tox env for builtin default
tox.ini
Add parallel tox env selector for Python 3.10–3.14 that sets DATAMODEL_CODE_GENERATOR_TEST_DEFAULT_FORMATTER=builtin.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested labels

breaking-change-analyzed

Poem

🐰 I hop through tokens, careful and spry,

Guarding # in strings from a false goodbye.
Temp pyproject made snug for the test,
Builtin formatter tucked, behaving its best,
A little rabbit cheers the code — bye-bye mess!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.56% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ 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 concisely summarizes the main objective of the changeset: configuring the builtin formatter for CI environments through test infrastructure and tox configuration.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 ci-builtin-default-formatters

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 and usage tips.

@github-actions

github-actions Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

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

@codspeed-hq

codspeed-hq Bot commented Jun 12, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 12.23%

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

⚡ 5 improved benchmarks
✅ 6 untouched benchmarks
⏩ 98 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime test_perf_deep_nested 5.5 s 4.8 s +13.44%
WallTime test_perf_openapi_large 3.1 s 2.7 s +12.88%
WallTime test_perf_all_options_enabled 5.7 s 5 s +12.6%
WallTime test_perf_large_models_pydantic_v2 3.8 s 3.4 s +11.17%
WallTime test_perf_complex_refs 2.1 s 1.9 s +11.1%

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 ci-builtin-default-formatters (089c9c0) with main (0c49f24)

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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/datamodel_code_generator/_builtin_formatter.py (1)

1300-1300: ⚠️ Potential issue | 🟡 Minor

Use _has_comment_token for remaining inline-comment skipping checks.

_format_generated_class_definition (line 1300) and _format_generated_module_statement (line 1332) still use naive "#" in line guards; replace them with _has_comment_token(line) for consistency with _format_generated_class_statement (line 1042) and to avoid skipping formatting when # appears inside string literals.

🤖 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 `@src/datamodel_code_generator/_builtin_formatter.py` at line 1300, Replace the
naive " '#' in line" guard with the helper _has_comment_token(line) in both
_format_generated_class_definition and _format_generated_module_statement so
they mirror _format_generated_class_statement; specifically change the
conditional that currently reads like "if len(line) <= line_length or '#' in
line:" to use "_has_comment_token(line)" (keeping the len(line) <= line_length
check intact) so inline-comment detection uses the centralized helper.
🤖 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.

Outside diff comments:
In `@src/datamodel_code_generator/_builtin_formatter.py`:
- Line 1300: Replace the naive " '#' in line" guard with the helper
_has_comment_token(line) in both _format_generated_class_definition and
_format_generated_module_statement so they mirror
_format_generated_class_statement; specifically change the conditional that
currently reads like "if len(line) <= line_length or '#' in line:" to use
"_has_comment_token(line)" (keeping the len(line) <= line_length check intact)
so inline-comment detection uses the centralized helper.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: c78d7e48-7916-4269-81e8-b7c7ed58b17f

📥 Commits

Reviewing files that changed from the base of the PR and between 0c49f24 and 148c0da.

📒 Files selected for processing (10)
  • pyproject.toml
  • src/datamodel_code_generator/_builtin_formatter.py
  • tests/conftest.py
  • tests/main/conftest.py
  • tests/main/jsonschema/test_main_jsonschema.py
  • tests/main/openapi/test_main_openapi.py
  • tests/test_conftest_helpers.py
  • tests/test_format.py
  • tests/test_main_kr.py
  • tox.ini

@codecov

codecov Bot commented Jun 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (0c49f24) to head (089c9c0).

Additional details and impacted files
@@            Coverage Diff             @@
##              main     #3380    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files          129       129            
  Lines        28571     28832   +261     
  Branches      3453      3484    +31     
==========================================
+ Hits         28571     28832   +261     
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 ci-builtin-default-formatters branch from f7c08eb to 089c9c0 Compare June 12, 2026 19:45
@koxudaxi koxudaxi merged commit d7857fb into main Jun 12, 2026
53 checks passed
@koxudaxi koxudaxi deleted the ci-builtin-default-formatters branch June 12, 2026 20:06
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: The authoritative prepared diff could not be accessed: the Read tool permission for /home/runner/work/_temp/release-draft-pr/ was not granted, all Bash routes to that path were sandbox-blocked (working directory only), the local git checkout is a shallow single root commit (d7857fb has no parent, so git show renders the whole repo as additions and cannot reconstruct the real diff), and gh/network fetch requires unavailable approval. With no visibility into any removed lines, the ground rules forbid asserting any removal of options, parameters, classes, enum values, or functions. The only reliable signal — the merged commit metadata 'Use builtin formatter in CI (#3380)' with sub-commits 'Use builtin formatter in CI', 'Handle PEP 695 aliases in builtin formatter', and 'Cover builtin formatter CI helpers' — indicates a CI-tooling/builtin-formatter-helper change rather than a user-facing API, CLI, or generated-code default change. Absent any evidence of a breaking change and unable to confirm any removed lines, I report no breaking changes, with the caveat that this rests on PR metadata, not the diff itself.


This analysis was performed by Claude Code Action

@github-actions

Copy link
Copy Markdown
Contributor

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