cpython icon indicating copy to clipboard operation
cpython copied to clipboard

GH-142950: Process format specifiers before colourization in argparse help

Open savannahostrowski opened this issue 2 months ago • 2 comments

  • Issue: gh-142950

savannahostrowski avatar Dec 18 '25 22:12 savannahostrowski

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 the 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.

alexprengere avatar Dec 19 '25 18:12 alexprengere

Hey folks - thanks for the thoughtful and thorough feedback on this PR! Apologies for the delay in addressing it - I was out on vacation over the holidays!

savannahostrowski avatar Jan 04 '26 18:01 savannahostrowski