Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix wrap serializer breaking union serialization in presence of extra fields #1530

Merged
merged 3 commits into from
Nov 12, 2024

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
Member

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
Member

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
Member

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
Member

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.

@@ -778,3 +761,67 @@ class ModelB:
model_b = ModelB(field=1)
assert s.to_python(model_a) == {'field': 1, 'TAG': 'a'}
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.

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
Member

@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
Member

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
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...
2 participants