Skip to content

Fix Enum option value lost when callback is used#1554

Open
worksbyfriday wants to merge 2 commits intofastapi:masterfrom
worksbyfriday:fix-enum-callback-223
Open

Fix Enum option value lost when callback is used#1554
worksbyfriday wants to merge 2 commits intofastapi:masterfrom
worksbyfriday:fix-enum-callback-223

Conversation

@worksbyfriday
Copy link

@worksbyfriday worksbyfriday commented Feb 17, 2026

Type of changes

  • Bug fix

AI?

  • AI was used to generate this PR

Checklist

  • I've run the latest black with default args on new code.
  • I've updated CHANGELOG.md and CONTRIBUTORS.md where appropriate (see note about typos above).
  • I've added tests for new code.

Description

Fixes #223

When a typer.Option has both a callback and an Enum type, the callback executes correctly but the command function receives None instead of the Enum value.

Root cause: The generate_enum_convertor in typer/main.py converts values by calling str(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 returns None.

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.

@svlandeg svlandeg added the bug Something isn't working label Mar 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] callback with type Enum passes None back to the app command

2 participants