Skip to content

Fix all-export generation for hyphenated directories#3033

Merged
koxudaxi merged 2 commits intokoxudaxi:mainfrom
an5t:fix-all-exports-hyphenated
Mar 9, 2026
Merged

Fix all-export generation for hyphenated directories#3033
koxudaxi merged 2 commits intokoxudaxi:mainfrom
an5t:fix-all-exports-hyphenated

Conversation

@an5t
Copy link
Copy Markdown
Contributor

@an5t an5t commented Mar 7, 2026

Fixes a bug where --all-exports-scope fails to generate __all__ imports for packages originating from schema directories with hyphens in the path.

Summary by CodeRabbit

  • New Features

    • Generates a package initializer and exposes an Order model (Pydantic) via the package public API.
  • Bug Fixes

    • Improved handling of hyphenated package/directory names by normalizing module identifiers for correct export collection.
  • Tests

    • Added test coverage validating hyphenated directory handling in JSON Schema code generation.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 7, 2026

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: a92d43bb-4c96-4c47-9f7d-473bd647e5de

📥 Commits

Reviewing files that changed from the base of the PR and between 868c3eb and 8d33a4a.

⛔ Files ignored due to path filters (1)
  • tests/data/jsonschema/all_exports_hyphenated_directory/hyphenated-directory/order.json is excluded by !tests/data/**/*.json and included by none
📒 Files selected for processing (4)
  • src/datamodel_code_generator/parser/base.py
  • tests/data/expected/main/jsonschema/all_exports_hyphenated_directory/hyphenated_directory/__init__.py
  • tests/data/expected/main/jsonschema/all_exports_hyphenated_directory/hyphenated_directory/order.py
  • tests/main/test_main_general.py
🚧 Files skipped from review as they are similar to previous changes (3)
  • src/datamodel_code_generator/parser/base.py
  • tests/data/expected/main/jsonschema/all_exports_hyphenated_directory/hyphenated_directory/init.py
  • tests/data/expected/main/jsonschema/all_exports_hyphenated_directory/hyphenated_directory/order.py

📝 Walkthrough

Walkthrough

Normalizes module identifiers by replacing hyphens with underscores in the parser's export-collection logic; updates tests and expected fixtures to cover hyphenated directory names and package re-exports.

Changes

Cohort / File(s) Summary
Parser change
src/datamodel_code_generator/parser/base.py
Adds hyphen-to-underscore normalization for the current module and processed modules inside _collect_exports_for_init(), using normalized identifiers for equality, prefix, and depth computations when collecting exports.
Generated expected fixtures
tests/data/expected/main/jsonschema/all_exports_hyphenated_directory/hyphenated_directory/__init__.py, tests/data/expected/main/jsonschema/all_exports_hyphenated_directory/hyphenated_directory/order.py
Adds expected package __init__.py (re-exporting Order) and order.py (Pydantic Order model) for hyphenated-directory test scenario.
Tests
tests/main/test_main_general.py
Adds test function test_all_exports_scope_children_jsonschema_hyphenated_package() (appears twice in the diff) to validate --all-exports-scope=children behavior with hyphenated directory names.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I hop where dashes once did roam,
I nudge them gently into underscores' home,
Exports align, names neatly dressed,
Hyphenated paths now pass the test! 🎉

🚥 Pre-merge checks | ✅ 3
✅ 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 'Fix all-export generation for hyphenated directories' directly and clearly describes the main change: fixing a bug in all-export generation when directories contain hyphens.
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 unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

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

2595-2604: Keep the self-module check in normalized space.

Line 2601 compares normalized_proc_module against the raw module tuple. It still works here because the later depth < 1 check filters the self-module case, but hyphenated init packages end up relying on that side effect. Comparing against normalized_module keeps this helper consistent end-to-end.

♻️ Small cleanup
-            if not proc_models or normalized_proc_module == module:
+            if not proc_models or normalized_proc_module == normalized_module:
                 continue
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/datamodel_code_generator/parser/base.py` around lines 2595 - 2604, The
loop in the parser compares normalized_proc_module to the raw module tuple (`if
not proc_models or normalized_proc_module == module:`) which mixes normalized
and raw forms; change that comparison to use normalized_module (i.e.,
`normalized_proc_module == normalized_module`) so the self-module check is
performed in normalized space alongside the earlier normalization steps for
`module`, ensuring consistent behavior for hyphenated package names; update the
condition in the for-loop that uses normalized_proc_module, module, and
proc_models to reference normalized_module instead of module.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/datamodel_code_generator/parser/base.py`:
- Around line 2595-2604: The loop in the parser compares normalized_proc_module
to the raw module tuple (`if not proc_models or normalized_proc_module ==
module:`) which mixes normalized and raw forms; change that comparison to use
normalized_module (i.e., `normalized_proc_module == normalized_module`) so the
self-module check is performed in normalized space alongside the earlier
normalization steps for `module`, ensuring consistent behavior for hyphenated
package names; update the condition in the for-loop that uses
normalized_proc_module, module, and proc_models to reference normalized_module
instead of module.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 05d0f8a3-e09d-47e8-9826-995921b48df9

📥 Commits

Reviewing files that changed from the base of the PR and between 01c2f41 and 868c3eb.

⛔ Files ignored due to path filters (1)
  • tests/data/jsonschema/all_exports_hyphenated_directory/hyphenated-directory/order.json is excluded by !tests/data/**/*.json and included by none
📒 Files selected for processing (4)
  • src/datamodel_code_generator/parser/base.py
  • tests/data/expected/main/jsonschema/all_exports_hyphenated_directory/hyphenated_directory/__init__.py
  • tests/data/expected/main/jsonschema/all_exports_hyphenated_directory/hyphenated_directory/order.py
  • tests/main/test_main_general.py

Fixes a bug where --all-exports-scope fails to generate __all__ imports
for packages originating from schema directories with hyphens in the
path.
@an5t an5t force-pushed the fix-all-exports-hyphenated branch from 868c3eb to 8d33a4a Compare March 7, 2026 23:25
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Mar 9, 2026

Merging this PR will improve performance by 14.02%

⚠️ 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_kubernetes_style_pydantic_v2 2.6 s 2.4 s +11.36%
WallTime test_perf_stripe_style_pydantic_v2 2 s 1.8 s +10.59%
WallTime test_perf_openapi_large 3.1 s 2.7 s +14.01%
WallTime test_perf_deep_nested 6.2 s 5.5 s +12.28%
WallTime test_perf_all_options_enabled 6.9 s 6.1 s +12.99%
WallTime test_perf_aws_style_openapi_pydantic_v2 1.9 s 1.7 s +11.57%
WallTime test_perf_graphql_style_pydantic_v2 822.6 ms 734.9 ms +11.92%
WallTime test_perf_duplicate_names 1,069.4 ms 937.9 ms +14.02%
WallTime test_perf_complex_refs 2.2 s 1.9 s +12.51%
WallTime test_perf_large_models_pydantic_v2 3.7 s 3.3 s +12.71%
WallTime test_perf_multiple_files_input 3.7 s 3.3 s +13.22%

Comparing an5t:fix-all-exports-hyphenated (04e7236) with main (3c244da)

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
Copy link
Copy Markdown

codecov Bot commented Mar 9, 2026

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #3033   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           85        85           
  Lines        17684     17688    +4     
  Branches      2068      2068           
=========================================
+ Hits         17684     17688    +4     
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 8c4bfd1 into koxudaxi:main Mar 9, 2026
38 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 9, 2026

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR is a pure bug fix. It corrects the handling of hyphenated directory names when using --all-exports-scope by normalizing paths (replacing hyphens with underscores) before comparing them. Previously, the feature was silently failing to generate all imports for packages in hyphenated directories. The fix makes the feature work correctly without changing any existing working behavior. No API/CLI changes, no template changes, no default behavior changes - just fixing broken functionality.


This analysis was performed by Claude Code Action

@github-actions
Copy link
Copy Markdown
Contributor

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

2 participants