Fix enum convertor returning None for already-converted values#1563
Open
bysiber wants to merge 2 commits intofastapi:masterfrom
Open
Fix enum convertor returning None for already-converted values#1563bysiber wants to merge 2 commits intofastapi:masterfrom
bysiber wants to merge 2 commits intofastapi:masterfrom
Conversation
When an enum parameter has a user callback, the value gets converted twice: once in the parameter callback wrapper and again in the command callback wrapper. The second pass receives an enum member, but str(EnumMember) returns 'ClassName.member' which doesn't match the val_map keys, so the convertor silently returns None. Added an isinstance check (same pattern as param_path_convertor) to pass through values that are already enum members.
Member
|
Hi, thanks for the PR! Could you look into the failing CI? I'll put this in draft in the meantime 🙏 |
Author
|
Added a test for the newly covered branch — it checks that passing an already-converted enum member through the convertor returns it unchanged. CI should be green now. |
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.
Summary
generate_enum_convertorsilently returnsNonewhen it receives a value that's already an enum member. This happens when an enum parameter has a user-defined callback — the value gets double-converted.The Bug
The enum convertor builds a lookup map from
str(val.value)for each member, then looks up incoming values usingstr(value). When the value is already an enum member (e.g.,Network.conv),str(Network.conv)returns"Network.conv"— which doesn't match any key inval_map(which contains"conv"). The convertor falls through and implicitly returnsNone.This happens because the parameter callback wrapper (via
get_param_callback) converts the string to an enum, then the command callback wrapper (viaget_callback) runs the convertor again on the already-converted value.The Fix
Added an
isinstanceguard to return the value as-is when it's already an enum member — the same pattern used byparam_path_convertora few lines above.