Repository navigation
Name --enum-field-as-literal all in the msgspec enum warnings only for enums that option writes as a Literal - #5292
Conversation
…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
|
No actionable comments were generated in the recent review.
|
| 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:
Estimated code review effort: 3 (Moderate) | ~25 minutes
Change: Bug fix
Possibly related issues
- msgspec.Struct output that msgspec rejects (mixed enums, constraints on Any and on non-matching types) #4493 — This issue concerns msgspec rejection of mixed-type enums. This pull request adjusts warning recommendations but does not change generated output to address that rejection.
Possibly related PRs
- datamodel-code-generator/datamodel-code-generator#5270 — This PR moves the msgspec warning pass that this pull request updates.
- datamodel-code-generator/datamodel-code-generator#5256 — This PR adds warnings in the same msgspec enum analysis path.
- datamodel-code-generator/datamodel-code-generator#5234 — This PR adds mixed-type enum warnings and recommendations that this pull request makes conditional.
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: 
5
-
Full details: Linked Issues check
-
Explanation
The changes satisfy the coding requirements in
#5247.JsonSchemaParserrecords enum references that--enum-field-as-literal allrenders asLiteral. 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: falseand 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.
Comment @coderabbitai help to get the list of available commands.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour 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. |
Merging this PR will degrade performance by 17.85%1 warning
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Breaking Change AnalysisResult: 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 |
Fixes: #5247
Summary by CodeRabbit
--enum-field-as-literal all, including cases where enum values have mixed types.