google / python-fire

Python Fire is a library for automatically generating command line interfaces (CLIs) from absolutely any Python object.
Other
26.66k stars 1.44k forks source link

Cannot parse list of strings containing `is` #481

Closed rentruewang closed 1 month ago

rentruewang commented 6 months ago

Reproduce as such:

# example.py
import fire
def main(hello: list[str]):
    print(hello)
fire.Fire(main)
python example.py --hello '[this,is,nice]'

This would yield the unexpected output where in the main function hello would be a string "[this,is,nice]". The correct behavior should be ["this", "is", "nice"]

This doesn't happen with non-keywords.

dbieber commented 5 months ago

Good catch. As a workaround for the moment, put quotes around "is".

python example.py --hello '[this,"is",nice]'
['this', 'is', 'nice']
DavidKatz-il commented 4 months ago

This behavior comes from the ast.parse here

ajitg25 commented 3 months ago

I am trying to understand the issue here @dbieber. When I am trying to get the elements from the list for example

import fire
def main(hello: list[str]):
    for i in hello:
        print(i)
fire.Fire(main)

The output is:

[
t
h
i
s
,
i
s
,
n
i
c
e
]

isnt it should be like:

this
is
nice

I want to fix this issue so it will be helpful is you could explain a bit.

rentruewang commented 3 months ago

@ajitg25 My guess is because fire would try to convert "2.5" in the command line to 2.5 by calling python's built in eval during parsing. Therefore, keywords not in quotes would cause SyntaxError probably for eval. This is a good behavior for numbers like 2.5, not so much for "is". That is why I raised the issue.

ajitg25 commented 2 months ago

Please assign this issue to me.