Skip to content

Name --enum-field-as-literal all in the msgspec enum warnings only for enums that option writes as a Literal - #5292

Merged
Koudai Aono (koxudaxi) merged 1 commit into
fix-5283-collapsed-tuple-orderfrom
fix-5247-msgspec-enum-warning
Oct 24, 2026
Merged

Koudai Aono (koxudaxi) merged 1 commit into
fix-5283-collapsed-tuple-orderfrom
fix-5247-msgspec-enum-warning

Conversation

@koxudaxi

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

Copy link
Copy Markdown
Member

Fixes: #5247

Summary by CodeRabbit

  • Bug Fixes
    • Improved generated msgspec models when using --enum-field-as-literal all, including cases where enum values have mixed types.
    • Union-clash warnings now account for which enums are rendered as literals and only recommend the option when it applies to the enums involved.
    • Mixed-enum warnings now provide more relevant guidance while retaining the caveat for float values.
    • Added coverage for generated models and payload validation across mixed enum schemas.

…r enums that option writes as a Literal

msgspec refuses an Enum whose values are neither all str nor all int, and a union that holds an Enum beside another
member of its kind; the warnings about both recommended --enum-field-as-literal all. That option leaves an Enum class
for an enum whose x-enum-field-as-literal is false or whose --enum-field-as-literal-map entry is not literal, and for
the enum of a type list holding array or of an OpenAPI operation, so the advice did nothing there. The parser now
remembers the enum classes that the option would write as a Literal, decided as for the enum itself, and the warnings
name the option only for those; for the others they say only that msgspec cannot decode the enum or the union. The
generated output is unchanged.

Fixes: #5247
@coderabbitai

coderabbitai Bot commented Oct 24, 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 ccb9800 and 84bf5a6.

Files ignored due to path filters: 7
  • tests/data/expected/main/jsonschema/msgspec_mixed_enums/beside_literal_option[default].payloads.txt is excluded by !tests/data/**/*.txt and included by none
  • tests/data/expected/main/jsonschema/msgspec_mixed_enums/beside_literal_option[default].warnings.txt is excluded by !tests/data/**/*.txt and included by none
  • tests/data/expected/main/jsonschema/msgspec_mixed_enums/beside_literal_option[literal_all].warnings.txt is excluded by !tests/data/**/*.txt and included by none
  • tests/data/expected/main/jsonschema/msgspec_mixed_enums/msgspec_mixed_operation_enum.warnings.txt is excluded by !tests/data/**/*.txt and included by none
  • tests/data/jsonschema/msgspec_enums_beside_literal_option.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/openapi/msgspec_mixed_operation_enum.yaml is excluded by !tests/data/**/*.yaml and included by none
  • tests/data/payloads/msgspec_mixed_enums/msgspec_enums_beside_literal_option.json is excluded by !tests/data/**/*.json and included by none
Files selected for processing: 10
  • src/datamodel_code_generator/parser/_passes/imports.py
  • src/datamodel_code_generator/parser/base.py
  • src/datamodel_code_generator/parser/jsonschema.py
  • tests/data/expected/main/jsonschema/msgspec_mixed_enums/beside_literal_option[default].py
  • tests/data/expected/main/jsonschema/msgspec_mixed_enums/beside_literal_option[literal_all].py
  • tests/data/expected/main/jsonschema/msgspec_mixed_enums/msgspec_mixed_operation_enum.py
  • tests/main/jsonschema/test_msgspec_mixed_enums.py
  • tests/main/jsonschema/test_msgspec_mixed_enums.refs
  • tests/main/test_payload_validation.refs
  • tests/test_yaml_fast_constructor.refs

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

Walkthrough

The parser now tracks enums rendered as literals under --enum-field-as-literal all. Msgspec warnings use that information to recommend the option only when it applies to the enum or clash. Tests cover mixed enums in JSON Schema and OpenAPI inputs.

Changes: Msgspec enum warning behavior

Layer / File(s) Summary
Track enums rendered as literals
src/datamodel_code_generator/parser/base.py, src/datamodel_code_generator/parser/jsonschema.py
The parser records references for enums rendered as literals under the global setting. Enum parsing call sites use a shared helper to capture those references.
Condition warning recommendations
src/datamodel_code_generator/parser/_passes/imports.py
Clash and mixed-enum warnings recommend --enum-field-as-literal all only when the option renders the relevant enum or every enum in the clash as a literal.
Test mixed-enum warning cases
tests/main/jsonschema/test_msgspec_mixed_enums.py, tests/main/jsonschema/test_msgspec_mixed_enums.refs, tests/data/expected/main/jsonschema/msgspec_mixed_enums/*, tests/main/test_payload_validation.refs, tests/test_yaml_fast_constructor.refs
Tests and expected outputs cover mixed-enum warning behavior with and without the literal-all setting, including JSON Schema and OpenAPI cases.

Priority: Low

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

Change: Bug fix

Possibly related issues

Possibly related PRs

Merge Risk: Minimal · up to 84bf5

No actionable merge-blocking risk was identified in the enum-warning change; it is ready to merge after 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 main change: msgspec enum warnings recommend --enum-field-as-literal all only when the option produces a Literal.
Linked Issues check Passed The changes satisfy the coding requirements in #5247. JsonSchemaParser records enum references that --enum-field-as-literal all renders as Literal. Msgspec warning analysis uses those references…
Out of Scope Changes check Passed The changes remain within the scope of #5247. The union, array, and OpenAPI cases exercise the same option-aware msgspec enum warning behavior. Parser tracking, warning logic, generated-output referen…
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 14 functions across 6 files. (4 skipped: 3…
Full details: Linked Issues check

Explanation

The changes satisfy the coding requirements in #5247. JsonSchemaParser records enum references that --enum-field-as-literal all renders as Literal. Msgspec warning analysis uses those references for enum and union warnings. The warning recommends the option only when it applies, and otherwise reports that msgspec cannot decode the enum. Tests cover mixed enums, x-enum-field-as-literal: false and enum-map behavior, unions, arrays, and OpenAPI operations.

Full details: Out of Scope Changes check

Explanation

The changes remain within the scope of #5247. The union, array, and OpenAPI cases exercise the same option-aware msgspec enum warning behavior. Parser tracking, warning logic, generated-output references, and regression tests directly support that behavior. No unrelated product behavior is shown.

Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 14 functions across 6 files. (4 skipped: 3 unsupported, 1 too large.)

Finishing Touches: 💡 1
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
🛠️ Fix failing CI checks: 💡
  • 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 24, 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-24T06:10:04.738359Z 84bf5a6 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 24, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 17.85%

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

❌ 25 regressed benchmarks
✅ 25 untouched benchmarks
⏩ 98 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_perf_load_yaml_openapi_large 19.2 ms 27.4 ms -29.84%
❌ test_perf_simple_pydantic_v2_field_construction 5.9 ms 8 ms -26.86%
❌ test_perf_inherited_required_fields[required-derived-first] 390.8 ms 495.8 ms -21.18%
❌ test_perf_large_models_typed_dict[legacy-requiredness] 645.9 ms 812 ms -20.45%
❌ test_perf_large_models_pydantic_v2_builtin_double_quotes 775.6 ms 972.5 ms -20.25%
❌ test_perf_ordinary_array_constraint_generation 1.3 s 1.6 s -20.23%
❌ test_perf_openapi_large_pydantic_v2_builtin 775.6 ms 971.8 ms -20.18%
❌ test_perf_large_models_pydantic_v2_noformat 752.2 ms 935.7 ms -19.61%
❌ test_perf_large_models_typed_dict[total-false-requiredness] 655.3 ms 812.3 ms -19.33%
❌ test_perf_target_render[client-small] 17 ms 20.9 ms -18.95%
❌ test_perf_inherited_required_fields[required-base-first] 422.1 ms 520.3 ms -18.87%
❌ test_perf_large_models_pydantic_v2_builtin 768 ms 937.6 ms -18.09%
❌ test_perf_inherited_required_fields[partial-base-first] 643.4 ms 779.7 ms -17.48%
❌ test_perf_inherited_required_fields[partial-derived-first] 592.8 ms 715.4 ms -17.14%
❌ test_perf_simple_msgspec_unset_field_rendering 73.7 ms 87.6 ms -15.86%
❌ test_perf_multiple_files_input 2 s 2.4 s -15.65%
❌ test_perf_large_models_pydantic_v2 1.9 s 2.2 s -15.04%
❌ test_perf_duplicate_names 559.9 ms 656.8 ms -14.75%
❌ test_perf_large_models_single_module_stdout 878.5 ms 1,027.8 ms -14.53%
❌ test_perf_complex_refs 1.1 s 1.3 s -14.2%
... ... ... ... ...

ℹ️ 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 fix-5247-msgspec-enum-warning (84bf5a6) with fix-5283-collapsed-tuple-order (ccb9800)

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 24, 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 47e2c80 into main Oct 24, 2026
131 of 132 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the fix-5247-msgspec-enum-warning branch October 24, 2026 07:54
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: The PR only refines the text of existing msgspec decoding UserWarnings so that the recommendation to pass --enum-field-as-literal all appears only for enums that the option would actually write as Literal values; enums kept as classes by x-enum-field-as-literal false, an enum-field-as-literal-map entry other than literal, a type list containing array, or OpenAPI operation response enums now get the warning without that advice. The same warnings are still emitted under the same conditions, generated code output is unchanged as shown by the new expected fixtures, no CLI options or public API are added or removed, and the source changes are confined to private helpers such as _msgspec_enum_clashes, _msgspec_mixed_enum_message, the _MsgspecEnumClash alias, a new private parser attribute and a new private _parse_enum_class wrapper around parse_enum, plus tests and fixtures, so none of the breaking change categories apply.


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.

msgspec enum warning recommends --enum-field-as-literal all where that option does not apply

1 participant