Skip to content
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

Parse arguments based on type hints in the corresponding function signature #327

Open
dhimmel opened this issue Mar 19, 2021 · 1 comment

Comments

@dhimmel
Copy link

dhimmel commented Mar 19, 2021

add_argument from argparse has a type option that "allows any necessary type-checking and type conversions to be performed". The ability to ensure the CLI args are converted to certain types helps ensure consistent CLI behavior and avoid bugs.

Since fire strives to be as automated as possible, it seems natural that it could inspect the type hints for the function signature that is being fired and cast the arguments to these types. If the arguments can't be converted to the type signature, an error would be raised to alert the CLI user to the invalid argument.

The docs are clear that this is not the current design:

The types of the arguments are determined by their values, rather than by the function signature where they're used.

But the convenience of relying on type hints for argument type conversion would be major. It could be implemented in an opt-in manner for back compat. Would this idea ever be considered? Are there any CLI projects that are already doing this?

See also the related issue at #260.

@zqqqqz2000
Copy link

You can try afire, which is a fork of python-fire supporing type convert according to the type hint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants