Skip to content

Argparse print_help() and print_usage() do not color based on their file argument #156144

Closed

Description

@bulletmark

Bug report

Bug description:

Currently (in Python 3.14), both argparse.print_help() and argparse.print_usage() merely check whether stdout is eligible for coloring but both take a file argument and both should be checking against that instead of stdout. E.g. my app's stdout is attached to a file but I need sometimes to write my help/usage to the screen (e.g. via /dev/tty) and presently this is output without color although it can be (and looks much better!).

I made a very trivial patch to 3.14 argparse.py to fix this and was going to submit that as PR but there have been many recent changes to the same code so I decided just to raise this issue and attach my patch here.

No user or public facing API is changed so no documentation requires update.

Note also that no AI was used here, all implemented and tested by hand.

CPython versions tested on:

3.14

Operating systems tested on:

Arch Linux - up to date.

Activity

  1. bulletmark commented on Aug 21, 2026

    @bulletmark
    Author

    The trivial patch against Python 3.14 to fix this:

    diff --git a/argparse.py b/argparse.py
    index a106a71..721e658 100644
    --- a/worktree_aid/argparse.py
    +++ b/worktree_aid/argparse.py
    @@ -196,10 +196,10 @@ class HelpFormatter(object):
             self._whitespace_matcher = _re.compile(r'\s+', _re.ASCII)
             self._long_break_matcher = _re.compile(r'\n\n\n+')
     
    -    def _set_color(self, color):
    +    def _set_color(self, color, file=None):
             from _colorize import can_colorize, decolor, get_theme
     
    -        if color and can_colorize():
    +        if color and can_colorize(file=file):
                 self._theme = get_theme(force_color=True).argparse
                 self._decolor = decolor
             else:
    @@ -2699,14 +2699,17 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
         # Help-formatting methods
         # =======================
     
    -    def format_usage(self):
    -        formatter = self._get_formatter()
    +    def _format_usage(self, file=None):
    +        formatter = self._get_formatter(file)
             formatter.add_usage(self.usage, self._actions,
                                 self._mutually_exclusive_groups)
             return formatter.format_help()
     
    -    def format_help(self):
    -        formatter = self._get_formatter()
    +    def format_usage(self):
    +        return self._format_usage()
    +
    +    def _format_help(self, file=None):
    +        formatter = self._get_formatter(file)
     
             # usage
             formatter.add_usage(self.usage, self._actions,
    @@ -2728,9 +2731,12 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
             # determine help from format above
             return formatter.format_help()
     
    -    def _get_formatter(self):
    +    def format_help(self):
    +        return self._format_help()
    +
    +    def _get_formatter(self, file=None):
             formatter = self.formatter_class(prog=self.prog)
    -        formatter._set_color(self.color)
    +        formatter._set_color(self.color, file)
             return formatter
     
         def _get_validation_formatter(self):
    @@ -2747,12 +2753,12 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
         def print_usage(self, file=None):
             if file is None:
                 file = _sys.stdout
    -        self._print_message(self.format_usage(), file)
    +        self._print_message(self._format_usage(file), file)
     
         def print_help(self, file=None):
             if file is None:
                 file = _sys.stdout
    -        self._print_message(self.format_help(), file)
    +        self._print_message(self._format_help(file), file)
     
         def _print_message(self, message, file=None):
             if message:
  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Aug 21, 2026
  3. hugovk commented on Aug 21, 2026

    @hugovk
    Member
  4. aisk commented on Aug 21, 2026

    @aisk
    Member

    And can you also try this issue on 3.15 or newer version, because I see #139946 may have some related changes.

  5. bulletmark commented on Aug 21, 2026

    @bulletmark
    Author

    Just tried 3.15.0rc1 and yes, it fixes this bug, for both print_help() and print_usage().

    Do you still want me to create a minimal example, e.g. to back-fit my or other patch to fix 3.14?

  6. aisk commented on Aug 21, 2026

    @aisk
    Member

    Thank you for ensure this! So this is already fixed on 3.15 (and main branch) by #140495 with original issue #139946, using a different approach compare to your patch.

    That PR was not backported to 3.14 because it may break subclasses, see the discussion there.

    Since this bug only means color is missing, and nothing is broken, I think fixing 3.14 with a different approach than main branch is not worth it, it adds maintenance cost and makes future backports harder. Leaving 3.14 as it is seems better to me.

  7. added
    pendingThe issue will be closed if no feedback is provided
    on Aug 21, 2026
  8. picnixz commented on Aug 21, 2026

    @picnixz
    Member

    Closing as fixed in 3.15.0+ and won't fix for 3.14.

  9. added
    3.14bugs and security fixes
    and removed
    pendingThe issue will be closed if no feedback is provided
    on Aug 21, 2026
  10. bulletmark commented on Aug 21, 2026

    @bulletmark
    Author

    If anybody else comes along, I added the following to my application startup to address this issue:

        if sys.version_info[:2] == (3, 14) and 'FORCE_COLOR' not in os.environ:
            os.environ['FORCE_COLOR'] = '1'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions