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
Rename a model named like an additional import that a type of its module writes by that name - #6041
Fixed generated model naming when a model shares a name with an additional import. Models are now renamed when needed to preserve references to imported types, including types expressed as text.
Improved generated output across dataclass and Pydantic models, including cases where the colliding model is used or unused.
Auto incremental reviews are disabled on this repository.
Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.
You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.
Use the checkbox below for a quick retry:
Trigger review
Walkthrough
The import-name pass now uses model type information to detect collisions between shared imports and generated model names. It renames a colliding model when a type writes the import’s name. Regression tests cover dataclass and Pydantic output, including payload validation.
The pass reads FIELD_TYPES and collects names from unreferenced Python type expressions and string types. When a non-__future__ shared import matches a local model name written by a type, the pass renames the model. Existing local-import checks and the Literal alias exception remain.
Regression coverage for generated output and payloads tests/main/jsonschema/test_main_jsonschema.py, tests/data/expected/main/jsonschema/main_jsonschema/additional_import_named_like_model*, tests/main/jsonschema/test_main_jsonschema.refs, tests/main/test_payload_validation.refs
Tests cover used and unused name collisions for dataclass and Pydantic output. Expected generated code and payload results are added for these cases.
Generated models can receive unnecessary names or shadow another imported type, causing annotations to resolve incorrectly. Correct name collection and reserve shared bindings during renaming before merging.
Pre-merge checks: 5
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title states the primary behavior change: a model is renamed when a type in its module writes the name of an additional import. The raw summary confirms the change in scripts/pass_decls.py and src…
Linked Issues check
For [#6010], change_imported_model_name now reads FIELD_TYPES and renames a local model when a shared, non-__future__ import name is written as text in a model type. The change preserves the imp…
Out of Scope Changes check
The changes stay within [#6010]. The pass-table declarations and architecture documentation describe the new input and behavior. The generated snapshots, fixtures, and regression tests verify the impo…
Docstring Coverage
Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Full details: Title check
Explanation
The title states the primary behavior change: a model is renamed when a type in its module writes the name of an additional import. The raw summary confirms the change in scripts/pass_decls.py and src/datamodel_code_generator/parser/_passes/imports.py, which extend change_imported_model_name to consider shared imports whose names appear as text in model types. The added regression tests for the colliding names support this. The title matches the PR objectives, which describe the same narrowed rename behavior from issue 6010.
Full details: Linked Issues check
Explanation
For [#6010], change_imported_model_name now reads FIELD_TYPES and renames a local model when a shared, non-__future__ import name is written as text in a model type. The change preserves the imported IPv4Address binding for the generated IPv4Address1 model. Regression coverage includes Pydantic v2 and dataclass output, plus used and unused model cases.
Full details: Out of Scope Changes check
Explanation
The changes stay within [#6010]. The pass-table declarations and architecture documentation describe the new input and behavior. The generated snapshots, fixtures, and regression tests verify the import-name collision behavior. No unrelated product behavior is shown.
Full details: Docstring Coverage
Explanation
Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 2 functions across 6 files. (4 skipped: 3 unsupported, 1 too large.)
Finishing Touches
Generate docstrings
Commit to this branch
Create a new PR
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.
Reviewing files that changed from the base of the PR and between 0123521900c366f032be500c477c1c9ff9e66623 and c035ce3904ac563fa92276c8b1c8ce1f1aec9762.
Files ignored due to path filters: 5
docs/llms-full.txt is excluded by none and included by none
tests/data/expected/main/jsonschema/main_jsonschema/additional_import_named_like_model[unused-dataclasses.dataclass].payloads.txt is excluded by !tests/data/**/*.txt and included by none
tests/data/expected/main/jsonschema/main_jsonschema/additional_import_named_like_model[written-dataclasses.dataclass].payloads.txt is excluded by !tests/data/**/*.txt and included by none
tests/data/jsonschema/additional_import_named_like_model.json is excluded by !tests/data/**/*.json and included by none
tests/data/jsonschema/additional_import_named_like_unused_model.json is excluded by !tests/data/**/*.json and included by none
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.
…ule writes by that name
`--additional-imports ipaddress.IPv4Address` is written by every module. A model of that name hid the import from the
types of its module that write the name as text (`x-python-type: IPv4Address`), which then named the model itself.
The model is renamed, as it is for an import of its own module (`change_imported_model_name`); a model whose module
writes no such name keeps its own.
Output changes only where a type of the module writes the name of an additional import that a model of the module
takes.
Fixes: #6010
Hyphenated directory (#6039). A directory is read as importable by its name once its `-` is `_`, as the output writes it
(`written_name`): `café-models` is written to `café_models/` and imported by that name, as the files of 0.88.0 were
written, where the check came before the `-` was replaced and the directory was sanitized as a file's name is
(`caf__models`), so it moved and two directories of the same length (`café-models`, `cafè-models`) shared one package. The
import of such a directory was a syntax error up to 0.88.0 (`from .café-models import a`), so only output that failed to
import changes.
Names an additional import binds (#6041). A type of a module that writes a dotted name writes the first name of it where
the module's names are read (`ipaddress.IPv4Address` binds `ipaddress`), not the last one. The check of the imports every
module writes that a model of the module shadows reads that first name (`iter_python_type_expr_head_names`), so a model
named like the last name of a qualified runtime symbol (`x-python-import` `symbols`) keeps its name, as in 0.88.0. The
name a model or an import takes anew where a shared import binds the name it had skips the names the shared imports bind
(`shared_import_bindings`), so `IPv4Address1` is not given to a model where `mymod.IPv4Address1` is imported by every
module, nor `Status_1` or `list_1` to the import a package names anew.
Enum member defaults (#6040). `set_default_enum_member` writes a member by the expression its type names the enum by when
it runs, so a default kept naming the import that the passes of the imports named anew afterwards (`Status` for
`Status as Status_1`, `list.Pop` for `list_1.Pop`) and the generated module failed to import. The two passes that name an
import anew write the new expression into the members of the defaults that use it (`name_enum_member`, a store mutation
with its row in the fact table).
Item counts (#6037). The item counts that `contains` and boolean `items` imply belong to a field whose schema admits an
array (it declares no type, or an array is one of them) as well as whose type can hold one: a string typed
`datetime.date` and an object with array values (`{"type": "object", "additionalProperties": {"type": "array"}}`) took
`min_length` from them, which raised `Unable to apply constraint` for a date and rejected the maps with fewer keys.
…, so a run without models does not load it (#6043)
* Import the type expression reader of the model renames where they run, so a run without models does not load it
* Fix the unresolved review findings of #6037, #6039, #6040 and #6041
Hyphenated directory (#6039). A directory is read as importable by its name once its `-` is `_`, as the output writes it
(`written_name`): `café-models` is written to `café_models/` and imported by that name, as the files of 0.88.0 were
written, where the check came before the `-` was replaced and the directory was sanitized as a file's name is
(`caf__models`), so it moved and two directories of the same length (`café-models`, `cafè-models`) shared one package. The
import of such a directory was a syntax error up to 0.88.0 (`from .café-models import a`), so only output that failed to
import changes.
Names an additional import binds (#6041). A type of a module that writes a dotted name writes the first name of it where
the module's names are read (`ipaddress.IPv4Address` binds `ipaddress`), not the last one. The check of the imports every
module writes that a model of the module shadows reads that first name (`iter_python_type_expr_head_names`), so a model
named like the last name of a qualified runtime symbol (`x-python-import` `symbols`) keeps its name, as in 0.88.0. The
name a model or an import takes anew where a shared import binds the name it had skips the names the shared imports bind
(`shared_import_bindings`), so `IPv4Address1` is not given to a model where `mymod.IPv4Address1` is imported by every
module, nor `Status_1` or `list_1` to the import a package names anew.
Enum member defaults (#6040). `set_default_enum_member` writes a member by the expression its type names the enum by when
it runs, so a default kept naming the import that the passes of the imports named anew afterwards (`Status` for
`Status as Status_1`, `list.Pop` for `list_1.Pop`) and the generated module failed to import. The two passes that name an
import anew write the new expression into the members of the defaults that use it (`name_enum_member`, a store mutation
with its row in the fact table).
Item counts (#6037). The item counts that `contains` and boolean `items` imply belong to a field whose schema admits an
array (it declares no type, or an array is one of them) as well as whose type can hold one: a string typed
`datetime.date` and an object with array values (`{"type": "object", "additionalProperties": {"type": "array"}}`) took
`min_length` from them, which raised `Unable to apply constraint` for a date and rejected the maps with fewer keys.
* Provide the module the additional imports of the renamed-import test name, so the output imports on Python 3.10
Reasoning: The diff only extends the change_imported_model_name pass so that a model whose class name equals a binding from the opt-in additional-imports option is renamed when a type of its module writes that name as text, for example through x-python-type, and adds docs, pass declaration metadata and tests for it. Previously such output was incorrect because the local class shadowed the import and the field type resolved to the model itself, so a field like host rejected valid IP addresses; now the model becomes IPv4Address1 and the field uses the imported type, while models whose module does not write the name keep their names. This is a bug fix for an opt-in option combination and does not change default output. The accepted cells of the behaviour diff gate concern constraint handling and integer precision, which this diff does not touch, so they are not attributable to this PR. No CLI options, API parameters, templates, Python version support or error handling were removed or changed; the deleted lines are only docstrings, a pass summary, pass read metadata and a coverage pragma.
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: #6010
Summary by CodeRabbit