Skip to content

feat: Add --enum-field-as-literal none option and respect user settings for TypedDict#2691

Merged
koxudaxi merged 2 commits intomainfrom
fix/typeddict-respect-enum-field-as-literal
Dec 19, 2025
Merged

feat: Add --enum-field-as-literal none option and respect user settings for TypedDict#2691
koxudaxi merged 2 commits intomainfrom
fix/typeddict-respect-enum-field-as-literal

Conversation

@koxudaxi
Copy link
Copy Markdown
Owner

@koxudaxi koxudaxi commented Dec 19, 2025

Closes: #2411

Summary by CodeRabbit

  • New Features

    • Added a "none" option for the --enum-field-as-literal flag to always generate Enum classes.
  • Improvements

    • Preserves user-specified enum-field-as-literal values when producing TypedDict outputs; defaults to literal-only behavior only when unset.
    • Clarified and simplified enum parsing logic for more readable and predictable behavior.
  • Documentation

    • Enhanced help text for the --enum-field-as-literal option.
  • Tests

    • Added tests covering none/one/all enum-field-as-literal scenarios.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Dec 19, 2025

Walkthrough

Preserves user-provided enum_field_as_literal when generating TypingTypedDict; adds LiteralType.Off = "none"; refactors enum-literal decision logic in GraphQL/JSON Schema parsers; adds JSON Schema fixture, expected outputs for three modes, and tests covering none/one/all behaviors.

Changes

Cohort / File(s) Summary
Core parameter handling
src/datamodel_code_generator/__init__.py
Change: only default enum_field_as_literal to LiteralType.All for TypingTypedDict when the provided value is None; preserve any explicitly supplied value.
Enum extension
src/datamodel_code_generator/parser/__init__.py
Change: add LiteralType.Off = "none" enum member to represent "never convert to Literal".
CLI/help text
src/datamodel_code_generator/arguments.py
Change: extend --enum-field-as-literal help text to document the new none option.
Parser logic
src/datamodel_code_generator/parser/jsonschema.py,
src/datamodel_code_generator/parser/graphql.py
Change: refactor should_parse_enum_as_literal into explicit conditional branches (behavior preserved: All → True, One → True when single value, Off/other → False).
Test fixture
tests/data/jsonschema/enum_literal_typed_dict.json
Add: JSON Schema defining object with status and priority enum properties used by tests.
Expected generated outputs
tests/data/expected/main/jsonschema/typed_dict_enum_literal_all.py,
tests/data/expected/main/jsonschema/typed_dict_enum_literal_one.py,
tests/data/expected/main/jsonschema/typed_dict_enum_literal_none.py
Add: three expected TypedDict/enum output modules demonstrating all, one, and none behaviors (Literals vs StrEnum classes).
Tests
tests/main/jsonschema/test_main_jsonschema.py
Add: tests test_main_typed_dict_enum_field_as_literal_none, test_main_typed_dict_enum_field_as_literal_one, test_main_typed_dict_enum_field_as_literal_all (duplicated insertion observed); each runs generator with respective --enum-field-as-literal and checks expected files.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • Inspect src/datamodel_code_generator/__init__.py to confirm None-check/override logic is correct and doesn't affect other output model types.
  • Verify LiteralType.Off is handled consistently across CLI docs and parser conditionals.
  • Review parser refactors in jsonschema.py and graphql.py to ensure no behavioral regression.
  • Validate new tests and expected files; check for duplicated test additions.

Possibly related PRs

Poem

🐰
I nibbled code beneath the moon,
Off, One, All—now choose your tune.
TypedDict fields wear Literal bows,
Or StrEnum jackets, neat in rows.
Hop, compile, and watch types bloom!

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main changes: adding a new --enum-field-as-literal none option and modifying TypedDict to respect user enum settings.
Linked Issues check ✅ Passed The PR addresses issue #2411 by implementing support for LiteralType.One and LiteralType.All in TypedDict generation, and adds the new LiteralType.Off (none) option as an enhancement.
Out of Scope Changes check ✅ Passed All changes are within scope: enum handling refactors in GraphQL and JSONSchema parsers support the feature, new enum value and help text document the new option, and test files validate the functionality.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/typeddict-respect-enum-field-as-literal

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a90b618 and 28189f6.

📒 Files selected for processing (1)
  • tests/main/jsonschema/test_main_jsonschema.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/main/jsonschema/test_main_jsonschema.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (14)
  • GitHub Check: 3.11 on Windows
  • GitHub Check: py312-isort7 on Ubuntu
  • GitHub Check: py312-black22 on Ubuntu
  • GitHub Check: py312-pydantic1 on Ubuntu
  • GitHub Check: py312-black23 on Ubuntu
  • GitHub Check: 3.13 on Windows
  • GitHub Check: 3.14 on Windows
  • GitHub Check: 3.9 on Windows
  • GitHub Check: 3.10 on Windows
  • GitHub Check: 3.14 on macOS
  • GitHub Check: 3.12 on Windows
  • GitHub Check: 3.13 on Ubuntu
  • GitHub Check: Analyze (python)
  • GitHub Check: benchmarks

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 and usage tips.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Dec 19, 2025

CodSpeed Performance Report

Merging #2691 will not alter performance

Comparing fix/typeddict-respect-enum-field-as-literal (28189f6) with main (38b953f)

Summary

✅ 52 untouched
⏩ 10 skipped1

Footnotes

  1. 10 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
Copy link
Copy Markdown

codecov Bot commented Dec 19, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.53%. Comparing base (d325cf3) to head (28189f6).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2691   +/-   ##
=======================================
  Coverage   99.53%   99.53%           
=======================================
  Files          81       81           
  Lines       11366    11387   +21     
  Branches     1357     1361    +4     
=======================================
+ Hits        11313    11334   +21     
  Misses         32       32           
  Partials       21       21           
Flag Coverage Δ
unittests 99.53% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@koxudaxi koxudaxi merged commit 29a6f5b into main Dec 19, 2025
41 checks passed
@koxudaxi koxudaxi deleted the fix/typeddict-respect-enum-field-as-literal branch December 19, 2025 06:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

generate() for TypingTypedDict does not support LiteralType.One or LiteralType.All

1 participant