Skip to content

Conversation

@savannahostrowski
Copy link
Member

@savannahostrowski savannahostrowski commented Dec 18, 2025

@alexprengere
Copy link
Contributor

Thanks for working on this and fixing the bug I introduced 😭 (I was planning to fix it today but saw your PR).
One minor nit: I tested it locally and this will raise when %% is encountered in the help text:

# 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:

       if '%' in result:
            raise ValueError(f"invalid format specifier in: {help_string!r}")

Removing this check it makes test_invalid_help fail though.

Comment on lines +693 to +713

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
Copy link
Contributor

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

Suggested change
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants