Skip to content

Fix IndexError when using --reuse-scope=tree with single file output#2954

Merged
koxudaxi merged 2 commits intomainfrom
fix/issue-2953-indexerror-reuse-scope-tree
Jan 10, 2026
Merged

Fix IndexError when using --reuse-scope=tree with single file output#2954
koxudaxi merged 2 commits intomainfrom
fix/issue-2953-indexerror-reuse-scope-tree

Conversation

@koxudaxi
Copy link
Copy Markdown
Owner

@koxudaxi koxudaxi commented Jan 10, 2026

Fixes: #2953

Summary by CodeRabbit

  • Bug Fixes
    • Improved module-name validation to ignore empty entries, reducing false-positive name collisions.
    • Added a clear validation error when combining --reuse-scope tree with single-file output, instructing users to provide an output directory instead.

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

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jan 10, 2026

📝 Walkthrough

Walkthrough

Filter out empty/falsy module entries when validating shared module names to avoid indexing into empty tuples; add a test asserting an error is raised when using --reuse-scope tree with a single-file output.

Changes

Cohort / File(s) Summary
Parser change
src/datamodel_code_generator/parser/base.py
__validate_shared_module_name now filters out empty/falsy module entries before accessing module[0], preventing IndexError during shared-module name collision checks.
Test addition
tests/main/openapi/test_main_openapi.py
Added test_main_openapi_reuse_scope_tree_single_file_error which asserts the CLI fails with "Modular references require an output directory, not a file" when --reuse-scope tree is used with a file output.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested labels

breaking-change-analyzed

Poem

🐰 A tuple once caused quite a fright,
Empty entries hid from sight,
I filtered what I could not see,
Now tests hop by, safe and free,
The shared tree hums — error handled right. 🥕

🚥 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 accurately describes the main fix: addressing an IndexError that occurs when using --reuse-scope=tree with single file output.
Linked Issues check ✅ Passed The code changes address issue #2953 by filtering empty module entries in __validate_shared_module_name to prevent IndexError, and adding regression test coverage.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the IndexError in __validate_shared_module_name and adding appropriate test coverage for the issue.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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

✨ Finishing touches
  • 📝 Generate docstrings

📜 Recent 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 003cf62 and 6c8aa36.

📒 Files selected for processing (1)
  • tests/main/openapi/test_main_openapi.py
🧰 Additional context used
🧬 Code graph analysis (1)
tests/main/openapi/test_main_openapi.py (2)
tests/main/conftest.py (2)
  • output_file (99-101)
  • run_main_and_assert (245-409)
src/datamodel_code_generator/__main__.py (1)
  • Exit (130-136)
⏰ 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). (10)
  • GitHub Check: 3.10 on Windows
  • GitHub Check: 3.14 on Windows
  • GitHub Check: 3.11 on Windows
  • GitHub Check: 3.10 on macOS
  • GitHub Check: 3.11 on macOS
  • GitHub Check: 3.12 on Windows
  • GitHub Check: 3.13 on Windows
  • GitHub Check: 3.13 on macOS
  • GitHub Check: Analyze (python)
  • GitHub Check: benchmarks
🔇 Additional comments (1)
tests/main/openapi/test_main_openapi.py (1)

4944-4966: Solid regression test for #2953: validates a clear CLI error instead of IndexError.

Optional (only if the intended contract is “no file on failure”): add file_should_not_exist=output_file (or an equivalent assertion) to prevent partial outputs from being left behind after the error.


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

github-actions Bot commented Jan 10, 2026

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

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Jan 10, 2026

CodSpeed Performance Report

Merging this PR will not alter performance

Comparing fix/issue-2953-indexerror-reuse-scope-tree (6c8aa36) with main (fa1fc11)

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

Summary

✅ 11 untouched benchmarks
⏩ 98 skipped benchmarks1

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.

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: 1

🤖 Fix all issues with AI agents
In @tests/main/openapi/test_main_openapi.py:
- Around line 4944-4965: The test function
test_main_openapi_reuse_scope_tree_single_file_error uses a Pydantic v2-specific
model flag but is missing the @SKIP_PYDANTIC_V1 decorator; add the
@SKIP_PYDANTIC_V1 decorator immediately above the test function definition to
skip this test in Pydantic v1 environments so it only runs when Pydantic v2 is
available (no other changes to test body or args).
📜 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 fa1fc11 and 003cf62.

⛔ Files ignored due to path filters (1)
  • tests/data/openapi/issue_2953.yaml is excluded by !tests/data/**/*.yaml and included by none
📒 Files selected for processing (2)
  • src/datamodel_code_generator/parser/base.py
  • tests/main/openapi/test_main_openapi.py
⏰ 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). (11)
  • GitHub Check: 3.10 on Ubuntu
  • GitHub Check: 3.14 on Windows
  • GitHub Check: 3.10 on macOS
  • GitHub Check: 3.10 on Windows
  • GitHub Check: 3.12 on Windows
  • GitHub Check: 3.11 on Windows
  • GitHub Check: 3.11 on macOS
  • GitHub Check: 3.13 on Windows
  • GitHub Check: 3.12 on macOS
  • GitHub Check: benchmarks
  • GitHub Check: Analyze (python)
🔇 Additional comments (1)
src/datamodel_code_generator/parser/base.py (1)

1755-1755: LGTM! Correctly prevents IndexError for empty module tuples.

The added if module filter prevents an IndexError when module is an empty tuple (). Since empty modules have no name, they cannot conflict with shared_module_name, so filtering them out is both safe and semantically correct. This fix resolves issue #2953 where using --reuse-scope tree with single-file output could trigger this error.

Comment thread tests/main/openapi/test_main_openapi.py
@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (fa1fc11) to head (6c8aa36).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #2954   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           94        94           
  Lines        17723     17726    +3     
  Branches      2037      2037           
=========================================
+ Hits         17723     17726    +3     
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 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.

@koxudaxi koxudaxi merged commit 98f3a48 into main Jan 10, 2026
38 checks passed
@koxudaxi koxudaxi deleted the fix/issue-2953-indexerror-reuse-scope-tree branch January 10, 2026 07:23
@github-actions
Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR is a pure bug fix. The change adds a filter if module to skip empty tuples when building the set of existing module names. Previously, using --reuse-scope=tree with single file output would crash with an IndexError when trying to access module[0] on an empty tuple. After the fix, empty modules are skipped, allowing the existing validation to properly raise a user-friendly error message ("Modular references require an output directory, not a file"). This is not a breaking change because: (1) the previous behavior was an unintentional crash, not intended functionality; (2) no valid use case is affected - the invalid configuration that was crashing now produces a helpful error instead; (3) no generated code, CLI options, defaults, or APIs were changed.


This analysis was performed by Claude Code Action

@github-actions
Copy link
Copy Markdown
Contributor

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

Generation fails with IndexError: tuple out of range

1 participant