From 78965e1ae2c80f92d403aa891e81a6de92d47c62 Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Fri, 24 Jul 2026 14:45:28 -0700 Subject: [PATCH 1/2] Accept string-like proxy objects in colorized argparse help --- Lib/argparse.py | 2 +- Lib/test/test_argparse.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/Lib/argparse.py b/Lib/argparse.py index f8739d031e01c5..c87ac7e34b0c4d 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -760,7 +760,7 @@ def colorize(match): return spec % params return self._apply_text_markup( - _re.sub(fmt_spec, colorize, help_string, flags=_re.VERBOSE) + _re.sub(fmt_spec, colorize, str(help_string), flags=_re.VERBOSE) ) def _iter_indented_subactions(self, action): diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 442cbb1aaadfe3..edd145382a4a14 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -7898,6 +7898,36 @@ def test_backtick_markup_in_argument_help_color_disabled(self): self.assertIn("set the `foo` value", help_text) self.assertNotIn("\x1b[", help_text) + def test_argument_help_interpolation_accepts_string_like_proxy(self): + class LazyStr: + def __init__(self, message): + self._message = message + + def __str__(self): + return self._message + + def __getattr__(self, name): + return getattr(str(self), name) + + def __contains__(self, item): + return item in self._message + + def __mod__(self, other): + return self._message % other + + parser = argparse.ArgumentParser(prog="PROG", color=True) + parser.add_argument( + "--foo", + default="bar", + help=LazyStr("foo (default: %(default)s)"), + ) + + interp = self.theme.interpolated_value + reset = self.theme.reset + + help_text = parser.format_help() + self.assertIn(f"foo (default: {interp}bar{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) From c9ca9a7c35b9445e3d8a368e4f680af87bdca431 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 24 Jul 2026 21:49:50 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2026-07-24-21-49-45.gh-issue-148468.pHoF_Q.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2026-07-24-21-49-45.gh-issue-148468.pHoF_Q.rst diff --git a/Misc/NEWS.d/next/Library/2026-07-24-21-49-45.gh-issue-148468.pHoF_Q.rst b/Misc/NEWS.d/next/Library/2026-07-24-21-49-45.gh-issue-148468.pHoF_Q.rst new file mode 100644 index 00000000000000..d1d0deb139bc20 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-24-21-49-45.gh-issue-148468.pHoF_Q.rst @@ -0,0 +1 @@ +Fix a regression in :mod:`argparse` where colorized argument help containing format specifiers did not accept string-like proxy objects.