Repository navigation
fix: match enum-valued Literal discriminators by their unstructured value - #840
Open
vanshbarfiwala-lgtm wants to merge 1 commit into
Open
vanshbarfiwala-lgtm wants to merge 1 commit into
vanshbarfiwala-lgtm wants to merge 1 commit into
Conversation
…alue create_default_dis_func keyed the discriminator mapping by the raw Literal args, but the disambiguator looks them up against the already-unstructured input. For an enum-valued Literal the member is serialized to its value, so the lookup raised KeyError (structure failed while unstructure succeeded). Key the mapping by the unstructured value, which is identity for str/int/bool and the member's value for enums.
This branch has not been deployed
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.
Structuring a union of attrs classes discriminated by an enum-valued
Literalcrashes withKeyError, even though unstructuring the same union works. String- and integer-valuedLiteraldiscriminators work, so it is an asymmetric round-trip failure specific to enums.Cause: in
create_default_dis_func(src/cattrs/disambiguators.py), the value-to-class mapping is keyed by the rawLiteralargs, i.e. the enum members. The returned disambiguator then looks up the discriminator from the already-unstructured payload, where the member has been serialized to its value ('cat'), so the member keys never match. Forstr/intLiterals the member and its serialized value are identical, which is why only enums break.Fix: key the mapping by the unstructured value (
converter.unstructure(key)). This is identity forstr/int/bool(existing cases unchanged) and yields the enum's value for enum members, so lookups match the unstructured discriminator. Added a test and a HISTORY entry.