Fix Enum option value lost when callback is used#1554
Open
Fridayai700 wants to merge 1 commit intofastapi:masterfrom
Open
Fix Enum option value lost when callback is used#1554Fridayai700 wants to merge 1 commit intofastapi:masterfrom
Fridayai700 wants to merge 1 commit intofastapi:masterfrom
Conversation
Closes fastapi#223 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Type of changes
AI?
Checklist
Description
Fixes #223
When a
typer.Optionhas both acallbackand anEnumtype, the callback executes correctly but the command function receivesNoneinstead of the Enum value.Root cause: The
generate_enum_convertorintyper/main.pyconverts values by callingstr(value)and looking up the result in a map of{str(enum.value): enum_member}. When a callback returns an Enum instance,str(EndpointChoices.localhost)produces"EndpointChoices.localhost", which doesn't match any key in the map (keys are"localhost","staging", etc.). The convertor returnsNone.Fix: Add an early return in the enum convertor: if the value is already an instance of the target enum, return it directly instead of trying to convert it again.
Test added:
test_enum_with_callback— verifies that an Enum option with a callback correctly passes the Enum value to the command function. Confirmed the test fails without the fix ('NoneType' object has no attribute 'value') and passes with it.