fix(dto): apply msgspec constraints to the constrained union member - #4988
Open
NoiceHax wants to merge 3 commits into
Open
fix(dto): apply msgspec constraints to the constrained union member#4988NoiceHax wants to merge 3 commits into
NoiceHax wants to merge 3 commits into
Conversation
Transfer models wrap the type of optional and partial fields in a union with `None`/`UnsetType`, but the constraints extracted from the source model were attached to the union itself, producing annotations such as `Annotated[Union[str, UnsetType], Meta(min_length=8)]`. msgspec only accepts type specific constraints on the type they apply to and raises a `TypeError` for those, so any DTO with a constrained optional field or a `partial` config failed to build. The metadata is now applied to the single constrainable union member instead, which also makes dropping length constraints for partial DTOs - a workaround for the same limitation - unnecessary. Closes litestar-org#4181
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4988 +/- ##
==========================================
+ Coverage 67.41% 67.42% +0.01%
==========================================
Files 293 293
Lines 15308 15316 +8
Branches 1736 1738 +2
==========================================
+ Hits 10320 10327 +7
- Misses 4839 4841 +2
+ Partials 149 148 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
`_annotate_with_meta` only moves the metadata onto a union member when there is exactly one member that can carry it. The fallback path, where the union has more than one such member and the metadata stays on the union itself, had no test. Add one so the branch is exercised.
| if field_definition.passthrough_constraints: | ||
| if (field_meta := _create_struct_field_meta_for_field_definition(field_definition)) is not None: | ||
| field_type = Annotated[field_type, field_meta] | ||
| field_type = _annotate_with_meta(field_type, field_meta) |
Member
There was a problem hiding this comment.
Wouldn't it be easier to check field_definition.is_partial than do the type unwrapping? iiuc this case only happens for partial fields where we add an outer Union. In other cases, we do not alter the type, so a wrongly attached Meta is a user error, which we should not silently fix.
Remove the _annotate_with_meta helper and its extra imports (get_args, is_union, NoneType). Apply the constraint annotation to the inner type before wrapping in Union only when field_definition.is_partial is set, which is the only case where we add the outer Union ourselves. For all other cases the user's own annotation is untouched (a wrongly attached Meta is a user error).
NoiceHax
force-pushed
the
fix/issue-4181
branch
from
August 28, 2026 16:36
f64f881 to
08d4967
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Transfer models wrap optional and partial fields in a union with
NoneorUnsetType, but constraints copied from the source model were attached to the union itself, giving annotations likeAnnotated[Union[str, UnsetType], Meta(min_length=8)]. msgspec only accepts type specific constraints on the type they apply to and raises a TypeError for that, so any DTO with a constrained optional field orpartial=Truefailed to build.The meta now goes on the single constrainable member of the union. If a union has more than one member that could take constraints there is nowhere obvious to put it, so it stays on the union as before. Partial DTOs no longer need to drop
min_lengthandmax_length, which was a workaround for the same problem.The old workaround test is replaced by a parametrized one covering each constraint on both backends, and I added tests for
Union[Annotated[str, Meta], UnsetType]andOptional[Annotated[str, Meta]]. They are in tests/unit/test_dto/test_factory/test_integration.py and fail without the change.Closes #4181
📚 Documentation preview 📚: https://litestar-org.github.io/litestar-docs-preview/4988