Skip to content

Compute the C3 order of models, raw schemas, the inheritance check and the target bindings in one place - #5602

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

Koudai Aono (koxudaxi) merged 1 commit into
impl-importsfrom
impl-mro

Conversation

@koxudaxi

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

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of complex model inheritance, including cyclic relationships and conflicting method-resolution orders.
    • Updated inheritance validation to follow Python-style ordering rules.
  • Documentation
    • Clarified how inheritance ordering and cycle handling work.

@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Walkthrough

The change adds shared C3 ordering utilities in _mro.py and routes model, target-binding, and parser inheritance processing through them. The utilities support first-head and python rules for handling cycles and merge conflicts.

Changes: Shared MRO ordering

Layer / File(s) Summary
Shared C3 ordering utilities
src/datamodel_code_generator/_mro.py
Adds descendant-first sorting, C3 merging, and iterative linearization. The first-head rule selects the first pending head when no head is free. The python rule returns None for cycles or merge conflicts.
Model and binding integration
src/datamodel_code_generator/model/base.py, src/datamodel_code_generator/_target_binding.py
Model ordering and target binding use the shared sorting and linearization utilities. Functional TypedDicts continue to use parent_first.
Parser integration and MRO records
src/datamodel_code_generator/parser/base.py, src/datamodel_code_generator/parser/jsonschema.py, docs/architecture.md, scripts/decision_homes.toml
Parser inheritance validation uses C3 merging with the python rule. JSON Schema ordering uses the shared sorting and linearization utilities. The architecture documentation and decision records identify the shared MRO implementation and its callers.

Priority: Low

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

Change: Refactor

Merge Risk: Minimal · up to c0d1b

The shared MRO refactor has no established merge-blocking issue and is mergeable subject to normal checks.

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 names the main change: C3 ordering of models, raw schemas, the inheritance check, and target bindings is consolidated in one place. The raw summary confirms that _mro.py is added, and that…
Docstring Coverage Passed Docstring coverage is 85.71% 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. Documentati…
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 names the main change: C3 ordering of models, raw schemas, the inheritance check, and target bindings is consolidated in one place. The raw summary confirms that _mro.py is added, and that base.py, _target_binding.py, parser/base.py, and parser/jsonschema.py now delegate to it. The title is long at 103 characters, but it stays on topic and matches the PR objective.

Full details: Docstring Coverage

Explanation

Docstring coverage is 85.71% 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 7 functions across 4 files. (3 skipped: 2 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.

❤️ 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-29T22:12:52.381593Z c0d1bea 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

@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!

@codspeed

codspeed Bot commented Oct 29, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 52.31%

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 regressed benchmarks
⏩ 98 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_perf_client_calls[sync] 17.5 ms 43 ms -59.23%
❌ test_perf_client_calls[async] 19.4 ms 47.1 ms -58.79%
❌ test_perf_all_options_enabled 2.8 s 6.3 s -56.22%
❌ test_perf_extreme_large_schema 7.6 s 17.2 s -56.14%
❌ test_perf_openapi_large 1.5 s 3.5 s -56.04%
❌ test_perf_duplicate_names 492.8 ms 1,118.5 ms -55.94%
❌ test_perf_complex_refs 980.7 ms 2,206.8 ms -55.56%
❌ test_perf_scalar_root_collapsed_builtin 95.4 ms 214.4 ms -55.52%
❌ test_perf_kubernetes_style_pydantic_v2 1.3 s 2.9 s -55.28%
❌ test_perf_multiple_files_input 1.8 s 4 s -55.09%
❌ test_perf_load_pyproject_config[mapping-None] 168.5 µs 374.3 µs -54.97%
❌ test_perf_simple_msgspec_unset_field_rendering 242.1 ms 537.4 ms -54.96%
❌ test_perf_large_models_dataclass_builtin 655.1 ms 1,450.5 ms -54.84%
❌ test_perf_load_pyproject_config[config-None] 167.6 µs 371 µs -54.83%
❌ test_perf_deep_nested 2.6 s 5.8 s -54.78%
❌ test_perf_load_pyproject_config[mapping-multiple] 174.9 µs 386.7 µs -54.78%
❌ test_perf_large_models_typed_dict[total-false-requiredness] 541.9 ms 1,191.2 ms -54.51%
❌ test_perf_load_pyproject_config[config-multiple] 174.1 µs 382 µs -54.42%
❌ test_perf_openapi_large_pydantic_v2_builtin 620.4 ms 1,356 ms -54.25%
❌ test_perf_large_models_pydantic_v2 1.8 s 3.9 s -54.21%
... ... ... ... ...

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

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing impl-mro (c0d1bea) with impl-imports (a33c919)

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

@koxudaxi
Koudai Aono (koxudaxi) merged commit 27b3752 into main Oct 29, 2026
163 of 164 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the impl-mro 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 that moves the C3 method resolution order logic into a new private module _mro.py and points the existing callers at it. The new code gives the same results as before: linearize_data_models and the JSON Schema raw-schema linearization keep the first-head order, including the cycle-cutting walk; the target bindings use the python rule, which returns None on a cycle or conflict as the removed private _linearize and _c3 did; and the inheritance check in parser/base.py now asks the merge for the python rule and raises the same error message in exactly the cases where the old first-head merge broke a sequence order or met a duplicate. The public c3_merge name stays in model/base.py as an alias whose only new parameter is an optional keyword rule that defaults to the old behaviour. The only removed functions, _sort_for_mro, _linearize_depth_first, _linearize and _c3, are private. The other changes are internal architecture docs and repo tooling config. The behaviour diff gate compares with the last release, and its accepted cells all concern allOf schemas with scalar parts becoming RootModel or alias types. Its notes point to a different PR, and this diff does not touch allOf handling, so those changes do not come from this PR. The crash-fix cell is unrelated, and the both-fail cells do not change behaviour. No changes to code generation, templates, CLI or API, defaults, Python versions or error handling come from this PR.


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