Skip to content

Commit

Permalink
gh-105712: Use appropriate regex to detect negative numbers in arguments
Browse files Browse the repository at this point in the history
previously, type=float values like -3e12 would be misinterpreted as -option

Signed-off-by: Marcus Müller <[email protected]>
  • Loading branch information
marcusmueller committed Feb 1, 2024
1 parent a79a272 commit f683f09
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1399,7 +1399,7 @@ def __init__(self,
self._defaults = {}

# determines whether an "option" looks like a negative number
self._negative_number_matcher = _re.compile(r'^-\d+$|^-\d*\.\d+$')
self._negative_number_matcher = _re.compile(r'^-\d+$|^-\d*\.\d+$|^-\d*\.?\d+e[+-]?\d+$')

# whether or not there are any optionals that look like negative
# numbers -- uses a list so it can be shared and edited
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Fix the issue that::

parser.add_argument("-p", "--param", type=float)
args = parser.parse_args()

fails to correctly deal with `-p -3e12`, thinking that `-3e12` is an option,
not a parameter to one. That's because the old regex was too restrictive.
This fixes that with no found downsides.

0 comments on commit f683f09

Please sign in to comment.