-
-
Notifications
You must be signed in to change notification settings - Fork 671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Must use typer.Option to supply default argument with Enums created with functional API #389
Comments
Funny I just bumped into this today. Would love to see Typer fully handle Enums created with the Functional API, but I think this might be a Python limitation. Maybe the fix/improvement is to make it known that in order to use Enums defined with the functional API:
If this is brought up in the documentation with an example, I'm thinking folks should understand how it works and be ok with the limitations. |
Here's a good example that worked for me: having as an input parameter, the log level: LogLevel = StrEnum('LogLevel', {k: k for k in logging._levelToName.values()}) # noqa
@app.command()
def main(
log_level: LogLevel = typer.Option('INFO'),
)
# `str(...)` is not needed, but adding for clarity and avoid linters complaining
logging.basicConfig(level=str(log_level)) When doing |
I agree - this isn't a major issue, but it would be helpful to have it documented |
Not sure if I'm not off topic, but: what about this example with arguments instead of option ? #!/usr/bin/env python3
from enum import Enum
import typer
NeuralNetwork = Enum("NeuralNetwork", {k: k for k in ["simple", "conv", "lstm"]})
def main(network: NeuralNetwork = typer.Argument("simple", case_sensitive=False)):
typer.echo(f"Training neural network of type: {network.value}")
if __name__ == "__main__":
typer.run(main) When I run the help usage, this is what it shows:
The help usage for the argument is a bit weird, and it does not do it for options. How do you fix the help usage for Arguments (not Options) ? def main(network: NeuralNetwork = typer.Argument(NeuralNetwork.simple.value, case_sensitive=False)): Because if used without the value: def main(network: NeuralNetwork = typer.Argument(NeuralNetwork.simple, case_sensitive=False)): This give you the following help message:
I think those behaviors should be documented as well, but I'm not sure to fully understand the rational behind this behavior. |
Hmm I might be misunderstanding, sorry if I'm missing something, but this idea doesn't seem to work: #!/usr/bin/env python3
from enum import Enum
from typing import Annotated
import typer
from typer import Option
class Color(Enum):
RED = 'red'
GREEN = 'green'
def main(color: Annotated[Color, Option(help='the color')] = 'red') -> None:
print(color)
if __name__ == '__main__':
typer.run(main) But that's a Mypy violation. Passing #!/usr/bin/env python3
from enum import Enum
from typing import Annotated
import typer
from typer import Option
class Color(Enum):
RED = 'red'
GREEN = 'green'
def main(color: Annotated[Color, Option(help='the color')] = Option(Color.RED)) -> None:
print(color)
if __name__ == '__main__':
typer.run(main) Running this gives: MixedAnnotatedAndDefaultStyleError: Cannot specify `Option` in `Annotated` and default value together for 'color' |
First Check
Commit to Help
Example Code
Description
When an enum is created via the functional API (as above), it cannot be used as a choice constraint, because it won't also subclass
str
.In the documentation, choices are created by subclassing both
str
andenum.Enum
. For example:However, as far as I know, this isn't possible when using the functional API to create enums.
This can be worked around by supplying
typer.Option
with the default argument instead:While the workaround is very simple and non-obstructive, it's not clear from the docs that this is the case.
Operating System
macOS
Operating System Details
No response
Typer Version
0.4.1
Python Version
3.7.2
Additional Context
No response
The text was updated successfully, but these errors were encountered: