From: Savannah Ostrowski Date: Sun, 7 Dec 2025 21:02:12 +0000 (-0800) Subject: GH-139862: Fix direct instantiation of `HelpFormatter` (#142384) X-Git-Tag: v3.15.0a3~169 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dc9f2385ed528caf4ab02965b6d3e16b8a72db25;p=thirdparty%2FPython%2Fcpython.git GH-139862: Fix direct instantiation of `HelpFormatter` (#142384) --- diff --git a/Lib/argparse.py b/Lib/argparse.py index 6ed7386d0dd4..398825508f59 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -189,6 +189,8 @@ class HelpFormatter(object): self._whitespace_matcher = _re.compile(r'\s+', _re.ASCII) self._long_break_matcher = _re.compile(r'\n\n\n+') + self._set_color(False) + def _set_color(self, color): from _colorize import can_colorize, decolor, get_theme diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index fe5232e9f3b5..7c5eed21219d 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -5694,6 +5694,11 @@ class TestHelpCustomHelpFormatter(TestCase): a-very-long-command command that does something ''')) + def test_direct_formatter_instantiation(self): + formatter = argparse.HelpFormatter(prog="program") + formatter.add_usage(usage=None, actions=[], groups=[]) + help_text = formatter.format_help() + self.assertEqual(help_text, "usage: program\n") # ===================================== # Optional/Positional constructor tests