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 — gh-139946: distinguish stdout or stderr when colorizing output in argparse by frostming · Pull Request #140495 · python/cpython

Conversation

@frostming

@frostming frostming commented Oct 23, 2025

Copy link
Copy Markdown
Contributor

Fix issue:

  • Errors raised in argparse are colorized when redirecting stderr to a file #139946

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@frostming
frostming requested a review from savannahostrowski as a code owner October 23, 2025 06:36
@bedevere-app bedevere-app Bot mentioned this pull request Oct 23, 2025
Errors raised in argparse are colorized when redirecting stderr to a file #139946
Closed
@bedevere-app bedevere-app Bot added the awaiting review label Oct 23, 2025
savannahostrowski
savannahostrowski requested changes Oct 25, 2025
View reviewed changes
Comment thread Lib/argparse.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting review label Oct 25, 2025
@bedevere-app

bedevere-app Bot commented Oct 25, 2025

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added the awaiting changes label Oct 25, 2025
@savannahostrowski savannahostrowski added the needs backport to 3.14 bugs and security fixes label Oct 27, 2025
@savannahostrowski

savannahostrowski commented Oct 27, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Thanks for addressing the comments. After thinking about this more, I'm a bit concerned about API compatibility. Adding the formatter parameter to format_usage() and format_help() changes the public API. While it has a default value, this could still break subclasses that override these methods. I would like to backport a fix for this bug, but maybe @hugovk has thoughts on this change as RM.

Also, we'll need to add some tests here, since this wasn't caught with our existing test suite.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hugovk

hugovk commented Oct 28, 2025

Copy link
Copy Markdown
Member

I would like to backport a fix for this bug, but maybe @hugovk has thoughts on this change as RM.

After originally adding colour to argparse (#132323), we did get reports (#133653) that subclassing HelpFormatter was broken:

TypeError: HelpFormatter.__init__() got an unexpected keyword argument 'prefix_chars'

We initially considered this a docs issue, because HelpFormatter isn't fully documented (#133653 (comment)) but then fixed it in another way (#133941).

We could again consider this a docs issue, but I think we need to be careful about backwards compatibility especially after 3.14.0. Could be worth checking things like tox, virtualenv and mypy that were affected last time.

And if we choose not to backport, we can still use NO_COLOR=1 or PYTHON_COLORS=0 when redirecting output:

❯ NO_COLOR=1 ./python.exe -m calendar 2020 01 01 2> err
❯ # or
❯ PYTHON_COLORS=0 ./python.exe -m calendar 2020 01 01 2> err
❯ # then
❯ cat err
usage: python.exe -m calendar [-h] [-w WIDTH] [-l LINES] [-s SPACING] [-m MONTHS]
                              [-c CSS] [-L LOCALE] [-e ENCODING] [-t {text,html}]
                              [-f FIRST_WEEKDAY]
                              [year] [month]
python.exe -m calendar: error: unrecognized arguments: 01
savannahostrowski and gpshead reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@savannahostrowski

savannahostrowski commented Oct 30, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

@hugovk Oh thanks, I missed that thread...this is the kind of thing I was afraid of. I'm inclined to say 3.15 onward + a docs update to note this limitation/workaround in 3.14.

hugovk reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@savannahostrowski savannahostrowski removed the needs backport to 3.14 bugs and security fixes label Oct 30, 2025
gpshead
gpshead reviewed Nov 11, 2025
View reviewed changes
Comment thread Lib/argparse.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@frostming
frostming force-pushed the fix/colorize-argparse branch from fe5c9d7 to 82e00f0 Compare November 12, 2025 02:18
@savannahostrowski

savannahostrowski commented Dec 4, 2025

Copy link
Copy Markdown
Member

Circling back on this (appreciate your patience @frostming!), based on this convo, I think we should just move forward with this for 3.15, no backport since this is an API change. For 3.14, we should open a separate PR for the documentation changes for the workaround.

I'll leave this open for another couple days to see if anyone else wants to chime in. Otherwise, will merge 😄 !

yihong0618 and hugovk reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

savannahostrowski
savannahostrowski approved these changes Dec 4, 2025
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting changes labels Dec 4, 2025
@savannahostrowski savannahostrowski linked an issue Dec 6, 2025 that may be closed by this pull request
Errors raised in argparse are colorized when redirecting stderr to a file #139946
Closed
@savannahostrowski

savannahostrowski commented Dec 7, 2025

Copy link
Copy Markdown
Member

@frostming Mind resolving the merge conflicts and then I can get to merging this? 🙂

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

frostming and others added 5 commits December 8, 2025 08:30
@frostming
fix: distinguish stdout or stderr when colorizing output in argparse
1c6c898
Signed-off-by: Frost Ming <[email protected]>
@blurb-it @frostming
📜🤖 Added by blurb_it.
6d10257
@frostming
fix: make _get_formatter to take file argument to avoid calling set_c…
6c02905
…olor

Signed-off-by: Frost Ming <[email protected]>
@frostming
add tests for the change
8ed66a2
Signed-off-by: Frost Ming <[email protected]>
@frostming
fix: handle backward-compatibility for format_* methods
8518870
Signed-off-by: Frost Ming <[email protected]>
@frostming
frostming force-pushed the fix/colorize-argparse branch from dc33ac5 to 8518870 Compare December 8, 2025 00:30
@frostming

frostming commented Dec 8, 2025

Copy link
Copy Markdown
Contributor Author

@frostming Mind resolving the merge conflicts and then I can get to merging this? 🙂

It's done. Thank you very much.

savannahostrowski and yihong0618 reacted with rocket emoji
All reactions
  • 🚀 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@savannahostrowski
Merge branch 'main' into fix/colorize-argparse
6dd3d3d
@savannahostrowski
savannahostrowski enabled auto-merge (squash) December 8, 2025 03:41
@savannahostrowski
savannahostrowski merged commit 7099af8 into python:main Dec 8, 2025
46 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Dec 8, 2025
@frostming
frostming deleted the fix/colorize-argparse branch December 8, 2025 04:16
@mpkocher

mpkocher commented Dec 9, 2025

Copy link
Copy Markdown
Contributor

Perhaps this is the best one can do with the current constraints and large usage of people using internal APIs.

  1. It looks like you're mechanically keeping "backwards" compatibility, but you're changing this semantic meaning of the _get_formatter API call
  2. Static analyzing tools aren't going to be helpful to debug or update your code to the new semantics
  3. If you want to support multiple versions of Python, you'll need to add if sys.version_info >= (3, 14): for both the ArgumentParser(...) constructor as well as the _get_formatter() call.

There's just so much widespread use of internal APIs.

The pattern is this:

formatter = parser._get_formatter()
# ...
parser._print_message(formatter.format_help(), sys.stderr)

https://github.com/dib-lab/khmer/blob/fb65d21eaedf0d397d49ae3debc578897f9d6eb4/khmer/khmer_args.py#L112

https://github.com/ioritzsb/omnipose/blob/4e3a172b41cb34bcefc99b957bf08af21fe39248/omnipose/cli.py#L5

General Search:

https://github.com/search?q=language%3Apython+._get_formatter%28%29&type=code

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@aisk aisk mentioned this pull request Aug 21, 2026
Argparse print_help() and print_usage() do not color based on their file argument #156144
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@gpshead gpshead gpshead left review comments
@savannahostrowski savannahostrowski savannahostrowski approved these changes
+1 more reviewer
@mpkocher mpkocher mpkocher left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Errors raised in argparse are colorized when redirecting stderr to a file

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@frostming @savannahostrowski @hugovk @mpkocher @gpshead
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.