]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] GH-139809: Fix argparse subcommand prog not respecting color environment varia...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 9 Oct 2025 17:19:54 +0000 (19:19 +0200)
committerGitHub <noreply@github.com>
Thu, 9 Oct 2025 17:19:54 +0000 (17:19 +0000)
GH-139809: Fix argparse subcommand prog not respecting color environment variables (GH-139818)
(cherry picked from commit 9fc4366f09904fd9aac797d542e332e8a4c1a921)

Co-authored-by: Savannah Ostrowski <savannah@python.org>
Lib/argparse.py
Lib/test/test_argparse.py
Misc/NEWS.d/next/Library/2025-10-09-03-06-19.gh-issue-139809.lzHJNu.rst [new file with mode: 0644]

index 01d30a278496fcfc6ebfe91c8b298527a02e754b..88c1f5a7ef334271b2851f0dc32bf849efb36457 100644 (file)
@@ -1960,7 +1960,9 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
         # prog defaults to the usage message of this parser, skipping
         # optional arguments and with no "usage:" prefix
         if kwargs.get('prog') is None:
-            formatter = self._get_formatter()
+            # Create formatter without color to avoid storing ANSI codes in prog
+            formatter = self.formatter_class(prog=self.prog)
+            formatter._set_color(False)
             positionals = self._get_positional_actions()
             groups = self._mutually_exclusive_groups
             formatter.add_usage(None, positionals, groups, '')
index 7de1d3e2373c33e6d6be5117eab31de62c33c333..af7cbcafcbea25f0839e92d700b1ff946a0e3112 100644 (file)
@@ -22,6 +22,7 @@ from test.support import (
     captured_stderr,
     force_not_colorized,
     force_not_colorized_test_class,
+    swap_attr,
 )
 from test.support import import_helper
 from test.support import os_helper
@@ -7128,7 +7129,8 @@ class TestColorized(TestCase):
     def setUp(self):
         super().setUp()
         # Ensure color even if ran with NO_COLOR=1
-        _colorize.can_colorize = lambda *args, **kwargs: True
+        self.enterContext(swap_attr(_colorize, 'can_colorize',
+                                     lambda *args, **kwargs: True))
         self.theme = _colorize.get_theme(force_color=True).argparse
 
     def test_argparse_color(self):
@@ -7355,6 +7357,17 @@ class TestColorized(TestCase):
                  {short_b}+f{reset}, {long_b}++foo{reset} {label_b}FOO{reset}  foo help
         '''))
 
+    def test_subparser_prog_is_stored_without_color(self):
+        parser = argparse.ArgumentParser(prog='complex', color=True)
+        sub = parser.add_subparsers(dest='command')
+        demo_parser = sub.add_parser('demo')
+
+        self.assertNotIn('\x1b[', demo_parser.prog)
+
+        demo_parser.color = False
+        help_text = demo_parser.format_help()
+        self.assertNotIn('\x1b[', help_text)
+
 
 def tearDownModule():
     # Remove global references to avoid looking like we have refleaks.
diff --git a/Misc/NEWS.d/next/Library/2025-10-09-03-06-19.gh-issue-139809.lzHJNu.rst b/Misc/NEWS.d/next/Library/2025-10-09-03-06-19.gh-issue-139809.lzHJNu.rst
new file mode 100644 (file)
index 0000000..498b8f7
--- /dev/null
@@ -0,0 +1 @@
+Prevent premature colorization of subparser ``prog`` in :meth:`argparse.ArgumentParser.add_subparsers` to respect color environment variable changes after parser creation.