-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
GH-142950: Process format specifiers before colourization in argparse help #142960
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
base: main
Are you sure you want to change the base?
GH-142950: Process format specifiers before colourization in argparse help #142960
Conversation
|
Thanks for working on this and fixing the bug I introduced 😭 (I was planning to fix it today but saw your PR). # parser.add_argument("--str", default="test", help="100%%")
#
Traceback (most recent call last):
File "/Users/aprengere/Dev/cpython/Lib/argparse.py", line 1810, in _check_help
formatter._expand_help(action)
~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^
File "/Users/aprengere/Dev/cpython/Lib/argparse.py", line 712, in _expand_help
raise ValueError(f"invalid format specifier in: {help_string!r}")
ValueError: invalid format specifier in: '100%%'This is due to final check: Removing this check it makes |
|
|
||
| if not t.reset: | ||
| return help_string % params | ||
|
|
||
| # Format first to preserve types for specifiers, like %x that require int. | ||
| def colorize(match): | ||
| spec, name = match.group(0, 1) | ||
| if spec == '%%': | ||
| return '%' | ||
| if name in params: | ||
| formatted = spec % {name: params[name]} | ||
| return f'{t.interpolated_value}{formatted}{t.reset}' | ||
| return spec | ||
|
|
||
| # Match %% or %(name)... format specifiers | ||
| result = _re.sub(r'%%|%\((\w+)\)[^a-z]*[a-z]', colorize, | ||
| help_string, flags=_re.IGNORECASE) | ||
|
|
||
| if '%' in result: | ||
| raise ValueError(f"invalid format specifier in: {help_string!r}") | ||
| return result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also hit the issue linked here in my nightly tests.
From a look at this fix, it looks like it would not handle all edge cases of % formatting. In addition to the case mentioned above #142960 (comment), the regex used here doesn't handle length modifiers. Here is a modified patch that handles both issues and uses a regex that emulated Python's % formatting as described here
| if not t.reset: | |
| return help_string % params | |
| # Format first to preserve types for specifiers, like %x that require int. | |
| def colorize(match): | |
| spec, name = match.group(0, 1) | |
| if spec == '%%': | |
| return '%' | |
| if name in params: | |
| formatted = spec % {name: params[name]} | |
| return f'{t.interpolated_value}{formatted}{t.reset}' | |
| return spec | |
| # Match %% or %(name)... format specifiers | |
| result = _re.sub(r'%%|%\((\w+)\)[^a-z]*[a-z]', colorize, | |
| help_string, flags=_re.IGNORECASE) | |
| if '%' in result: | |
| raise ValueError(f"invalid format specifier in: {help_string!r}") | |
| return result | |
| result = help_string % params # always format the whole string to raise on invalid input | |
| if not t.reset: | |
| return result | |
| # Format first to preserve types for specifiers, like %x that require int. | |
| def colorize(match): | |
| spec, name = match.group(0, 'mapping') | |
| if spec == '%%': | |
| return '%' | |
| if name in params: | |
| formatted = spec % {name: params[name]} | |
| return f'{t.interpolated_value}{formatted}{t.reset}' | |
| return spec | |
| # Match %% or %(name)... format specifiers | |
| result = _re.sub( | |
| r""" | |
| % # Percent character | |
| (?:\((?P<mapping>[^)]*)\))? # Mapping key | |
| (?P<flag>[#0\-+ ])? # Conversion Flags | |
| (?P<width>\*|\d+)? # Minimum field width | |
| (?P<precision>\.(?:\*?|\d*))? # Precision | |
| [hlL]? # Length modifier (ignored) | |
| (?P<format>[diouxXeEfFgGcrsa%]) # Conversion type | |
| """, | |
| colorize, | |
| help_string, | |
| flags=_re.VERBOSE, | |
| ) | |
| return result |
Uh oh!
There was an error while loading. Please reload this page.