Skip to content

Import the models that collapsed root types and dict keys name through the reference import, under a named legacy row - #5600

Merged
Koudai Aono (koxudaxi) merged 1 commit into
impl-membersfrom
impl-imports
Oct 29, 2026
Merged

Koudai Aono (koxudaxi) merged 1 commit into
impl-membersfrom
impl-imports

Conversation

@koxudaxi

@koxudaxi Koudai Aono (koxudaxi) commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Bug Fixes
    • Corrected generated import paths for types inlined into a module, including collapsed root types and dictionary keys based on root types. These imports now use the module path without adding an internal-module path or an unnecessary package separator, and preserve bindings when names match the class.
    • Same-module references in these cases no longer generate unnecessary imports.

…h the reference import, under a named legacy row
@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Walkthrough

Reference-import handling now supports a legacy import path for types inlined during root-model collapse and root-derived dictionary-key replacement. The module passes use this path, and the architecture and decision records identify it under H26.

Changes: Legacy import handling

Layer / File(s) Summary
Row-aware reference import behavior
src/datamodel_code_generator/parser/_passes/_shared.py
Reference-import targets, registration, and expression assignment now accept an import-path row. The legacy row preserves its module-path form and existing expressions for same-module targets.
Shared reference import helper
src/datamodel_code_generator/parser/_passes/_shared.py, scripts/generation_store_allowlist.toml
The shared helper resolves references using explicit module context. The previous per-data-type registration helper and its allowlist entry are removed.
Collapse and dictionary-key integration
src/datamodel_code_generator/parser/_passes/modules.py, scripts/decision_homes.toml, docs/architecture.md
Root-model collapse and root-derived dictionary-key replacement use the legacy row. The H26 decision records and architecture table document the row and related import-path helpers.

Priority: Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Possibly related issues

  • Issue 5451: The PR changes the import path for dictionary-key types. The issue reports a failing import for a dictionary-key type alias, though its cycle-resolution fix may differ.
  • Issue 5450: The PR changes the collapse import path for inlined types. The issue reports that root-model collapse imports a module instead of a class.
  • Issue 5595: The PR changes the same import-copy path identified by the issue. The issue’s reported SerializeAsAny effect is not confirmed as fixed by this change.
Merge Risk: Minimal · up to a33c9

The refactor preserves existing import behavior for collapsed root models and dictionary keys. No actionable merge-blocking issue was found; merge after normal checks pass.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title accurately describes the main change: importing models named by collapsed root types and dictionary keys through the reference import under a named legacy row.
Docstring Coverage Passed 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…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
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 8 functions across 2 files. (2 skipped: 2 unsupported.)

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-29T21:43:41.619070Z a33c919 PR opened
ℹ️ About Codex in GitHub

Your 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.

@github-actions

Copy link
Copy Markdown
Contributor

@codspeed

codspeed Bot commented Oct 29, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by ×2.1

2 warnings

⚠️ Walltime results may be unreliable

Some benchmarks used the Walltime instrument outside CodSpeed Macro Runners, which can make the results inconsistent.

Learn more about Macro Runners

See affected benchmarks

⚠️ Base and head ran on different runner types

Some benchmarks with significant performance changes were compared across different runner types, so the changes might not come from the code. Run base and head on the same runner type, or use CodSpeed Macro Runners for more stable results.

Learn more about Macro Runners

Open the report in CodSpeed to investigate

⚡ 52 improved benchmarks
⏩ 98 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_perf_client_calls[sync] 42.2 ms 17.5 ms ×2.4
⚡ test_perf_client_calls[async] 46.6 ms 19.4 ms ×2.4
⚡ test_perf_all_options_enabled 6.5 s 2.8 s ×2.3
⚡ test_perf_large_models_typed_dict[total-false-requiredness] 1,262.7 ms 541.9 ms ×2.3
⚡ test_perf_scalar_root_collapsed_builtin 222.2 ms 95.4 ms ×2.3
⚡ test_perf_duplicate_names 1,141 ms 492.8 ms ×2.3
⚡ test_perf_load_yaml_openapi_large 41 ms 17.7 ms ×2.3
⚡ test_perf_extreme_large_schema 17.4 s 7.6 s ×2.3
⚡ test_perf_inherited_required_fields[required-base-first] 680.1 ms 295.9 ms ×2.3
⚡ test_perf_deep_nested 6 s 2.6 s ×2.3
⚡ test_perf_complex_refs 2,246.9 ms 980.7 ms ×2.3
⚡ test_perf_openapi_large 3.5 s 1.5 s ×2.3
⚡ test_perf_multiple_files_input 4.1 s 1.8 s ×2.3
⚡ test_perf_kubernetes_style_pydantic_v2 2.9 s 1.3 s ×2.3
⚡ test_perf_large_models_dataclass_builtin 1,475.4 ms 655.1 ms ×2.3
⚡ test_perf_large_models_msgspec 2,105.6 ms 942.3 ms ×2.2
⚡ test_perf_simple_msgspec_unset_field_rendering 539.5 ms 242.1 ms ×2.2
⚡ test_perf_large_models_pydantic_v2 4 s 1.8 s ×2.2
⚡ test_perf_graphql_style_pydantic_v2 930.1 ms 419 ms ×2.2
⚡ test_perf_load_pyproject_config[config-multiple] 383.4 µs 174.1 µs ×2.2
... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing impl-imports (a33c919) with impl-members (ef4614d)

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. ↩

@codecov

codecov Bot commented Oct 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@koxudaxi
Koudai Aono (koxudaxi) merged commit 2ca9295 into main Oct 29, 2026
164 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the impl-imports branch October 29, 2026 23:51
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR is an internal refactor of how the collapse-root-models and type-dict-keys-by-root-types passes import models of other modules. The private helpers _relative_import and _register_data_type_import in parser/_passes/_shared.py are removed and replaced by a new import_references function plus an ImportPathRow literal. The new legacy-collapse-import-path row reproduces the old logic. It computes the path with relative and exact_import only when use_exact is set and the target is not an ancestor package. It skips the import and keeps the alias when the model is in the same module. It writes the binding even when it equals the class name. It does not apply the hyphen, _internal or same-named-module rules. The reference row and the default parameters keep existing callers unchanged. The other changes are a treat_dot_as_module protocol property, documentation rows and tooling TOML entries, and no public CLI option, Python API, template or supported Python version changes. The behaviour diff gate does show accepted default-output changes, but its notes tie all of them to the allOf intersection change, where an allOf whose parts admit no object becomes a RootModel or alias. This PR does not touch that code. The only other non-identical cell is a crash-fix, which does not count as a behaviour change. So none of the default-output changes come from this PR's diff, and it has no breaking changes.


This analysis was produced by the release draft workflow

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

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

1 participant