Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,11 +688,29 @@ def _expand_help(self, action):
params[name] = value.__name__
if params.get('choices') is not None:
params['choices'] = ', '.join(map(str, params['choices']))
# Before interpolating, wrap the values with color codes

t = self._theme
for name, value in params.items():
params[name] = f"{t.interpolated_value}{value}{t.reset}"
return help_string % params

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
Comment on lines +693 to +713
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


def _iter_indented_subactions(self, action):
try:
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -7663,6 +7663,22 @@ def test_backtick_markup_special_regex_chars(self):
help_text = parser.format_help()
self.assertIn(f'{prog_extra}grep "foo.*bar" | sort{reset}', help_text)

def test_help_with_format_specifiers(self):
# GH-142950: format specifiers like %x should work with color=True
parser = argparse.ArgumentParser(prog='PROG', color=True)
parser.add_argument('--hex', type=int, default=255,
help='hex: %(default)x')
parser.add_argument('--str', default='test',
help='str: %(default)s')

help_text = parser.format_help()

interp = self.theme.interpolated_value
reset = self.theme.reset

self.assertIn(f'hex: {interp}ff{reset}', help_text)
self.assertIn(f'str: {interp}test{reset}', help_text)

def test_print_help_uses_target_file_for_color_decision(self):
parser = argparse.ArgumentParser(prog='PROG', color=True)
parser.add_argument('--opt')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix regression in :mod:`argparse` where format specifiers in help strings raised :exc:`ValueError`.
Loading