Skip to content

Fix incorrect relative imports with --use-exact-imports and --collapse-root-models#3020

Merged
koxudaxi merged 1 commit intomainfrom
fix/exact-imports-collapse-root-models-title
Mar 3, 2026
Merged

Fix incorrect relative imports with --use-exact-imports and --collapse-root-models#3020
koxudaxi merged 1 commit intomainfrom
fix/exact-imports-collapse-root-models-title

Conversation

@koxudaxi
Copy link
Copy Markdown
Owner

@koxudaxi koxudaxi commented Mar 3, 2026

Fixes: #3001

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Fixed import path resolution when using exact imports. The full import path is now correctly updated during alias resolution, preventing outdated path references.
  • Tests

    • Added test coverage for array field titles with exact imports and root model collapsing functionality.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 3, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 69eda18 and b213e93.

⛔ Files ignored due to path filters (2)
  • tests/data/jsonschema/exact_imports_collapse_root_models_title/data/ReflectionSetData.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/jsonschema/exact_imports_collapse_root_models_title/datatypes/FooStats.json is excluded by !tests/data/**/*.json and included by none
📒 Files selected for processing (7)
  • src/datamodel_code_generator/parser/base.py
  • tests/data/expected/main/jsonschema/exact_imports_collapse_root_models_title/__init__.py
  • tests/data/expected/main/jsonschema/exact_imports_collapse_root_models_title/data/ReflectionSetData.py
  • tests/data/expected/main/jsonschema/exact_imports_collapse_root_models_title/data/__init__.py
  • tests/data/expected/main/jsonschema/exact_imports_collapse_root_models_title/datatypes/FooStats.py
  • tests/data/expected/main/jsonschema/exact_imports_collapse_root_models_title/datatypes/__init__.py
  • tests/main/jsonschema/test_main_jsonschema.py

📝 Walkthrough

Walkthrough

This PR fixes an import path resolution bug in the datamodel code generator. When processing data types with exact import resolution enabled, the full_path variable now correctly reflects the exact import path, ensuring consistent alias resolution throughout the generated code.

Changes

Cohort / File(s) Summary
Core Fix
src/datamodel_code_generator/parser/base.py
Updated full_path assignment to use the result of exact_import when processing non-base-class data types with exact import resolution, fixing incorrect relative imports with --use-exact-imports and --collapse-root-models flags.
Expected Test Output
tests/data/expected/main/jsonschema/exact_imports_collapse_root_models_title/*
Added complete test fixture directory with generated Python package structure containing Pydantic models ReflectionSetData and FooStats with proper relative imports for exact import resolution scenarios.
Test Function
tests/main/jsonschema/test_main_jsonschema.py
Added new test test_main_exact_imports_collapse_root_models_title_array that validates exact imports and root-model collapsing work correctly when array fields have titles in Pydantic v2.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested labels

breaking-change-analyzed

Poem

🐰 With paths exact and imports true,
The FooStats alias shines anew!
One line corrects the winding way,
No more confusion—hip hooray!
Our collapsed models hop with glee,
Now properly imported, can't you see? ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% 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 PR title clearly describes the main change: fixing incorrect relative imports when both --use-exact-imports and --collapse-root-models are used together.
Linked Issues check ✅ Passed The PR implementation fixes the core issue: updating full_path in exact_import resolution ensures consistent alias usage across all type references.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the exact-imports/collapse-root-models issue: core fix in base.py and test additions confirming the fix.

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

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/exact-imports-collapse-root-models-title

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 Mar 3, 2026

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Mar 3, 2026

Merging this PR will not alter performance

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

✅ 11 untouched benchmarks
⏩ 98 skipped benchmarks1


Comparing fix/exact-imports-collapse-root-models-title (b213e93) with main (69eda18)

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.

@koxudaxi koxudaxi merged commit 991f0ff into main Mar 3, 2026
37 checks passed
@koxudaxi koxudaxi deleted the fix/exact-imports-collapse-root-models-title branch March 3, 2026 23:09
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 3, 2026

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR is a bug fix that corrects incorrect relative import path generation when using --use-exact-imports with --collapse-root-models. The previous behavior generated incorrect imports that would cause runtime import errors. While the generated code output changes, it changes from broken/incorrect to correct/working - this is a corrective fix rather than a breaking change. No CLI options, Python API, templates, or default behaviors were modified.


This analysis was performed by Claude Code Action

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 3, 2026

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #3020   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           94        94           
  Lines        18179     18182    +3     
  Branches      2108      2108           
=========================================
+ Hits         18179     18182    +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.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 4, 2026

🎉 Released in 0.54.1

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.

incorrect relative imports with --use-exact-imports and --collapse-root-models - bis

1 participant