GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Argparse print_help() and print_usage() do not color based on their file argument · Issue #156144 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
Closed
Argparse print_help() and print_usage() do not color based on their file argument#156144
Copy link
Labels
3.14bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@bulletmark
bulletmark
opened on Aug 21, 2026
Issue body actions

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
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Aug 21, 2026
  2. bulletmark commented on Aug 21, 2026

    @bulletmark
    bulletmark
    on Aug 21, 2026
    Author
    More actions

    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:
  3. aisk
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Aug 21, 2026
  4. aisk
    added this to Argparse issueson Aug 21, 2026
  5. hugovk commented on Aug 21, 2026

    @hugovk
    hugovk
    on Aug 21, 2026
    Member
    More actions

    @bulletmark Please can you include a minimal, reproducible code example?

  6. aisk commented on Aug 21, 2026

    @aisk
    aisk
    on Aug 21, 2026
    Member
    More actions

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

  7. bulletmark commented on Aug 21, 2026

    @bulletmark
    bulletmark
    on Aug 21, 2026
    Author
    More actions

    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?

  8. aisk commented on Aug 21, 2026

    @aisk
    aisk
    on Aug 21, 2026
    Member
    More actions

    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.

    Reacted by Hugo van Kemenade
  9. hugovk
    added
    pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is provided
    on Aug 21, 2026
  10. picnixz commented on Aug 21, 2026

    @picnixz
    picnixz
    on Aug 21, 2026
    Member
    More actions

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

  11. picnixz
    closed this as completedon Aug 21, 2026
  12. github-project-automation
    moved this to Doc issues in Argparse issueson Aug 21, 2026
  13. picnixz
    added
    3.14bugs and security fixesbugs and security fixes
    and removed
    pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is provided
    on Aug 21, 2026
  14. bulletmark commented on Aug 21, 2026

    @bulletmark
    bulletmark
    on Aug 21, 2026
    Author
    More actions

    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'
    Reacted by An Long and Elan Ruusamäe
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 fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    • Argparse issues
      Status
      Doc issues
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.