-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Unhandled arguments checked after execution, not before #168
Comments
Sorry to hear you're hitting this issue. Unfortunately, there's not an obvious fix: Brainstorming possible workarounds:
|
Thanks for a quick response @dbieber , I didn't realize that the chaining feature has these consequences, good to know that. |
Hey @lopuhin, I wrote the decorator. I've pulled it into a gist here: https://gist.github.com/trhodeos/5a20b438480c880f7e15f08987bd9c0f. |
It's worth noting that this issue has hit 3 users of https://github.com/openai/gpt-2/ and those are just the ones I personally know of. EDIT: 4th user. |
Thanks for the feedback. We may be able to fix this after all. |
@dbieber Is there any way to explicitly turn off chaining? This should solve this problem, too, right? |
Hi everyone, how can we help push this forward? The inability to check whether the arguments provided are correct is definitely a large drawback to what otherwise is an awesome framework. I would say I'd prefer explicit chaining personally. The obvious default behavior would be for a CLI to fail if the signature is wrong. |
Thanks for the interest. The change we're considering is to require explicit chaining (using a separator, which is "-" by default) when not all the arguments are received, and to only allow implicit chaining when all arguments have values. No one is actively working on this. One implications of this change would be that functions that accept *args or **kwargs would always require explicit chaining. If you want to help, some things you could do are:
How would the implementation work? Roughly, it would be something like this: In the main while loop Line 425 in 3be260e
There are two places where we dispatch function calls to user code: Line 463 in 3be260e
and Line 553 in 3be260e
_CallAndUpdateTrace uses parse to determine which arguments to use to call the user function, and which arguments will remain. parse is defined hereLine 670 in 3be260e
The user function is called here Line 672 in 3be260e
It's at this point (before the call of if fn has optional args that don't have values specified in varargs and kwargs and remaining_args is not empty:
raise FireError('An error message here saying how the user probably specified the args wrong, or maybe they just want chaining, and if they want chaining they should use a separator explicitly') |
In case this interests some of you, here's a fire fork which is strict by default, meant as a temporary fix: |
Just checking if there is any interest in addressing this. I understand the chaining concern, but I feel like the ability to just pass in a Honestly most of my usage of fire is as follows and I can see it is the case for most other folks in the internet too.
to make CLIs easy to work with. Which would then become:
The fork here https://github.com/danieldugas/python-strict-fire from @danieldugas shows that the change is indeed not that large |
For anybody else hitting this and frustrated that For the common use-case it's a drop in replacement:
Plus it uses type-hints to you don't have to cast everything from a string. |
For those of you who are looking for this feature but who cannot use typer, I found this is a simple workaround (using kwargs). def main(wanted:str, **kwargs):
if len(kwargs) > 0:
print("Unknown options: ", kwargs)
return
print("wanted : ", wanted)
if __name__ == "__main__":
fire.Fire(main) |
This |
Chaining seems like a very niche feature, error-checking the arguments that the user types in seems like a top-3 feature for any CLI interface library. This issue is a dealbreaker for using Fire. @hponde's proposal seems great. |
It would be valuable to support It is much preferred to parse bool-like input rather than force users to use argparse's --option and --no-option pattern. Since the strict flag is entirely optional, and users would explicitly need to enable it to prevent chaining, you could invert the logic to say Fire(..., chain=True) as default and set chain=False to have strict arg parsing. For now, the fork will suffice but it's not an ideal solution |
Consider a simple program:
And then suppose we make a typo in the argument name, writing
--zerro
instead of--zero
. This is what I get with fire 0.1.3 under Python 3.6:Notice that first we run the code, and only then the error is reported. While I expected the errors to be checked before any user code is executed, because this code could be working for a long time, doing wrong things, etc.
The text was updated successfully, but these errors were encountered: