Patch unset attributes with MISSING during model serialization with exclude_unset#12905
Merged
Patch unset attributes with MISSING during model serialization with exclude_unset#12905
MISSING during model serialization with exclude_unset#12905Conversation
… `exclude_unset`
Deploying pydantic-docs with
|
| Latest commit: |
25afe64
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://845dc080.pydantic-docs.pages.dev |
| Branch Preview URL: | https://dh-union-exclude-unset.pydantic-docs.pages.dev |
Contributor
MISSING during model serialization with exclude_unsetMISSING during model serialization with exclude_unset
Member
It looks like the following behaves as expected? from typing import Annotated
from pydantic import BaseModel, WrapSerializer, model_serializer
def ser(v, h):
# not called
return h(v)
class Model(BaseModel):
a: Annotated[int, WrapSerializer(ser)] = 1
@model_serializer(mode='wrap')
def ser_model(self, handler):
rv = handler(self)
print(rv) # {}
return rv
m = Model()
m.model_dump(exclude_unset=True) |
Viicos
reviewed
Mar 18, 2026
Co-authored-by: Victorien <[email protected]>
Viicos
approved these changes
Mar 18, 2026
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.
Change Summary
This is a possible fix for #12888 that avoids regressing the tests previously added.
The approach is to use the missing sentinel to signal which fields are unset during serialization instead of removing them from the dictionary.
Note: I think this is probably user-observable with custom serializer functions (e.g. wrap serializers); I'm unsure if that means that this approach is not ok (or has to be limited only to when there are no model serializers present). We should discuss.
Related issue number
Fixes #12888
Checklist