From 2e741efd0439009c2782a9138f3702f00f6950cf Mon Sep 17 00:00:00 2001 From: Friday Date: Tue, 17 Feb 2026 17:37:49 +0000 Subject: [PATCH] Fix Enum option value lost when callback is used Closes #223 Co-Authored-By: Claude Opus 4.6 --- tests/test_type_conversion.py | 24 ++++++++++++++++++++++++ typer/main.py | 2 ++ 2 files changed, 26 insertions(+) diff --git a/tests/test_type_conversion.py b/tests/test_type_conversion.py index 83edd1ecb5..7aa0001e00 100644 --- a/tests/test_type_conversion.py +++ b/tests/test_type_conversion.py @@ -168,3 +168,27 @@ def custom_click_type( result = runner.invoke(app, ["0x56"]) assert result.exit_code == 0 + + +def test_enum_with_callback(): + """Test that Enum value is preserved when a callback is used (issue #223).""" + app = typer.Typer() + + class Endpoint(str, Enum): + localhost = "localhost" + staging = "staging" + + def validate_endpoint(value: Endpoint): + return value + + @app.command() + def cmd( + endpoint: Endpoint = typer.Option( + ..., case_sensitive=False, callback=validate_endpoint + ), + ): + print(f"endpoint={endpoint.value}") + + result = runner.invoke(app, ["--endpoint", "localhost"]) + assert result.exit_code == 0, result.output + assert "endpoint=localhost" in result.output diff --git a/typer/main.py b/typer/main.py index f4f21bb844..98a2d2195e 100644 --- a/typer/main.py +++ b/typer/main.py @@ -1446,6 +1446,8 @@ def generate_enum_convertor(enum: type[Enum]) -> Callable[[Any], Any]: def convertor(value: Any) -> Any: if value is not None: + if isinstance(value, enum): + return value val = str(value) if val in val_map: key = val_map[val]