Closed daneah closed 1 month ago
A workaround for this is to override the Enum
class's __str__
method to return the string representation of the enum value:
from typing_extensions import Annotated
class SomeChoiceType(str, Enum):
ONE = "one"
TWO = "two"
def __str__(self):
return str(self.value)
some_app = typer.Typer()
@some_app.command
def some_command(some_option: Annotated[SomeChoiceType] = SomeChoiceType.ONE):
...
This continues allowing Typer to help with finding type safety issues while allowing Trogon to construct a Textual Select
appropriately.
I've been using the workaround above which feels pretty natural in practice now; I'm not sure there's a clear ask from the Trogon side of the fence on it, at this point. The only thing I can think of would be that Trogon could call .value
if it detects that the object in question is an Enum
, but that isn't guaranteed to be a string either.
Whereas Click recommends using
click.Choice(["one", "two", ...])
, Typer recommends using something like the following:This all works great in Typer land, and Trogon will start up okay. But once one navigates to the
some_command
command in the Trogon TUI, it currently spits out an exception like the following:This appears to occur specifically when one of the enum choices is used as a default. If the Enum is used to restrict a required argument, or an option where the default is e.g.
None
, Trogon works as expected.I understand that one would need to inject a
.value
there somewhere to get at the actual string that the Enum is referencing, which I imagine is fully within Trogon's control to be doing.I haven't looked at the parameter conversion code base, so I'm fully willing to hear that knowing about Enums and doing something special with them is against Trogon's design!
Here is a small reproduction if that is helpful.