Handle types.UnionType in _serialize_python_type for Python 3.10-3.13#2848
Handle types.UnionType in _serialize_python_type for Python 3.10-3.13#2848
Conversation
📝 WalkthroughWalkthroughAdds support for PEP 604 union syntax (X | Y) in Python 3.10–3.13 by handling Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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 |
|
📚 Docs Preview: https://pr-2848.datamodel-code-generator.pages.dev |
CodSpeed Performance ReportMerging #2848 will not alter performanceComparing
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/datamodel_code_generator/__main__.py (1)
635-656: LGTM! Well-implemented support for PEP 604 union syntax.The implementation correctly handles
types.UnionType(X | Y syntax) for Python 3.10-3.13:
- Properly version-gates the logic by checking
types.UnionType is not Union- Recursively serializes union arguments and preserves the
|syntax when needed- Correctly falls back to
_simple_type_namefor standard types- The
strict=Falseparameter inzip()is safe since this block only executes in Python 3.10+Minor cleanup: Remove unused noqa directives
Static analysis indicates the
# noqa: PLC0415directives on lines 635, 636, and 645 may be unused (PLC0415 might not be enabled in your Ruff config). Consider removing them:- import types # noqa: PLC0415 + import types - from typing import get_args, get_origin # noqa: PLC0415 + from typing import get_args, get_origin - from typing import Union # noqa: PLC0415 + from typing import Union
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/datamodel_code_generator/__main__.pytests/data/python/input_model/pydantic_models.pytests/test_input_model.py
🧰 Additional context used
🪛 Ruff (0.14.10)
src/datamodel_code_generator/__main__.py
635-635: Unused noqa directive (non-enabled: PLC0415)
Remove unused noqa directive
(RUF100)
636-636: Unused noqa directive (non-enabled: PLC0415)
Remove unused noqa directive
(RUF100)
645-645: Unused noqa directive (non-enabled: PLC0415)
Remove unused noqa directive
(RUF100)
⏰ 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). (11)
- GitHub Check: build-deploy
- GitHub Check: 3.13 on macOS
- GitHub Check: 3.11 on Ubuntu
- GitHub Check: 3.10 on macOS
- GitHub Check: 3.11 on Windows
- GitHub Check: 3.12 on Windows
- GitHub Check: benchmarks
- GitHub Check: 3.10 on Windows
- GitHub Check: 3.14 on Windows
- GitHub Check: 3.13 on Windows
- GitHub Check: Analyze (python)
🔇 Additional comments (2)
tests/data/python/input_model/pydantic_models.py (1)
36-36: LGTM! Test data correctly uses PEP 604 union syntax.The addition of
optional_mapping: Mapping[str, str] | Noneprovides appropriate test coverage for the newtypes.UnionTypehandling in Python 3.10+.tests/test_input_model.py (1)
595-606: LGTM! Comprehensive test for union syntax preservation.The test correctly verifies that
Mapping[str, str] | None(using PEP 604 syntax) is preserved in the generated output. The docstring provides helpful context about the Python version-specific behavior.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2848 +/- ##
=======================================
Coverage 99.50% 99.50%
=======================================
Files 90 90
Lines 14479 14489 +10
Branches 1735 1736 +1
=======================================
+ Hits 14407 14417 +10
Misses 37 37
Partials 35 35
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Breaking Change AnalysisResult: No breaking changes detected Reasoning: This PR is a bug fix that adds proper handling for Python's native union type syntax (X | Y) using types.UnionType in Python 3.10-3.13. Before this fix, when using preserved types like Mapping in a union with the pipe syntax (e.g., Mapping[str, str] | None), the serialization would not work correctly. The change is purely additive - it adds handling for a case that was previously unhandled. No existing functionality is modified or removed, no CLI/API changes, no template changes, and no Python version support changes. Users who weren't using the pipe syntax with preserved types won't see any difference, and users who were will now get correct output. This analysis was performed by Claude Code Action |
|
🎉 Released in 0.51.0 This PR is now available in the latest release. See the release notes for details. |
Summary by CodeRabbit
New Features
Tests
✏️ Tip: You can customize this high-level summary in your review settings.