fix: improve enum misuse error - #770
Open
Sohel2309 wants to merge 1 commit into
Open
Conversation
Member
|
The problem is this inserts an isinstance check into every unstructure call, affecting performance. cattrs expects the type hints to be correct, so I'm not inclined to trade performance for better error messages in this particular case. |
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.
Bug
Using a non-Enum value with an enum-typed attrs field can result in an
unhelpful
AttributeError: 'str' object has no attribute 'value'.Root cause
enum_unstructure_factory()generated unstructure hooks that accessed.valuewithout first checking that the value was an instance of theexpected Enum type.
Fix
Add an
isinstance()check for the expected Enum type before accessing.value. Invalid values now raise a clearTypeErrordescribing theexpected Enum type and the received value.
The fix covers both the regular Enum and typed-enum paths.
Tests
Added regression tests covering:
The regression tests were verified to fail before the fix and pass after
the fix.
Changelog
Added the fix to
HISTORY.md.