Skip to content

Break the import cycle between reference and the model base - #7310

Merged
Koudai Aono (koxudaxi) merged 1 commit into
mainfrom
break-reference-model-cycle
Nov 21, 2026
Merged

Koudai Aono (koxudaxi) merged 1 commit into
mainfrom
break-reference-model-cycle

Conversation

@koxudaxi

@koxudaxi Koudai Aono (koxudaxi) commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Bug Fixes
    • Improved reference handling so eligible child models are selected consistently and retain their original order.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-21T17:04:37.896179Z e8db3c1 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.

@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change adds a graph-link helper for selecting data-model children and uses it in Reference. Alias-checking APIs now use private protocols for field and parent-model attributes, with Sequence-based field parameters.

Changes: Reference types

Layer / File(s) Summary
Graph-link child selection
src/datamodel_code_generator/_graph_links.py, src/datamodel_code_generator/reference.py
A new helper yields children without a replace_reference attribute in input order. Reference uses the helper to iterate data-model children.
Alias-checking protocols
src/datamodel_code_generator/reference.py
Alias-checking functions use private field and parent-model protocols. Their field parameters use Sequence instead of list-specific annotations.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Merge Risk: Minimal · up to e8db3

This change breaks an import cycle by using protocol-based typing and a graph-link helper for selecting model children. No concrete behavior regression was identified, so merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Break the import cycle between reference and the model base" matches the stated objective (PR #7310) and the summarized changes. The changes remove the type-checking-only import of model.ba…
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: Title check

Explanation

The title "Break the import cycle between reference and the model base" matches the stated objective (PR #7310) and the summarized changes. The changes remove the type-checking-only import of model.base from reference.py, replace the runtime DataModel type check with the data_model_children helper in _graph_links.py, and switch alias handling to local protocols. This directly addresses the import cycle, so the title names the main change.

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.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between c3c4113 and e8db3c1.

Files ignored due to path filters: 2

  • docs/data/migration/unreleased/reference-field-protocols-api.toml is excluded by none and included by none
  • tests/data/expected/main/public_api_signature_baseline/public_api_record_matches_stored_output.txt is excluded by !tests/data/**/*.txt and included by none
Files selected for processing: 2

  • src/datamodel_code_generator/_graph_links.py
  • src/datamodel_code_generator/reference.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@github-actions

Copy link
Copy Markdown
Contributor

@codspeed

codspeed Bot commented Nov 21, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 48.74%

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

⚠️ 53 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.

Learn more about Macro Runners

See affected benchmarks

⚡ 53 improved benchmarks
⏩ 98 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_perf_simple_msgspec_unset_field_rendering 618 ms 340.9 ms +81.28%
⚡ test_perf_client_calls[sync] 45.2 ms 25.4 ms +77.64%
⚡ test_perf_client_calls[async] 49.8 ms 28.1 ms +77.33%
⚡ test_perf_load_pyproject_config[config-None] 268.6 µs 164 µs +63.78%
⚡ test_perf_load_pyproject_config[mapping-None] 271.3 µs 165.9 µs +63.56%
⚡ test_perf_load_pyproject_config[mapping-multiple] 286.2 µs 175.8 µs +62.79%
⚡ test_perf_load_pyproject_config[config-multiple] 284.9 µs 178 µs +60.03%
⚡ test_perf_large_models_msgspec 2.9 s 1.8 s +58.09%
⚡ test_perf_large_models_dataclass_builtin 1.7 s 1.1 s +58.07%
⚡ test_perf_unique_items_collapsed_builtin 331.7 ms 210.7 ms +57.42%
⚡ test_perf_large_models_typed_dict[total-false-requiredness] 1,382.4 ms 891.5 ms +55.06%
⚡ test_perf_scalar_root_collapsed_builtin 259.4 ms 167.4 ms +54.92%
⚡ test_perf_openapi_large_pydantic_v2_builtin 1.7 s 1.1 s +54.51%
⚡ test_perf_all_options_enabled 6.8 s 4.4 s +54.26%
⚡ test_perf_large_models_typed_dict[legacy-requiredness] 1,398.9 ms 907.2 ms +54.2%
⚡ test_perf_large_models_single_module_stdout 2 s 1.3 s +53.93%
⚡ test_perf_complex_refs 2.4 s 1.5 s +53.02%
⚡ test_perf_unique_items_schema_validators 428.6 ms 281 ms +52.53%
⚡ test_perf_multiple_files_input 4.6 s 3 s +52.24%
⚡ test_perf_inherited_required_fields[partial-derived-first] 1.6 s 1.1 s +51.73%
... ... ... ... ...

ℹ️ 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 break-reference-model-cycle (e8db3c1) with main (c3c4113)

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 Nov 21, 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 9660348 into main Nov 21, 2026
118 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the break-reference-model-cycle branch November 21, 2026 17:22
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR is an internal refactor that stops the reference module from importing the model layer. Six methods change type annotations only: prepare_explicit_field_aliases, explicit_field_alias and validate_explicit_field_aliases on both FieldNameResolver and ModelResolver. Their field parameters used to be annotated as list of DataModelFieldBase or DataModelFieldBase and are now Sequence of _AliasField or _AliasField, where _AliasField is a structural protocol. This widens the accepted types, and the method names, parameters, defaults and runtime behaviour stay the same, so existing callers are unaffected. The project migration note says the same. Reference.iter_data_model_children now yields the children that lack replace_reference instead of doing an isinstance check for DataModel. Children are only ever DataType or DataModel, so it yields the same objects as before. The removed _is_data_model helper was private, and the new data_model_children function is in the private _graph_links module. The behaviour diff gate lists only both-fail cells and no accepted or violation cells, so default generated output does not change. No CLI options, templates, defaults, Python version support or error handling change.


This analysis was produced by the release draft workflow

@github-actions

Copy link
Copy Markdown
Contributor

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