Skip to content

Find discriminated union sites with one iterator and keep the tag passes' run state in one tag plan - #5454

Merged
Koudai Aono (koxudaxi) merged 5 commits into
dry-a4from
dry-a12a
Oct 29, 2026
Merged

Koudai Aono (koxudaxi) merged 5 commits into
dry-a4from
dry-a12a

Conversation

@koxudaxi

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

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Refactor
    • Tagged-union generation now coordinates discriminator tags, reused variants, deferred unions, and field ordering through a shared planning process.
    • Possible tag-field names are considered across union variants and inherited models when resolving conflicting layouts.
  • Documentation
    • Architecture documentation now describes how discriminator-tag planning relates to model generation and layout.

@coderabbitai

coderabbitai Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Important

Review skipped

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.

Run configuration

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

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 788ee6f and afdb065.

Files ignored due to path filters: 1
  • docs/llms-full.txt is excluded by none and included by none
Files selected for processing: 13
  • docs/architecture.md
  • scripts/check_architecture_boundaries.py
  • src/datamodel_code_generator/parser/_facts.py
  • src/datamodel_code_generator/parser/_passes/_shared.py
  • src/datamodel_code_generator/parser/_passes/_state.py
  • src/datamodel_code_generator/parser/_passes/_tables.py
  • src/datamodel_code_generator/parser/_passes/cross_module.py
  • src/datamodel_code_generator/parser/_passes/imports.py
  • src/datamodel_code_generator/parser/_passes/modules.py
  • src/datamodel_code_generator/parser/_passes/plans.py
  • src/datamodel_code_generator/parser/_passes/structure.py
  • src/datamodel_code_generator/parser/_tag_plan.py
  • src/datamodel_code_generator/parser/base.py

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

Walkthrough

The parser now stores discriminator-tag planning state in a shared TagPlan. Discriminator and cross-module passes use that plan for union-site discovery, tag recording, reuse snapshots, and related state. Layout planning and architecture references use the shared tag-plan helpers.

Changes: Discriminator tag planning

Layer / File(s) Summary
TagPlan state and reuse snapshot
src/datamodel_code_generator/parser/_tag_plan.py, src/datamodel_code_generator/parser/base.py, src/datamodel_code_generator/parser/_passes/structure.py, src/datamodel_code_generator/parser/_passes/plans.py, src/datamodel_code_generator/parser/_facts.py, src/datamodel_code_generator/parser/_passes/_shared.py
Adds shared union-site and tag-plan data. Parser initializes the plan, and structure processing records declared union tags in its reuse snapshot.
Discriminator pass processing
src/datamodel_code_generator/parser/_passes/modules.py
Discriminator passes discover union sites and use TagPlan for inline variants, discriminator tags, collapsed variants, reused fields, and deferred unions.
Cross-module and finish-pass consumers
src/datamodel_code_generator/parser/_passes/cross_module.py, src/datamodel_code_generator/parser/_passes/imports.py, src/datamodel_code_generator/parser/_passes/_state.py, src/datamodel_code_generator/parser/_passes/_tables.py
Cross-module and finish passes read opted-out models, tag warnings, and reused tag fields from TagPlan rather than run-level or parser-owned state.
Layout planning and architecture references
src/datamodel_code_generator/parser/_passes/plans.py, docs/architecture.md, scripts/check_architecture_boundaries.py
Layout flattening derives possible tag names from union sites. Architecture documentation and the boundary check reference and permit the tag-plan module.

Priority: Low

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Refactor

Possibly related issues

  • #5421 — The pull request updates discriminator-tag architecture documentation, which is the subject of this issue.
  • #5361 — The pull request centralizes shared-union tag records in TagPlan; the issue concerns tag assignment for variants used by multiple unions.

Possibly related PRs

Merge Risk: Minimal · previous review afdb0

The shared tag-plan refactor has no identified merge-blocking regression 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 clearly summarizes the two main changes: shared discriminated-union site discovery and centralized tag-pass state in a TagPlan.
Docstring Coverage Passed Docstring coverage is 95.65% 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: Docstring Coverage

Explanation

Docstring coverage is 95.65% 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 23 functions across 10 files. (1 skipped: 1 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

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 🔄 Running since 2026-10-28T20:38:23.027731Z afdb065 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 28, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

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

✅ 52 untouched benchmarks
⏩ 98 skipped benchmarks1


Comparing dry-a12a (ce050c7) with dry-a4 (788ee6f)2

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

  2. No successful run was found on dry-a4 (f8b75a6) during the generation of this report, so 7d38856 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@codecov

codecov Bot commented Oct 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

# Conflicts:
#	docs/architecture.md
#	docs/llms-full.txt
#	src/datamodel_code_generator/parser/_facts.py
#	src/datamodel_code_generator/parser/_passes/_tables.py
#	src/datamodel_code_generator/parser/_passes/imports.py
#	src/datamodel_code_generator/parser/_passes/modules.py
#	src/datamodel_code_generator/parser/_passes/plans.py
# Conflicts:
#	docs/architecture.md
#	docs/llms-full.txt
@koxudaxi
Koudai Aono (koxudaxi) merged commit 63207d6 into main Oct 29, 2026
29 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the dry-a12a branch October 29, 2026 01:57
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR is an internal refactor that gathers the discriminator tag state into a new private module parser/_tag_plan.py with a TagPlan record and the helpers UnionSite, iter_union_sites, declared_union_tags and possible_tag_names. It replaces six private Parser attributes with one _tag_plan attribute and moves the opted_out list off the pipeline run object. It moves find_possible_tag_names, _union_tags, record_reused_variant_tag_field and _VariantDiscriminatorTags from the private _passes modules into the new module with the same logic, and rewrites the discriminator field loops to use a shared site iterator with the same conditions and order. The rest is docs, architecture-check scripts and new legacy row bookkeeping. Every removed symbol is private (underscore-prefixed or in the private _passes package), so no CLI option, public Python API, template or Python version support changes. The variant-tag warning keeps its text and still fires once per variant. The accepted behaviour-diff cells all concern allOf schemas whose parts admit no object, which the gate attributes to an earlier PR; this diff does not touch allOf handling, so they most likely come from earlier merges since the last release. The other cells are both-fail or crash-fix, which do not change default behaviour.


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.

2 participants