Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions typer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading