Skip to content

Patch unset attributes with MISSING during model serialization with exclude_unset#12905

Merged
Viicos merged 5 commits intomainfrom
dh/union-exclude-unset
Mar 18, 2026
Merged

Patch unset attributes with MISSING during model serialization with exclude_unset#12905
Viicos merged 5 commits intomainfrom
dh/union-exclude-unset

Conversation

@davidhewitt
Copy link
Copy Markdown
Collaborator

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

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

@github-actions github-actions Bot added the relnotes-fix Used for bugfixes. label Mar 9, 2026
@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented Mar 9, 2026

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

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

View logs

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Mar 9, 2026

Merging this PR will not alter performance

✅ 212 untouched benchmarks


Comparing dh/union-exclude-unset (25afe64) with main (6178953)

Open in CodSpeed

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 9, 2026

Coverage report

This PR does not seem to contain any modification to coverable code.

@Viicos Viicos changed the title patch unset attributes with MISSING during model serialization with exclude_unset Patch unset attributes with MISSING during model serialization with exclude_unset Mar 10, 2026
@Viicos
Copy link
Copy Markdown
Member

Viicos commented Mar 10, 2026

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.

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)

@davidhewitt davidhewitt marked this pull request as ready for review March 18, 2026 14:33
@davidhewitt davidhewitt requested a review from Viicos March 18, 2026 14:35
Comment thread pydantic-core/src/serializers/type_serializers/model.rs
@Viicos Viicos enabled auto-merge (squash) March 18, 2026 16:36
@Viicos Viicos merged commit 0e4be5e into main Mar 18, 2026
81 checks passed
@Viicos Viicos deleted the dh/union-exclude-unset branch March 18, 2026 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

relnotes-fix Used for bugfixes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

model_dump(exclude_unset=True) produces spurious warnings for Union members with unset defaults

2 participants