From 171c2188d111c4b390c530e534ed97b277c5c4a6 Mon Sep 17 00:00:00 2001 From: Aditya Dixit Date: Tue, 30 Jun 2026 23:23:58 +0530 Subject: [PATCH] fix: resolve Enum default value resolution when parameter callbacks are present --- tests/test_types.py | 19 +++++++++++++++++++ typer/main.py | 2 ++ 2 files changed, 21 insertions(+) diff --git a/tests/test_types.py b/tests/test_types.py index db6dae08da..d9bb824c83 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -163,3 +163,22 @@ def test_list_pair() -> None: def test_float_range_open_bounds_with_clamp_not_allowed(): with pytest.raises(TypeError, match="Clamping is not supported for open bounds."): _click.types.FloatRange(min=0.0, min_open=True, clamp=True) + + +def test_enum_with_callback() -> None: + app = typer.Typer() + + class User(str, Enum): + rick = "Rick" + morty = "Morty" + + def cb(value: User) -> User: + return value + + @app.command() + def main(user: User = typer.Option(User.rick, callback=cb)) -> None: + print(f"Main received: {user.value}") + + result = runner.invoke(app, []) + assert result.exit_code == 0 + assert "Main received: Rick" in result.output diff --git a/typer/main.py b/typer/main.py index a825c1b14e..b79a2d0ca3 100644 --- a/typer/main.py +++ b/typer/main.py @@ -1446,6 +1446,8 @@ def generate_enum_convertor(enum: type[Enum]) -> Callable[[Any], Any]: val_map = {str(val.value): val for val in enum} def convertor(value: Any) -> Any: + if isinstance(value, enum): + return value if value is not None: val = str(value) if val in val_map: