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

Cannot parse list of strings containing `is`

Open rentruewang opened this issue 1 year ago • 5 comments

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.

rentruewang avatar Jan 27 '24 23:01 rentruewang

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

python example.py --hello '[this,"is",nice]'
['this', 'is', 'nice']

dbieber avatar Feb 24 '24 16:02 dbieber

This behavior comes from the ast.parse here

DavidKatz-il avatar Mar 28 '24 14:03 DavidKatz-il

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.

ajitg25 avatar Apr 18 '24 17:04 ajitg25

@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.

rentruewang avatar Apr 18 '24 19:04 rentruewang

Please assign this issue to me.

ajitg25 avatar May 01 '24 17:05 ajitg25