[3.14] GH-139809: Fix argparse subcommand prog not respecting color environment variables (GH-139818) (#139866)

GH-139809: Fix argparse subcommand prog not respecting color environment variables (GH-139818)
(cherry picked from commit 9fc4366f09)

Co-authored-by: Savannah Ostrowski <savannah@python.org>
This commit is contained in:
Miss Islington (bot) 2025-10-09 19:19:54 +02:00 committed by GitHub
parent 2cc13ff304
commit af1cb1cdb8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 18 additions and 2 deletions

View file

@ -22,6 +22,7 @@
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 @@ def __init__(self, prog):
{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.