Skip to content
Open
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
24 changes: 24 additions & 0 deletions tests/test_type_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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]:

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]
Expand Down
Loading