Skip to content

Emit unformatted output when formatting fails#2737

Merged
koxudaxi merged 3 commits intomainfrom
feature/emit-unformatted-output-on-formatting-failure
Dec 22, 2025
Merged

Emit unformatted output when formatting fails#2737
koxudaxi merged 3 commits intomainfrom
feature/emit-unformatted-output-on-formatting-failure

Conversation

@koxudaxi
Copy link
Copy Markdown
Owner

@koxudaxi koxudaxi commented Dec 22, 2025

Fixes: #2315

Summary by CodeRabbit

  • Bug Fixes

    • Code formatting errors are now handled gracefully; unformatted code is emitted instead of causing failures, with warnings logged.
  • Tests

    • Added tests to verify fallback behavior when code formatting fails.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Dec 22, 2025

Warning

Rate limit exceeded

@koxudaxi has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 17 minutes and 37 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 475e407 and 4e49b06.

📒 Files selected for processing (1)
  • tests/main/test_main_general.py

Walkthrough

The changes add graceful error handling for code formatting failures. When code formatting raises an exception, a warning is logged and unformatted code is emitted instead of failing completely. This includes wrapping formatting calls in try/except blocks in the base parser and adding new tests to verify this fallback behavior.

Changes

Cohort / File(s) Summary
Error handling in parser
src/datamodel_code_generator/parser/base.py
Wraps code_formatter.format_code() calls with try/except blocks in _generate_module_output and _generate_empty_init_exports methods. When formatting fails, a UserWarning is logged and unformatted body is returned instead of propagating the exception.
Test coverage for formatting fallback
tests/main/test_main_general.py
Updated import to include CodeFormatter; added test_format_code_fallback_on_error and test_format_code_fallback_on_error_init_exports test cases that mock formatter failure, verify warning emission, and assert unformatted output is produced.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Specific areas requiring attention:
    • Logic of try/except blocks in _generate_module_output and _generate_empty_init_exports—verify exception types caught are appropriate and warnings convey sufficient context
    • Mock setup in tests—confirm CodeFormatter.format_code mocking properly simulates real formatting failure scenarios
    • Output validation in tests—ensure assertions on unformatted output (e.g., presence of "class Model", "name:", and exports indicators) align with actual generated code structure

Poem

🐰 When formatters fail and syntax breaks,
A gentle warning the rabbit makes,
Unformatted code, though rough and bare,
Helps debug the mess with loving care! ✨

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR implements fallback to unformatted output when formatting fails, but issue #2315 explicitly requests a --skip-formatting CLI option that allows skipping formatting entirely, not just on failure. Implement the requested --skip-formatting CLI option and generate.skip_formatting parameter to allow intentional skipping of formatting, as described in issue #2315.
Docstring Coverage ⚠️ Warning Docstring coverage is 71.43% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: wrapping code formatting calls with try/except to emit unformatted output when formatting fails.
Out of Scope Changes check ✅ Passed All changes are focused on implementing graceful formatting failure handling and related tests, which are within the intended scope of addressing formatting issues.

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.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/datamodel_code_generator/parser/base.py (1)

2840-2847: Consider adjusting stacklevel for better warning context.

The warning uses stacklevel=1, which points to this line in base.py rather than the user's call site. Using stacklevel=2 or higher would provide users with more helpful context about where in their code the formatting failure occurred.

Additionally, this try/except pattern is duplicated at lines 2876-2883. Consider extracting a helper method like _safe_format_code(code: str, formatter: CodeFormatter) -> str to reduce duplication.

🔎 Proposed refactor to reduce duplication
+    def _safe_format_code(self, body: str, code_formatter: CodeFormatter) -> str:
+        """Apply code formatting with graceful fallback on error."""
+        try:
+            return code_formatter.format_code(body)
+        except Exception as exc:  # noqa: BLE001
+            warn(
+                f"Failed to format code: {exc!r}. Emitting unformatted output.",
+                stacklevel=3,
+            )
+            return body
+
     def _generate_module_output(...):
         ...
         body = "\n".join(result)
         if config.code_formatter:
-            try:
-                body = config.code_formatter.format_code(body)
-            except Exception as exc:  # noqa: BLE001
-                warn(
-                    f"Failed to format code: {exc!r}. Emitting unformatted output.",
-                    stacklevel=1,
-                )
+            body = self._safe_format_code(body, config.code_formatter)
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2b4af3e and 475e407.

📒 Files selected for processing (2)
  • src/datamodel_code_generator/parser/base.py
  • tests/main/test_main_general.py
🧰 Additional context used
🧬 Code graph analysis (2)
src/datamodel_code_generator/parser/base.py (1)
src/datamodel_code_generator/format.py (1)
  • format_code (272-291)
tests/main/test_main_general.py (2)
src/datamodel_code_generator/format.py (2)
  • CodeFormatter (162-332)
  • PythonVersion (56-119)
src/datamodel_code_generator/__init__.py (3)
  • generate (393-859)
  • InputFileType (195-205)
  • AllExportsScope (249-257)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (9)
  • GitHub Check: 3.11 on macOS
  • GitHub Check: 3.14 on macOS
  • GitHub Check: 3.11 on Windows
  • GitHub Check: 3.10 on Windows
  • GitHub Check: 3.12 on Windows
  • GitHub Check: 3.14 on Windows
  • GitHub Check: 3.13 on Windows
  • GitHub Check: benchmarks
  • GitHub Check: Analyze (python)
🔇 Additional comments (3)
tests/main/test_main_general.py (3)

21-21: LGTM!

Import of CodeFormatter is appropriate for mocking in the new fallback tests.


1095-1117: Comprehensive test coverage for formatting fallback.

The test appropriately mocks the formatter to raise an exception, verifies the warning is emitted, and confirms that unformatted output is produced. Good coverage of the happy path for the fallback behavior.


1119-1140: Good coverage of init.py formatting fallback.

This test effectively covers the second code path where formatting can fail (in _generate_empty_init_exports). Using OpenAPI with AllExportsScope.Children is an appropriate way to exercise this path.

@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 22, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.36%. Comparing base (2b4af3e) to head (4e49b06).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2737   +/-   ##
=======================================
  Coverage   99.36%   99.36%           
=======================================
  Files          83       83           
  Lines       12123    12165   +42     
  Branches     1458     1459    +1     
=======================================
+ Hits        12046    12088   +42     
  Misses         45       45           
  Partials       32       32           
Flag Coverage Δ
unittests 99.36% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 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.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Dec 22, 2025

CodSpeed Performance Report

Merging #2737 will not alter performance

Comparing feature/emit-unformatted-output-on-formatting-failure (4e49b06) with main (2b4af3e)

Summary

✅ 70 untouched
⏩ 10 skipped1

Footnotes

  1. 10 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.

@koxudaxi koxudaxi merged commit 522101a into main Dec 22, 2025
37 checks passed
@koxudaxi koxudaxi deleted the feature/emit-unformatted-output-on-formatting-failure branch December 22, 2025 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Emit output without formatting to help investigate syntax errors

1 participant