Visitar URL original
Dispose parser on parse errors by koxudaxi · Pull Request #3390 · datamodel-code-generator/datamodel-code-generator · GitHub
Skip to content

Dispose parser on parse errors - #3390

Merged
Koudai Aono (koxudaxi) merged 2 commits into
mainfrom
dispose-parser-on-parse-errors
Jun 13, 2026
Merged

Koudai Aono (koxudaxi) merged 2 commits into
mainfrom
dispose-parser-on-parse-errors

Conversation

@koxudaxi

@koxudaxi Koudai Aono (koxudaxi) commented Jun 13, 2026 •

Copy link
Copy Markdown
Member

No description provided.

@coderabbitai

coderabbitai Bot commented Jun 13, 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: 7a97bbff-e4c9-46e8-a5ea-699ea6007f39

📥 Commits

Reviewing files that changed from the base of the PR and between 0322482 and dedf3a7.

📒 Files selected for processing (2)
  • src/datamodel_code_generator/__init__.py
  • tests/main/test_main_general.py

📝 Walkthrough

Walkthrough

The PR adds exception-safe cleanup for parser resources in the generate() function. When parser.parse() raises any BaseException, the function now calls parser._dispose() to release resources (suppressing any disposal errors) before re-raising the original error. A test validates this behavior for both standard exceptions and non-Exception BaseException subclasses.

Changes

Parser resource cleanup on parse errors

Layer / File(s) Summary
Parser disposal error handling
src/datamodel_code_generator/__init__.py
generate() wraps parser.parse() in try/except BaseException; on any parse failure, parser._dispose() is invoked with exceptions suppressed, then the original error is re-raised.
Test parser disposal on parse errors
tests/main/test_main_general.py
A test-only BaseException subclass _GenerateParseAbort is introduced to simulate non-Exception failures. Integration test test_generate_disposes_parser_when_parse_raises mocks _build_parser and validates parser._dispose() is called exactly once and the original error is re-raised for both RuntimeError and _GenerateParseAbort cases.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A parser that fails now cleans up with care,
Try-except BaseException handles despair,
Disposal assured, no resource left bare,
Tests prove the way both errors can pair,
Cleanup assured, the rabbit's aware! 🐰

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Dispose parser on parse errors' directly and clearly summarizes the main change: ensuring parser cleanup when parse operations fail.
Docstring Coverage ✅ Passed Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%.
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 dispose-parser-on-parse-errors

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

Copy link
Copy Markdown
Contributor

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

@codspeed

codspeed Bot commented Jun 13, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 15.41%

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

⚡ 11 improved benchmarks
⏩ 98 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
⚡ WallTime test_perf_openapi_large 3.2 s 2.7 s +16.88%
⚡ WallTime test_perf_all_options_enabled 5.9 s 5.1 s +16.37%
⚡ WallTime test_perf_multiple_files_input 3.9 s 3.3 s +16.36%
⚡ WallTime test_perf_graphql_style_pydantic_v2 867.7 ms 748 ms +16%
⚡ WallTime test_perf_aws_style_openapi_pydantic_v2 2 s 1.8 s +15.72%
⚡ WallTime test_perf_kubernetes_style_pydantic_v2 2.8 s 2.5 s +15.45%
⚡ WallTime test_perf_complex_refs 2.1 s 1.8 s +15.45%
⚡ WallTime test_perf_deep_nested 5.5 s 4.8 s +14.94%
⚡ WallTime test_perf_stripe_style_pydantic_v2 2.2 s 1.9 s +14.4%
⚡ WallTime test_perf_large_models_pydantic_v2 3.9 s 3.4 s +14.08%
⚡ WallTime test_perf_duplicate_names 1.1 s 1 s +13.91%

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 dispose-parser-on-parse-errors (dedf3a7) with main (0322482)

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 Jun 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (0322482) to head (dedf3a7).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #3390   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          138       138           
  Lines        29258     29274   +16     
  Branches      3504      3504           
=========================================
+ Hits         29258     29274   +16     
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
Koudai Aono (koxudaxi) merged commit bcd0b3c into main Jun 13, 2026
52 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the dispose-parser-on-parse-errors branch June 13, 2026 09:33
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: PR #3390 wraps the existing parser.parse() call in generate() with a try/except that disposes the parser (via the private _dispose() method) on any BaseException and then re-raises the original exception unchanged. The successful code-generation path is identical, so generated output is unaffected. No public API, CLI option, default value, template, or Python-version support changed. The original exception is re-raised verbatim (preserving type and identity), and dispose-time errors are suppressed so they cannot mask the real error — preserving rather than altering user-facing error behavior. The only added test code is non-breaking. This is an internal resource-cleanup bugfix with no breaking changes.


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