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

How do I preserve underscores in arguments? #116

Open
MattKleinsmith opened this issue Feb 17, 2018 · 10 comments
Open

How do I preserve underscores in arguments? #116

MattKleinsmith opened this issue Feb 17, 2018 · 10 comments

Comments

@MattKleinsmith
Copy link

Here's run.py:

import fire

def f(x):
    print(x)

if __name__ == '__main__':
    fire.Fire(f)

Command:
python run.py --x 20180217_064501

Output:

20180217064501

Expected output:

20180217_064501

@MattKleinsmith
Copy link
Author

These issues seem related:
#102
#97

@dbieber
Copy link
Member

dbieber commented Feb 19, 2018

I'm not able to reproduce this issue.
When I run python run.py --x 20180217_064501 I get the expected output 20180217_064501.

What version of Python are you using? What version of fire?

@MattKleinsmith
Copy link
Author

Python 3.6.3
fire 0.1.2, installed via pip
OS: Ubuntu 16.04 on Docker and host

I should have included this information in the original post. My apologies.

@MattKleinsmith
Copy link
Author

MattKleinsmith commented Feb 19, 2018

With this Dockerfile I get the expected output:

FROM ubuntu:16.04

RUN apt-get update --fix-missing && apt-get install -y \
    python3 \
    python3-pip \
    vim \
    wget

RUN pip3 install fire

RUN wget https://raw.githubusercontent.com/MattKleinsmith/dockerfiles/master/errors/fire/run.py

CMD ["python3", "run.py", "--x", "20180217_064501"]

@MattKleinsmith
Copy link
Author

When I install Anaconda and use its provided pip to install fire, I get the erroneous output.

Here's a Dockerfile reproducing the issue.

@frextrite
Copy link

Tested with anaconda and python 2.7.15, I was getting the expected output. Then tested with anaconda and python 3.5.3(latest version available on Ubuntu 16.04, dockerfile uses 3.5.2) and again got the expected output. Then tested with anaconda and python 3.6.5 and got the erroneous output. Maybe the problem is with python 3.6+

@JadeIden
Copy link

It looks like an issue with PEP 515 and underscores in numbers, which was implemented in Python 3.6. It seems like underscores between numbers are more likely to be used as part of a string rather than for a long number, so it could be worthwhile to, when we encounter a number with an underscore in it during traversal, treat it as a string instead.

I can take a shot at implementing that if that is the chosen solution.

@kyle-goodwin
Copy link

@JadeIden Do you still feel like implementing something? I tracked down where this happens but implementing a fix for this is well beyond me.

This is caused by line 96 of parser.py in the _LiteralEval function. Specifically root = ast.parse(value, mode='eval').

This can be demonstrated with the following code:

import ast
root = ast.parse("123_456", mode='eval')
print(root.body.value)

prints 123456.

@farridav
Copy link

*bump, this is also affecting me, any news on a workaround ? why not inspect the type annotations (if present) and use that to bias the type ?

@dbieber
Copy link
Member

dbieber commented Jan 22, 2024

One (poor :() workaround for the moment is to surround your argument with quotes... twice...

python run.py --x '"20180217_064501"'

The outer quotes get stripped by your shell, leaving just the inner quotes to be parsed by Python Fire, resulting in the arg getting treated as a string rather than a number.

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

6 participants