Skip to content

Read the value type of an enum's members from the enum class - #6160

Merged
Koudai Aono (koxudaxi) merged 1 commit into
fix7-10from
fix7-11
Nov 3, 2026
Merged

Koudai Aono (koxudaxi) merged 1 commit into
fix7-10from
fix7-11

Conversation

@koxudaxi

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

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of enum value types across merged enums, inherited enums, and compatibility checks.
    • Enum validation now accounts for declared value types when checking mixed values and wide integers, including integral floats.
    • Preserved legacy behavior for certain merged enums inherited by field-less models.
  • Documentation
    • Added an architecture decision entry describing how enum value types are determined and documenting the legacy merged-enum behavior.

@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Walkthrough

The change adds a shared helper to determine enum value bases. Enum merging, msgspec compatibility checks, and discriminator key generation now use the helper or Enum.value_base.

Changes: Enum value-base handling

Layer / File(s) Summary
Enum value-base contract
src/datamodel_code_generator/model/enum.py, docs/architecture.md
Adds EnumValueBaseRow and enum_value_base. Enum.value_base delegates to the helper, which checks the enum class and bases and supports a legacy compatibility row.
Merged-enum compatibility
src/datamodel_code_generator/parser/_all_of_values.py, src/datamodel_code_generator/parser/_passes/modules.py, docs/architecture.md
Merged-enum checks use enum_value_base with the legacy row to determine the specialized value type.
Value-base readers
src/datamodel_code_generator/parser/_passes/imports.py, src/datamodel_code_generator/parser/_tag_plan.py
Msgspec mixed-value and wide-integer checks use value_base. Discriminator keys use member values when a value base exists and enum-identity-qualified keys otherwise.

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

Change: Refactor

Possibly related issues

Possibly related PRs

Merge Risk: Minimal · up to d22cc

The shared enum value-base helper preserves behavior on the inspected generation paths. No concrete merge-blocking issue was identified; 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 clearly and concisely describes the main change: reading enum member value types from the enum class.
Docstring Coverage Passed Docstring coverage is 83.33% 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 83.33% 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 6 functions across 5 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

Warning

Some tools did not complete. Review the errors below.

LanguageTool
docs/architecture.md

LanguageTool checks are incomplete because the per-file request limit of 5 was reached. Remaining text was skipped; findings from completed checks are retained.

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

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.
Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between ff136c5 and d22cccf.

Files ignored due to path filters: 1
  • docs/llms-full.txt is excluded by none and included by none
Files selected for processing: 6
  • docs/architecture.md
  • src/datamodel_code_generator/model/enum.py
  • src/datamodel_code_generator/parser/_all_of_values.py
  • src/datamodel_code_generator/parser/_passes/imports.py
  • src/datamodel_code_generator/parser/_passes/modules.py
  • src/datamodel_code_generator/parser/_tag_plan.py

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

@koxudaxi
Koudai Aono (koxudaxi) marked this pull request as ready for review November 3, 2026 22:27
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 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-11-03T22:29:26.569841Z d22cccf Draft marked ready
ℹ️ 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

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

@codecov

codecov Bot commented Nov 3, 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 Nov 3, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 55.18%

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

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

⚡ 52 improved benchmarks
⏩ 98 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_perf_client_calls[sync] 44.1 ms 24.8 ms +77.95%
⚡ test_perf_simple_msgspec_unset_field_rendering 586.3 ms 330.4 ms +77.45%
⚡ test_perf_load_pyproject_config[mapping-None] 229.4 µs 130.8 µs +75.41%
⚡ test_perf_client_calls[async] 49.2 ms 28.3 ms +73.53%
⚡ test_perf_load_pyproject_config[config-None] 225.5 µs 130.9 µs +72.31%
⚡ test_perf_scalar_root_collapsed_builtin 230.8 ms 135 ms +70.96%
⚡ test_perf_local_reference_file_resolution 159.2 ms 93.4 ms +70.42%
⚡ test_perf_load_pyproject_config[mapping-multiple] 244.8 µs 144.4 µs +69.58%
⚡ test_perf_ordinary_array_constraint_generation 2.4 s 1.4 s +69.23%
⚡ test_perf_load_pyproject_config[config-multiple] 243.9 µs 144.6 µs +68.66%
⚡ test_perf_large_models_typed_dict[total-false-requiredness] 1,408.4 ms 840.4 ms +67.58%
⚡ test_perf_large_models_pydantic_v2_noformat 1,551.8 ms 936.7 ms +65.66%
⚡ test_perf_large_models_msgspec 2.3 s 1.4 s +64.95%
⚡ test_perf_inherited_required_fields[partial-base-first] 1,215.8 ms 743.4 ms +63.55%
⚡ test_perf_inherited_required_fields[required-derived-first] 865 ms 528.9 ms +63.53%
⚡ test_perf_large_models_typed_dict[legacy-requiredness] 1,381.2 ms 844.9 ms +63.48%
⚡ test_perf_inherited_required_fields[required-base-first] 763.8 ms 469 ms +62.87%
⚡ test_perf_large_models_single_module_stdout 1.8 s 1.1 s +62.51%
⚡ test_perf_inherited_required_fields[partial-derived-first] 1,303.8 ms 803.8 ms +62.2%
⚡ test_perf_large_models_pydantic_v2_builtin_double_quotes 1,573 ms 977.8 ms +60.87%
... ... ... ... ...

ℹ️ 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 fix7-11 (d22cccf) with fix7-10 (ff136c5)

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 bc704a0 into main Nov 3, 2026
117 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the fix7-11 branch November 3, 2026 23:58
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR is an internal refactor. It adds one helper in model/enum.py, enum_value_base, with an EnumValueBaseRow alias, and that helper now decides which builtin type an enum's members are instances of. It replaces the private _base_value_type, _SPECIALIZED_VALUE_BASES and _members_equal_values, and the inline IntEnum and StrEnum isinstance checks in _all_of_values.py, _passes/imports.py, _passes/modules.py and _tag_plan.py. The merged-enum call sites use the legacy-merged-enum-specialized-only row, which returns int for IntEnum, str for StrEnum and None otherwise, exactly as the old inline code did. Enum.value_base gives the same results as before: IntEnum and StrEnum map to int and str, and a use-subclass-enum builtin base maps to its type. The msgspec mixed-enum and wide-int warnings and the discriminator member-key check give the same answers for the enum kinds the generator writes. Only private names were removed. No CLI options, templates, defaults, Python version support or error reporting changed, and the docs files only gained new table rows. The behaviour diff gate compares the whole PR tier against the last release. Its accepted cells cover discriminator mapping paths, modules named annotations, integer constraints past 2 to the 53, prefixItems and JSON pointer handling, and none of the code paths behind them are touched by this diff, so they come from other PRs and are not attributed to this one.


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.

1 participant