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

Failure to recognize negative scientific notation (with nargs:2) #55

Closed
hpaulj opened this issue Mar 14, 2013 · 5 comments
Closed

Failure to recognize negative scientific notation (with nargs:2) #55

hpaulj opened this issue Mar 14, 2013 · 5 comments

Comments

@hpaulj
Copy link

hpaulj commented Mar 14, 2013

On stackoverflow I came across an argparse.py issue that also affects this version. It has to do with recognizing negative numbers like -2e4. The negativeNumberMatcher does not handle scientific notation.

However the issue only rises in special circumstances such as a nargs:2 case, and there are work arounds.

ArgumentParser = require('argparse').ArgumentParser

parser = new ArgumentParser({debug:true});
parser.addArgument(['--xlim'], {nargs: 2, type: 'float'});

args = parser.parseArgs(['--xlim', '-.002', '1e4']);
console.log(args);
// { xlim: [ -0.002, 10000 ] }

args = parser.parseArgs(['--xlim', '-2e3', '1e4'])
// TypeError: argument "--xlim": Expected 2 argument(s)

http://stackoverflow.com/questions/9025204/python-argparse-issue-with-optional-arguments-which-are-negative-numbers
http://bugs.python.org/issue9334
may be a related issue (parsing ['-a','-one','-two'] where (['-a',{nargs:2}). That is, should '-one' be interpreted as an optional, or as an argument to '-a'?

@hpaulj
Copy link
Author

hpaulj commented Apr 14, 2013

I submitted a patch to Python issue 9334 to

  • accept negative scientific and complex numbers
  • add the args_default_to_positional parser option

http://bugs.python.org/issue9334#msg184987

@trebor89 trebor89 mentioned this issue May 12, 2013
@puzrin puzrin closed this as completed in 5e94ebe May 12, 2013
@puzrin
Copy link
Member

puzrin commented May 12, 2013

@hpaulj negative sentific floats now works. This fix does not cover complex numbers and values with leading dashes. I think, this can be safely postponed, until someone really needs it in real life.

@hpaulj
Copy link
Author

hpaulj commented May 12, 2013

I explored expanding the regex expression like this. Adding the exponential is easy. Adding complex makes the whole expression unwieldy. In the Python patch I chose instead to separate the two uses of _negativeNumberMatcher.

For identifying negative like option strings I left the original matcher (examples are all simple ones like '-1', not even '-1.23', much less '-1.23e4'.)

For identifying real negative numbers, I chose instead to let Python's own number parser do the job:

 if not self._has_negative_number_optionals:
     try:
         complex(arg_string)
         return None
 except ValueError:
        pass

The javascript equivalent could use !isNaN(argString)

if (!_.any(this._hasNegativeNumberOptionals) && !isNaN(argString)) {
  return null;
}

But since javascript does not have complex number type, expanding the regex to include exponentials should be just fine.

I've submitted a number of python argparse patches, but haven't gotten much feedback from their developers.

@puzrin
Copy link
Member

puzrin commented May 12, 2013

Yes, isNaN would be more simple. I didn't digged into algorythm, and just replaced existing regexp, after trebor89 pointed to bad line of code :) .

By the way, may be you have time to look into mincer project https://github.com/nodeca/mincer ? It has much more space for improvements, than argparse. I think, even in current state, argparse cover node developer's needs for 200% :) .

@rlidwka
Copy link
Member

rlidwka commented Aug 14, 2020

Argparse 2.0 was ported from scratch based on python 3.9.0. We aimed to keep source code as close as possible to the original, so this change didn't make it in.

Is it used by anybody? If so, maybe bug python developers again, issue still not resolved over there.

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

3 participants