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
9 changes: 9 additions & 0 deletions tests/test_type_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ class SomeEnum(Enum):
THREE = "three"


def test_enum_convertor_already_converted():
"""If the value is already an enum member, the convertor should return it as-is."""
from typer.main import generate_enum_convertor

convertor = generate_enum_convertor(SomeEnum)
result = convertor(SomeEnum.ONE)
assert result is SomeEnum.ONE


@pytest.mark.parametrize(
"type_annotation",
[list[Path], list[SomeEnum], list[str]],
Expand Down
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