Skip to content

Fix x-python-type for Optional container types in anyOf schemas#2840

Merged
koxudaxi merged 1 commit intomainfrom
fix/x-python-type-anyof-propagation
Dec 28, 2025
Merged

Fix x-python-type for Optional container types in anyOf schemas#2840
koxudaxi merged 1 commit intomainfrom
fix/x-python-type-anyof-propagation

Conversation

@koxudaxi
Copy link
Copy Markdown
Owner

@koxudaxi koxudaxi commented Dec 28, 2025

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of Union and Optional types when processing Python input models, ensuring more accurate type preservation for complex generic types like Optional[Set] and Union[None, FrozenSet].
  • Tests

    • Added test coverage for Python type preservation with Union and Optional types.

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

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Dec 28, 2025

📝 Walkthrough

Walkthrough

Enhanced x-python-type interpretation in the JSON schema parser to recognize and extract flags from Union and Optional inner types. Added test coverage for Optional[Set[str]] and Union[None, FrozenSet[str]] handling with the --input-model flag, along with a new field in the test model.

Changes

Cohort / File(s) Summary
Parser Enhancement
src/datamodel_code_generator/parser/jsonschema.py
Extended x-python-type flag resolution logic to detect Union/Optional generic types and parse their inner type lists to determine applicable flags, while preserving existing direct-mapping behavior for concrete types.
Test Model Updates
tests/data/python/input_model/pydantic_models.py
Added nullable_frozenset: Union[None, FrozenSet[str]] field to ModelWithPythonTypes class and extended imports to include Union.
Test Coverage
tests/test_input_model.py
Added three test functions covering Optional[Set[str]] preservation, Optional[Set[str]] with TypedDict output, and Union[None, FrozenSet[str]] handling; tests are duplicated in the file with identical intent.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • PR #2837: Introduces _get_python_type_flags function and parent object propagation in jsonschema.py to extract container-type flags from x-python-type metadata; this PR builds upon that foundation by extending the flag detection logic to handle Union/Optional inner types.

Poem

🐰 With Union types and Optional friends,
We parse the inner types that nature sends,
Frozensets now wrapped in flags so bright,
Your generic types are handled just right! ✨

Pre-merge checks and finishing touches

✅ Passed checks (3 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 change: enhancing x-python-type interpretation to support Union and Optional inner types in anyOf schemas, which is the primary focus of the modifications in jsonschema.py.
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/x-python-type-anyof-propagation

📜 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 bb0eb62 and 364affb.

📒 Files selected for processing (3)
  • src/datamodel_code_generator/parser/jsonschema.py
  • tests/data/python/input_model/pydantic_models.py
  • tests/test_input_model.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). (9)
  • GitHub Check: 3.12 on Windows
  • GitHub Check: 3.10 on macOS
  • GitHub Check: 3.10 on Windows
  • GitHub Check: 3.11 on macOS
  • GitHub Check: 3.11 on Windows
  • GitHub Check: benchmarks
  • GitHub Check: 3.13 on Windows
  • GitHub Check: 3.14 on Windows
  • GitHub Check: Analyze (python)
🔇 Additional comments (3)
src/datamodel_code_generator/parser/jsonschema.py (1)

1250-1277: LGTM! Clean implementation for extracting container flags from Union/Optional wrapper types.

The bracket-depth parsing correctly handles nested generic types like Union[None, FrozenSet[str]] and Optional[Set[str]]. The early return on first matching container type is appropriate since a Union/Optional can only wrap one container type meaningfully for the purpose of flag extraction.

tests/data/python/input_model/pydantic_models.py (1)

6-6: LGTM! Good test coverage for Union with container type not in first position.

Using Union[None, FrozenSet[str]] (instead of Optional[FrozenSet[str]]) is a good choice to specifically test that the parsing logic correctly finds the container type when it's not the first argument in the Union.

Also applies to: 35-35

tests/test_input_model.py (1)

564-592: LGTM! Comprehensive test coverage for Optional/Union container type handling.

The three new tests properly exercise the enhanced _get_python_type_flags logic:

  • test_input_model_optional_set_type: Validates Optional[Set[str]] is preserved
  • test_input_model_optional_set_to_typeddict: Ensures it works with TypedDict output
  • test_input_model_union_none_frozenset: Tests the case where None precedes the container type in Union

The expected output set[str] | None and frozenset[str] | None correctly reflects the modern union syntax.


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.

@github-actions
Copy link
Copy Markdown
Contributor

📚 Docs Preview: https://pr-2840.datamodel-code-generator.pages.dev

@koxudaxi koxudaxi enabled auto-merge (squash) December 28, 2025 16:28
@koxudaxi koxudaxi merged commit a67163c into main Dec 28, 2025
32 of 33 checks passed
@koxudaxi koxudaxi deleted the fix/x-python-type-anyof-propagation branch December 28, 2025 16:29
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Dec 28, 2025

CodSpeed Performance Report

Merging #2840 will degrade performance by 19.03%

Comparing fix/x-python-type-anyof-propagation (364affb) with main (bb0eb62)

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

Summary

❌ 11 regressions
⏩ 98 skipped1

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Mode Benchmark BASE HEAD Efficiency
WallTime test_perf_all_options_enabled 5.8 s 6.8 s -15.65%
WallTime test_perf_duplicate_names 863.2 ms 1,033.1 ms -16.45%
WallTime test_perf_complex_refs 1.8 s 2.2 s -19.03%
WallTime test_perf_stripe_style_pydantic_v2 1.8 s 2.1 s -17.09%
WallTime test_perf_multiple_files_input 3.2 s 3.8 s -17.44%
WallTime test_perf_aws_style_openapi_pydantic_v2 1.7 s 2 s -17.34%
WallTime test_perf_graphql_style_pydantic_v2 717.4 ms 866.3 ms -17.19%
WallTime test_perf_kubernetes_style_pydantic_v2 2.3 s 2.8 s -18.59%
WallTime test_perf_deep_nested 5.3 s 6.4 s -17.01%
WallTime test_perf_large_models_pydantic_v2 3.1 s 3.8 s -17.98%
WallTime test_perf_openapi_large 2.5 s 3.1 s -18.34%

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.

@github-actions
Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR is a bug fix that corrects incorrect behavior when processing x-python-type values wrapped in Union or Optional types. Previously, Optional[Set[str]] or Union[None, FrozenSet[str]] would fail to preserve the Set/FrozenSet type because the code only checked the outermost type name (Union/Optional) against the container type mapping. The fix parses inside Union/Optional wrappers to find the actual container type. While the generated output will change for affected users (from incorrect list[str] | None to correct set[str] | None), this is a correction of broken behavior rather than a breaking change to working functionality. Users relying on the previous incorrect output would have been experiencing a bug, not expected behavior.


This analysis was performed by Claude Code Action

@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 28, 2025

Codecov Report

❌ Patch coverage is 91.17647% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.49%. Comparing base (bb0eb62) to head (364affb).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/datamodel_code_generator/parser/jsonschema.py 88.00% 0 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2840      +/-   ##
==========================================
- Coverage   99.51%   99.49%   -0.02%     
==========================================
  Files          90       90              
  Lines       14258    14290      +32     
  Branches     1695     1705      +10     
==========================================
+ Hits        14189    14218      +29     
  Misses         37       37              
- Partials       32       35       +3     
Flag Coverage Δ
unittests 99.49% <91.17%> (-0.02%) ⬇️

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.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 1, 2026

🎉 Released in 0.51.0

This PR is now available in the latest release. See the release notes for details.

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