Repository navigation
Import every model and builtin that an annotation names where the output writes it - #5998
Conversation
|
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
|
| Layer / File(s) | Summary |
|---|---|
Import target rules and collapsed-reference call sites docs/architecture.md, src/datamodel_code_generator/parser/_passes/_import_binding.py, src/datamodel_code_generator/parser/_passes/modules.py |
Import target handling now uses written module paths and treats same-module targets consistently. Root-model collapse and dict-key imports use the updated legacy row. |
Generic base-class imports src/datamodel_code_generator/parser/_passes/_import_binding.py, src/datamodel_code_generator/parser/_passes/cross_module.py, tests/checks/module_imports.py, tests/main/openapi/*, tests/data/expected/main/openapi/main_openapi/generic_base_class_other_packages/*, tests/test_yaml_fast_constructor.refs |
Generic base-class imports now use the shared import-target helper and the target module’s initializer state. Tests cover bases in other packages. |
Collapsed references and directory paths tests/main/jsonschema/test_main_jsonschema.py, tests/main/jsonschema/test_main_jsonschema.refs, tests/main/openapi/*, tests/data/expected/main/jsonschema/main_jsonschema/hyphenated_directory_reference/*, tests/data/expected/main/openapi/main_openapi/collapse_root_models_package_init/*, tests/main/test_payload_validation.refs, tests/test_yaml_fast_constructor.refs |
Regression cases cover collapsed-root references from package initializers, module/class name collisions, and references to schemas in hyphenated directories. |
Builtin field-name collisions
| Layer / File(s) | Summary |
|---|---|
Builtin names in generated annotations src/datamodel_code_generator/parser/_passes/imports.py, tests/main/jsonschema/test_main_jsonschema.py, tests/main/jsonschema/test_main_jsonschema.refs, tests/data/expected/main/jsonschema/main_jsonschema/builtin_named_fields*, tests/data/expected/main/xmlschema/builtin_datatypes_matrix.py |
Field-name collision handling now considers builtins recognized for the target Python version. Regression tests and generated outputs cover builtin-named fields across model backends. |
Estimated code review effort: 3 (Moderate) | ~25 minutes
Change: Bug fix · Severity of issue fixed: Medium
Merge Risk: 
Low · previous review 0ee7b
-
Custom templates combined with additional imports can resolve an annotation to the wrong type when builtin aliases collide. This is a bounded compatibility issue; otherwise no concrete merge-blocking risk was established.
Pre-merge checks: 
5
-
Full details: Linked Issues check
-
Explanation
The PR addresses all six active linked issues. The import-binding changes apply class-versus-module collision handling and
__init__relative-depth rules to collapsed-root imports for#5450and#5465.generic_base_class_importderives the base-class import from the actual base module for#5594. The import path logic writes hyphenated directory names through their underscore form for#5709._bind_module_field_namesnow covers all target-version builtins, includingset,tuple,dict, andfrozenset, for#5524and#5826. Added tests and expected outputs cover collapsed imports, package initializers, generic base classes, hyphenated directories, and builtin-name collisions.
Full details: Out of Scope Changes check
-
Explanation
The changed source files implement the linked import-path and builtin-name collision fixes. The architecture documentation updates describe those import-path changes. The added fixtures, expected outputs, and test registrations validate the linked scenarios. No unrelated product behavior is demonstrated.
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 10 functions across 31 files. (7 skipped: 5 unsupported, 2 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.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Merging this PR will degrade performance by 49.28%2 warnings
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
…put writes it A collapsed root type and a dict key typed by a root type import the models they name by the rule of a model's own reference (`reference_import_target`): a package `__init__` reaches a model outside its package one level up, and a model in a module named like it is imported from that module, not as the module. Row `legacy-collapse-import-path` becomes `legacy-collapse-internal-module-import`: only a model of an `_internal` module keeps being named through the imported `_internal` module, as since 0.25.9, which imports. The generated base class is imported by the same rule from the module that writes it, not from the package of the importing module. Every import names its module by the path the output writes it at, a `-` written as `_`. A field named like a builtin class hides the builtin from the annotations and later defaults of its class, so each builtin class the class loads under a field's name is imported under an alias, as `list` was. Only output that failed to import, to evaluate its annotations or to decode changes. Fixes: #5450 Fixes: #5465 Fixes: #5524 Fixes: #5594 Fixes: #5709 Fixes: #5826
0ee7b98 to
e0032e3
Compare
Breaking Change AnalysisResult: No breaking changes detected Reasoning: This PR fixes import bugs in generated code, so every output change replaces code that previously failed to import or evaluate. First, references into hyphenated directories now import from the written package name my_dir instead of my-dir, which used to be a syntax error. Second, collapsed root types inlined into a package init now import with the correct relative dot instead of a broken from . import third. Third, a collapsed array whose class shares its module name now imports the class rather than the module. Fourth, the generated base class from --use-generic-base-class (opt-in) is now imported from its real module in other packages instead of a non-importable from .d import BaseModel. Fifth, the earlier aliasing of a builtin hidden by a same-named field, which only covered list, now also covers set, dict, tuple, str, int and similar names through from builtins import X as X_aliased. Previously those annotations raised a TypeError or failed get_type_hints. The behaviour diff gate agrees: the cells tied to this PR (builtin_named_fields, hyphenated_directory_reference, collapse_root_models_package_init, x_python_type_builtin_dict_collision) are all classed crash-fix or no-behaviour. The only accepted cells concern integer multipleOf and exclusive bounds past 2 to the 53, which this diff does not touch, so they come from other changes since the last release. The removed or renamed symbols (_generic_base_class_module, ReferenceImportTarget.row, the legacy-collapse-import-path literal, which became legacy-collapse-internal-module-import, and the signature of _bind_module_field_names) all live in private parser _passes modules, so no public API changes. No CLI options, defaults, templates, Python version support or error handling changed. This analysis was produced by the release draft workflow |
Fixes: #5450
Fixes: #5465
Fixes: #5524
Fixes: #5594
Fixes: #5709
Fixes: #5826
Summary by CodeRabbit