No longer fall back to trying all union members when the variant selected by discriminator fails to serialize#12825
Conversation
|
please review |
49f539e to
9c46ac4
Compare
davidhewitt
left a comment
There was a problem hiding this comment.
Thanks for the PR, and sorry for the slow reply.
I have been thinking about this PR and discussing with @Viicos. I believe (@Viicos please correct me if I am wrong) that while this is slightly breaking because it removes the left-to-right path from this serialization, it feels almost certain that if the tag selected a variant which then fails to serialize, the other union members will fail to serialize too.
There might be some edge case out there where the left-to-right serialization was succeeding, but falling back to type inference is probably good enough for most of those, and if the left-to-right serialization was really necessary we can encourage users to use a TypeAdapter in left-to-right serialization mode to define their own PlainSerializer.
We should make sure that this is mentioned in the release notes for 2.13.
2866635 to
219b6c9
Compare
davidhewitt
left a comment
There was a problem hiding this comment.
Thanks, I think we can simplify the test and then merge.
219b6c9 to
0384c08
Compare
0384c08 to
9780fc4
Compare
9780fc4 to
ad3f375
Compare
Change Summary
Don't try every union member when the discriminator already found the right one but serialization failed.
Related issue number
Closes pydantic/pydantic-core#1455
fix #12800
Ref #12822 (reduces spurious warnings from N to 1 by not falling back to all union members)
Checklist
Selected Reviewer: @Viicos