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 0bea4c2315..421f72ca1e 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]