Skip to content

Fix exact imports with module/class name collision#2998

Merged
koxudaxi merged 1 commit intomainfrom
fix/exact-imports-module-class-collision
Feb 14, 2026
Merged

Fix exact imports with module/class name collision#2998
koxudaxi merged 1 commit intomainfrom
fix/exact-imports-module-class-collision

Conversation

@koxudaxi
Copy link
Owner

@koxudaxi koxudaxi commented Feb 14, 2026

Fixes: #2984

Summary by CodeRabbit

  • Bug Fixes

    • Fixed import path handling when exact imports are enabled to avoid unintended modification of relative import references.
  • Tests

    • Added test coverage for module/class name collision scenarios with exact-imports enabled, including nested/deep module cases.
  • New

    • Added generated model/response types to support the collision scenarios exercised by the new tests.

@coderabbitai
Copy link

coderabbitai bot commented Feb 14, 2026

📝 Walkthrough

Walkthrough

Adjusted import-path composition in parser: __change_from_import now only composes/short-circuits relative paths when exact imports are disabled, changing the condition to require a truthy from_ and non-exact-import mode before altering full_path. Added expected output files and tests for exact-imports collision cases.

Changes

Cohort / File(s) Summary
Parser logic
src/datamodel_code_generator/parser/base.py
Changed __change_from_import control flow: only modify/short-circuit relative import path composition when use_exact_imports is False and from_ is truthy (adjusted condition involving internal module or module/class collision).
Test expected outputs — shallow collision
tests/data/expected/main/openapi/module_class_name_collision_exact_imports/A.py, tests/data/expected/main/openapi/module_class_name_collision_exact_imports/__init__.py
Added generated model A and package-level AGetResponse expected outputs for exact-imports scenario.
Test expected outputs — deep collision
tests/data/expected/main/openapi/module_class_name_collision_deep_exact_imports/A/B.py, tests/data/expected/main/openapi/module_class_name_collision_deep_exact_imports/A/__init__.py, tests/data/expected/main/openapi/module_class_name_collision_deep_exact_imports/__init__.py
Added nested package A with model B and package-level BGetResponse expected outputs for deep exact-imports scenario.
Tests
tests/main/openapi/test_main_openapi.py
Added two tests (shallow and deep) asserting behavior with --use-exact-imports against Pydantic v2; note: the diff shows duplicated pairs of these test definitions.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

breaking-change-analyzed

Poem

🐰 I hop through code where imports used to roam,
I stitch the paths so every module finds home,
Exact ones stay put, relatives only sway,
Tests and packages now agree and play,
A tidy hop — and then I munch a poem 🥕

🚥 Pre-merge checks | ✅ 6
✅ Passed checks (6 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically summarizes the main change: fixing exact imports when module and class names collide, which directly addresses the primary objective of the PR.
Linked Issues check ✅ Passed The PR directly addresses issue #2984 by fixing the import path composition logic in __change_from_import to prevent incorrect import statements when using exact imports with module/class name collisions.
Out of Scope Changes check ✅ Passed All changes are in scope: the core fix modifies import logic, and test files add expected output and test cases specifically for the exact imports collision scenario.
Docstring Coverage ✅ Passed Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into main

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

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/exact-imports-module-class-collision

No actionable comments were generated in the recent review. 🎉


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
Contributor

github-actions bot commented Feb 14, 2026

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

@codecov
Copy link

codecov bot commented Feb 14, 2026

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #2998   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           94        94           
  Lines        18075     18081    +6     
  Branches      2090      2090           
=========================================
+ Hits         18075     18081    +6     
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 force-pushed the fix/exact-imports-module-class-collision branch from af1dde1 to a8d0b88 Compare February 14, 2026 15:11
@koxudaxi koxudaxi merged commit 907a1a8 into main Feb 14, 2026
36 of 37 checks passed
@koxudaxi koxudaxi deleted the fix/exact-imports-module-class-collision branch February 14, 2026 15:17
@github-actions
Copy link
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR is a bug fix that corrects the behavior of the --use-exact-imports option when there are module/class name collisions. Previously, even with exact imports enabled, the code generator would modify import paths when a module name collided with a class name (e.g., module A containing class A). This was incorrect behavior that contradicted the purpose of --use-exact-imports. The fix adds a check not imports.use_exact to prevent the import path modification when exact imports are requested. This is a correction of broken behavior, not a breaking change - users who were using --use-exact-imports will now get the correct exact import paths they expected.


This analysis was performed by Claude Code Action

@github-actions
Copy link
Contributor

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

Corrupted import statements in generated Pydantic classes

1 participant