Repository navigation
Fix tagged unions dropping tags that are also member fields - #831
Closed
Aschenbath wants to merge 1 commit into
Closed
Aschenbath wants to merge 1 commit into
Aschenbath wants to merge 1 commit into
Conversation
Author
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.
With
forbid_extra_keys=True,configure_tagged_unionpopped the tag out of the payload before dispatching to the member's structure hook. That pop is needed so members that don't declare the tag don't trip the extra-keys check — but when a member class does declare a field with the tag's name, the pop removed data its hook requires:This moves the pop from the dispatchers into the per-member wrapper: the dispatchers now pass the payload through, and each member hook receives a tag-stripped copy only if that member does not consume the tag as one of its own fields (checked against its init fields, accounting for cattrs' leading-underscore key renaming). The
default=fallback for unknown tags keeps the previous behavior (tag stripped before the default handler runs). Non-forbid_extra_keysconverters are untouched.Tests: new cases in
tests/strategies/test_tagged_unions.pycover the reported scenario with and withoutdefault=(structure succeeds, the member'stypefield is populated from the tag, unstructure round-trips) and confirm members that don't declare the tag still structure fine underforbid_extra_keys=True. The strategies suite plustests/test_unions.pypass: 285 passed, 15 xfailed. A changelog entry is added underNEXTin HISTORY.md.Fixes #483