python-fire icon indicating copy to clipboard operation
python-fire copied to clipboard

mis-assigned keyword arguments when two function parameters have the same name

Open CrepeGoat opened this issue 3 years ago • 6 comments

Environment

Python 3.9.2 fire==0.4.0

Example Script

I have the following example script.py:

import fire

class Foo:
    def __init__(self):
        print("__init__")

    def bar(self, param=0):
        print(f"bar (param={param})")
        return self

    def baz(self, param=0):
        print(f"baz (param={param})")
        return self

    def end(self):
        return "end"


if __name__ == "__main__":
    fire.Fire(Foo)

Example Usages (which demonstrate a problem)

When I use the script and provide the arguments by position, it works fine:

$ python script.py bar 3 baz 5 end
__init__
bar (param=3)
baz (param=5)
end

But when I provide the arguments by keyword, the output is different and the program brings up the command-line help text for Foo:

$ python script.py bar --param=3 baz --param=5 end
__init__
bar (param=5)
baz (param=end)

More examples:

$ python script.py bar --param=3 baz 5 end
__init__
bar (param=3)
baz (param=5)
end
$ python script.py bar 3 baz --param=5 end
__init__
bar (param=5)
ERROR: Could not consume arg: 3
Usage: fire_issue_eg.py bar 3 <command>
  available commands:    bar | baz | end

For detailed information on this command, run:
  fire_issue_eg.py bar 3 --help

Thoughts

It looks like there is a problem:

  • when Fire is running through two (or more) callable objects, and each object has an identically-named parameter (in the above example, param)
  • when that parameter is provided on the command line via keyword on the 2nd(+) function called (in this example, baz)

CrepeGoat avatar Jan 13 '22 23:01 CrepeGoat

Hello,

Can I be assigned to this issue? I really want to work on this issue and contribute to python fire.

Thank you Rishav

rishav-karanjit avatar Jan 23 '22 05:01 rishav-karanjit

Thanks for reporting this.

As a workaround for python script.py bar --param=3 baz --param=5 end you should be able to run python script.py bar --param=3 - baz --param=5 end. The separator will prevent the second --param from being applied to the bar function.

Yes, @rishav-karanjit, you can work on this. Thanks for offering to contribute.

dbieber avatar Jan 29 '22 02:01 dbieber

Working on this

ordas21 avatar Apr 29 '22 03:04 ordas21

Hi @dbieber it looks like this is still open, can I work on it?

Xceptions avatar Feb 05 '24 08:02 Xceptions

Yes, you can work on this.

dbieber avatar Feb 05 '24 11:02 dbieber

ok thanks. On it!

Xceptions avatar Feb 05 '24 12:02 Xceptions