You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Import a mapped class only in the modules that use it - #6796
Corrected generated imports for externally mapped types and custom Python imports. Shared imports are retained where needed, but written only in modules that use the referenced type, reducing unnecessary imports and avoiding conflicts with locally defined models.
Documentation
Clarified how shared imports are handled across generated modules.
No actionable comments were generated in the recent review.
Walkthrough
The import plan now distinguishes shared import names from imports rendered in each module. Module and empty package-initializer generation receive shared import text. Tests cover external reference mappings across generated modules.
The import plan identifies external-ref-mapping and x-python-import entries as shared imports. Documentation states that a module writes an import when its model names the class.
Pass shared imports to output generation src/datamodel_code_generator/parser/_passes/imports.py, src/datamodel_code_generator/parser/base.py, src/datamodel_code_generator/_target_binding.py
Module rendering passes shared import text to module and empty package-initializer generation. The base and target-binding methods accept and forward that text.
Mapped class imports are now written only in modules that use them, as issue #6787 requests, and no actionable merge-blocking risk remains.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title states the main change: a mapped class is now imported only in the modules that use it. The raw summary shows the shared-import handling in the parser and the tests that check the mapped imp…
Linked Issues check
[#6787] Objective: with --external-ref-mapping and directory input, only the module that uses the mapped $ref class (out/order.py, Customer) imports it. Other modules (out/product.py) import only pyda…
Out of Scope Changes check
Changes in the current PR (base 362cc90 to head f72e7fa) relate to the #6787 fix: - Core changes in parser/base.py, parser/_imports_plan.py, parser/_passes/imports.py, and _target_binding.py pass shar…
Full details: Title check
Explanation
The title states the main change: a mapped class is now imported only in the modules that use it. The raw summary shows the shared-import handling in the parser and the tests that check the mapped import is written only where its model uses the type. This matches the linked issue #6787, which asks that only the module using the mapped class import it.
Full details: Linked Issues check
Explanation
[#6787] Objective: with --external-ref-mapping and directory input, only the module that uses the mapped $ref class (out/order.py, Customer) imports it. Other modules (out/product.py) import only pydantic.BaseModel.
Evidence from the PR summary and attested file summaries:
src/datamodel_code_generator/parser/_imports_plan.py: the module docs and _without_unrendered now treat Added entries from the external-ref-mapping and x-python-import shared-import passes as rendering no imports. The name is kept in shared imports, but only the module whose model names the type writes the import.
src/datamodel_code_generator/parser/base.py: _generate_module_output and _generate_empty_init_exports take shared import text instead of rendering self.imports directly.
src/datamodel_code_generator/parser/_passes/imports.py: render_modules renders shared imports once and passes them to module-output and empty-init generation, including the --treat-dot-as-module path.
Expected snapshot tests/data/expected/main/jsonschema/main_jsonschema/external_ref_mapping_modules/product.py is added with a Product model and no mapped import. order.py imports the mapped types.
tests/main/jsonschema/test_main_jsonschema.py adds a test for external-ref-mapping imports and updates the shared-import collision test description.
Stack note: PR #6793 is uncertain and has no current review evidence. PR #6794 is reviewed and only changes .github/workflows/release-draft.yaml, which does not touch the import path. The current PR diff, taken from base 362cc90, contains the code changes that implement this objective. The objective is therefore Addressed by PR #6796 alone. Limit: the source was not read directly. The conclusion rests on the attested summaries and the expected-output fixtures.
Full details: Out of Scope Changes check
Explanation
Changes in the current PR (base 362cc90 to head f72e7fa) relate to the #6787 fix:
Core changes in parser/base.py, parser/_imports_plan.py, parser/_passes/imports.py, and _target_binding.py pass shared import text through module and empty-init generation. This is the mechanism for the fix.
docs/architecture.md documents the shared-import behavior for external-ref-mapping and x-python-import. This is documentation of the changed behavior.
Expected output changes in shared_import_named_like_module_and_model (removed collections.OrderedDict import, unused because the local OrderedDict model shadows it) follow from the same rule: shared imports render only in modules that use them.
New fixture expected outputs (external_ref_mapping_modules/init.py, order.py, product.py) and the test additions in test_main_jsonschema.py and test_main_jsonschema.refs support the new test case.
test_payload_validation.refs adds the new product.json fixture to payload validation cases. This is test coverage for the new fixture. It is a minor, supporting addition and does not change product behavior.
No change was found that is unrelated to #6787. Ignored test input files were not inspected. Their exclusion does not change this result.
Finishing Touches
Generate unit tests (beta)
Commit to this branch
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
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.
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
⚠️26 benchmarks with a significant change ran on different hardware
Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.
Reasoning: The PR changes how shared imports are written. The Python type that an --external-ref-mapping ref or an x-python-import names still keeps its name in every module, so model naming does not change. But only a module whose model actually names the type now writes the import. Before, every module of a multi-module output wrote it, including modules that never use it. That made unrelated modules fail with ModuleNotFoundError when the external package was missing. The behaviour diff gate marks the new external_ref_mapping_modules input as a crash-fix. The x-python-import cells are marked no-behaviour or both-fail. The only accepted cells concern regex patterns that hold single quotes. This diff touches no pattern code, so those cells come from other changes since 0.95.0. In the expected outputs, the only changes are unused import lines removed from modules that do not reference the mapped class, and binding-test line offsets shifted by one because the import block layout changed. No generated model names, types or fields change. The internal signatures of _generate_module_output and _generate_empty_init_exports gained a shared_imports_str parameter, but these are private methods, not public API. No CLI options, defaults, template variables, Python version support or error reporting change. This is a bug fix and not a breaking change.
This analysis was produced by the release draft workflow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes: #6787
Summary by CodeRabbit