Skip to content

Move the reuse, collapse and discriminator passes into their own modules - #5259

Merged
Koudai Aono (koxudaxi) merged 1 commit into
fix-5252-quoted-optionalfrom
ph3c2-pass-modules
Oct 24, 2026
Merged

Koudai Aono (koxudaxi) merged 1 commit into
fix-5252-quoted-optionalfrom
ph3c2-pass-modules

Conversation

@koxudaxi

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

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Refactor
    • Reorganized model reuse, root-model collapsing, and discriminator handling into shared processing, with no direct change to parsing behavior.
    • Improved handling of discriminator tags, nested model references, imports, and recursion-limit retries.
  • Documentation
    • Updated architecture and contributor guidance to reflect current processing terminology and responsibilities.

@coderabbitai

coderabbitai Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

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 5ab42f9 and 83d004a.

⛔ 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
  • docs/development-contributing.md
  • scripts/generation_store_allowlist.toml
  • src/datamodel_code_generator/model/_decisions/literal_default.py
  • src/datamodel_code_generator/model/_decisions/tag_value.py
  • src/datamodel_code_generator/parser/_imports_plan.py
  • src/datamodel_code_generator/parser/_passes/_shared.py
  • src/datamodel_code_generator/parser/_passes/_warnings.py
  • src/datamodel_code_generator/parser/_passes/modules.py
  • src/datamodel_code_generator/parser/base.py
  • tests/assert_helper_allowlists.py
  • tests/main/test_main_general.py
  • tests/parser/test_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

Reuse, root-model collapse, and discriminator processing move from parser/base.py into parser pass modules. The change adds shared helpers and host protocols, updates parser delegation, and revises tests, the generation-store allowlist, and pass documentation.

Changes: Parser Pass Extraction

Layer / File(s) Summary
Shared pass support
src/datamodel_code_generator/parser/_passes/_shared.py, src/datamodel_code_generator/parser/_passes/_warnings.py, src/datamodel_code_generator/parser/_passes/modules.py, src/datamodel_code_generator/parser/base.py
Shared helpers and warning handling support the extracted passes. Host protocols define the state and operations used by those passes.
Model reuse and root-model collapse
src/datamodel_code_generator/parser/_passes/modules.py, src/datamodel_code_generator/parser/base.py, tests/main/test_main_general.py, tests/parser/test_base.py, docs/architecture.md, docs/development-contributing.md
Module-level passes handle model reuse and root-model collapse. They update references, imports, and schema-rule references. Tests and documentation use the module-level entry points.
Discriminator processing
src/datamodel_code_generator/parser/_passes/_shared.py, src/datamodel_code_generator/parser/_passes/modules.py, src/datamodel_code_generator/parser/base.py, tests/parser/test_base.py, scripts/generation_store_allowlist.toml, src/datamodel_code_generator/model/_decisions/*, src/datamodel_code_generator/parser/_imports_plan.py
The discriminator pass validates variants and updates tag types, defaults, aliases, imports, and tag metadata. Shared helpers resolve discriminator fields and enum values. Related tests, allowlist entries, and documentation use the extracted pass locations.
Parser delegation and supporting updates
src/datamodel_code_generator/parser/base.py, docs/architecture.md, tests/assert_helper_allowlists.py, tests/main/test_main_general.py
Parser pass entries delegate to the extracted functions. Documentation and test imports refer to the updated pass organization.

Priority: ⬇️ Low

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

Change: Refactor

Merge Risk: ⚪ Minimal · up to 83d00

No concrete merge-blocking issue is established for this module extraction. Merge readiness remains 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 and concisely describes the primary change: moving the reuse, collapse, and discriminator passes into separate modules.
Docstring Coverage Passed Docstring coverage is 94.34% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 159 functions across 9 files. (3 skipped: 3…
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 94.34% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 159 functions across 9 files. (3 skipped: 3 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 23, 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-23T20:25:14.191324Z 83d004a 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 23, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 43.5%

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

⚡ 50 improved benchmarks
⏩ 98 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_perf_client_calls[sync] 42.1 ms 23.7 ms +77.39%
⚡ test_perf_client_calls[async] 46.6 ms 26.8 ms +74.11%
⚡ test_perf_load_pyproject_config[config-None] 365.8 µs 227.2 µs +60.98%
⚡ test_perf_load_pyproject_config[config-multiple] 381.6 µs 238.8 µs +59.8%
⚡ test_perf_load_pyproject_config[mapping-None] 370.6 µs 233.2 µs +58.96%
⚡ test_perf_load_pyproject_config[mapping-multiple] 381.6 µs 241.1 µs +58.29%
⚡ test_perf_scalar_root_collapsed_builtin 231 ms 151.4 ms +52.64%
⚡ test_perf_large_models_single_module_stdout 1.8 s 1.2 s +50.95%
⚡ test_perf_large_models_typed_dict[total-false-requiredness] 1,357.4 ms 913.5 ms +48.59%
⚡ test_perf_local_reference_file_resolution 178.2 ms 120.2 ms +48.33%
⚡ test_perf_extreme_large_schema 18.2 s 12.4 s +47.33%
⚡ test_perf_simple_msgspec_unset_field_rendering 167.9 ms 114.3 ms +46.95%
⚡ test_perf_all_options_enabled 7.4 s 5 s +46.59%
⚡ test_perf_ordinary_array_constraint_generation 2.6 s 1.8 s +46.41%
⚡ test_perf_inherited_required_fields[partial-base-first] 1,277.7 ms 873.6 ms +46.25%
⚡ test_perf_openapi_large_pydantic_v2_builtin 1.6 s 1.1 s +46.2%
⚡ test_perf_inherited_required_fields[partial-derived-first] 1,198.2 ms 821.3 ms +45.88%
⚡ test_perf_multiple_files_input 4.3 s 3 s +45.69%
⚡ test_perf_large_models_pydantic_v2 4.2 s 2.9 s +45.59%
⚡ test_perf_inherited_required_fields[required-base-first] 868.3 ms 597.5 ms +45.33%
... ... ... ... ...

ℹ️ 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 ph3c2-pass-modules (83d004a) with fix-5252-quoted-optional (5ab42f9)

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 23, 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 09c24ac into main Oct 24, 2026
132 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the ph3c2-pass-modules branch October 24, 2026 01:42
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR is an internal refactor. The private, name-mangled Parser methods for model reuse, root model collapsing and discriminator tagging move out of parser/base.py and become functions in parser/_passes/modules.py, reached through narrow host protocols (ReuseHost, CollapseHost, DiscriminatorHost). Their private helpers move to parser/_passes/_shared.py. The pipeline tables now call the module functions, and the logic is copied over unchanged, so the generated code is the same. No CLI options, defaults, templates or Python version support change. Public names stay the same: get_most_of_parent is still exported from parser.base as an alias, and Parser._get_enum_discriminator_literal is kept and now delegates to enum_discriminator_literal. The moved warnings go through a new warn_as_parser_base helper. It keeps the UserWarning category, the stacklevel meaning and the parser.base module attribution, so existing warning filters by module still match. The only removed names are underscore-prefixed or name-mangled internals, such as the double-underscore Parser methods and helpers like _get_discriminator_field_value and _get_enum_from_base in parser.base. The tests were updated for these moves, but they are not public API, so nothing here breaks for users.


This analysis was produced by the release draft workflow

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