]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-148468: Accept string-like proxy objects in colorized argparse help (#154653)
authorSavannah Ostrowski <savannah@python.org>
Thu, 30 Jul 2026 15:59:59 +0000 (08:59 -0700)
committerGitHub <noreply@github.com>
Thu, 30 Jul 2026 15:59:59 +0000 (15:59 +0000)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Lib/argparse.py
Lib/test/test_argparse.py
Misc/NEWS.d/next/Library/2026-07-24-21-49-45.gh-issue-148468.pHoF_Q.rst [new file with mode: 0644]

index f8739d031e01c58e2f54b0befb154ec861456b08..0840c4c45cffaa2d3ae83cfb22ac7b74621384dd 100644 (file)
@@ -713,7 +713,7 @@ class HelpFormatter(object):
         return result
 
     def _expand_help(self, action):
-        help_string = self._get_help_string(action)
+        help_string = str(self._get_help_string(action))
         if '%' not in help_string:
             return self._apply_text_markup(help_string)
         params = dict(vars(action), prog=self._prog)
index 442cbb1aaadfe3f85e870f84302b213e691ee69e..287fcb7a084a49942eb188bba27beef612c79b04 100644 (file)
@@ -7898,6 +7898,41 @@ class TestColorized(TestCase):
         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)"),
+        )
+        parser.add_argument(
+            "--baz",
+            help=LazyStr("baz plain text"),
+        )
+
+        interp = self.theme.interpolated_value
+        reset = self.theme.reset
+
+        help_text = parser.format_help()
+        self.assertIn(f"foo (default: {interp}bar{reset})", help_text)
+        self.assertIn("baz plain text", 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)
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 (file)
index 0000000..d1d0deb
--- /dev/null
@@ -0,0 +1 @@
+Fix a regression in :mod:`argparse` where colorized argument help containing format specifiers did not accept string-like proxy objects.