Skip to content

Conversation

@davidhewitt
Copy link
Contributor

Change Summary

It turns out that wrap serializers can potentially need "retrying" in lax mode (as per the union logic), but this was not currently handled. This is necessary when a model contains unexpected extra fields; the Strict mode will fail.

Related issue number

Fixes pydantic/pydantic#10682

Checklist

  • Unit tests for the changes exist
  • Documentation reflects the changes where applicable
  • Pydantic tests pass with this pydantic-core (except for expected changes)
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

return Err(PydanticSerializationUnexpectedValue::new_err(None));
let type_name = field_extra.model_type_name();
return Err(PydanticSerializationUnexpectedValue::new_err(Some(format!(
"Unexpected field `{key}`{for_type_name}",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a drive-by improvement to the warning which was necessary for me to debug.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reminds me of #1483

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement though, thanks

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we go ahead and fix #1483 while we're at it here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last comment here - we should do a quick search through the codebase to see if other (similar) warnings can be updated with this structure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I will punt on both of these and leave them for a later refactoring of serialization warnings.

assert s.to_python(model_b) == {'field': 1, 'TAG': 'b'}


def test_union_model_wrap_serializer():
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test fails on main.

@codspeed-hq
Copy link

codspeed-hq bot commented Nov 4, 2024

CodSpeed Performance Report

Merging #1530 will not alter performance

Comparing dh/union-wrap-ser (21537b5) with main (cd0346d)

Summary

✅ 155 untouched benchmarks

Copy link
Contributor

@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, just a few notes on the error wording. Thanks @davidhewitt !

},
},
},
core_schema.union_schema(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much cleaner, thanks.

@davidhewitt davidhewitt enabled auto-merge (squash) November 12, 2024 12:57
@davidhewitt davidhewitt merged commit a3f13c7 into main Nov 12, 2024
28 checks passed
@davidhewitt davidhewitt deleted the dh/union-wrap-ser branch November 12, 2024 13:02
davidhewitt added a commit to pydantic/pydantic that referenced this pull request Oct 20, 2025
davidhewitt added a commit to pydantic/pydantic that referenced this pull request Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(🐞) invalid serialization warning when...

3 participants