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-139809: Fix argparse subcommand prog not respecting color environment variables by savannahostrowski · Pull Request #139818 · python/cpython

Conversation

@savannahostrowski

@savannahostrowski savannahostrowski commented Oct 9, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member
  • Issue: 3.14+ argparse colorization makes sub-commands prog always colored if colored at creation time #139809

Sorry, something went wrong.

Uh oh!

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

All reactions
savannahostrowski added 2 commits October 8, 2025 19:49
@savannahostrowski
Add fix to decolorize and test
1e3e343
@savannahostrowski
Remove extra line
8511193
@savannahostrowski
savannahostrowski requested a review from hugovk October 9, 2025 02:56
@savannahostrowski savannahostrowski added the needs backport to 3.14 bugs and security fixes label Oct 9, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Oct 9, 2025
3.14+ argparse colorization makes sub-commands prog always colored if colored at creation time #139809
Closed
@bedevere-app bedevere-app Bot added the awaiting core review label Oct 9, 2025
gaborbernat
gaborbernat reviewed Oct 9, 2025
View reviewed changes
Comment thread Lib/argparse.py Outdated
groups = self._mutually_exclusive_groups
formatter.add_usage(None, positionals, groups, '')
kwargs['prog'] = formatter.format_help().strip()
kwargs['prog'] = formatter._decolor(formatter.format_help().strip())

@gaborbernat gaborbernat Oct 9, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to make sure this removes to the coloring independent if coloring is on or not, yes?

Sorry, something went wrong.

Uh oh!

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

All reactions

@savannahostrowski savannahostrowski Oct 9, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, _decolor strips ANSI codes when coloring is enabled. When coloring is disabled, it's a no-op since no ANSI codes should exist.

Sorry, something went wrong.

Uh oh!

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

All reactions

@gaborbernat gaborbernat Oct 9, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not going to be enough. I mentioned before, but prog is a public metadata on the parser that contains the name of the application. We should not color it until it's about to be printed later on, otherwise will not fix my issue here https://github.com/tox-dev/sphinx-argparse-cli/blob/main/src/sphinx_argparse_cli/_logic.py#L340-L342. Thanks!

Sorry, something went wrong.

Uh oh!

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

All reactions

@savannahostrowski savannahostrowski Oct 9, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I see your point. I think another approach here could be to create a separate formatter explicitly set with color=False, so that prog wouldn't get colored in the first place (rather than having to decolor it).

Sorry, something went wrong.

Uh oh!

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

serhiy-storchaka reacted with thumbs up emoji gaborbernat reacted with heart emoji
All reactions
  • 👍 1 reaction
  • ❤️ 1 reaction

@gaborbernat gaborbernat Oct 9, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated but I should mention it here that I was surprised that setting color to False for the formatter (see here https://github.com/tox-dev/sphinx-argparse-cli/blob/main/src/sphinx_argparse_cli/_logic.py#L402-L404) doesn't actually disables color printing, had to set NO_COLOR during format_usage to not color that output 🤔 Seems the color argument of the parser is instead used 🤔 not sure if that's a bug or a feature, but suprised me.

By the way I love the colors in the CLI ❤️ (definitely the best feature of 3.14) , just they are not needed when you're writing a sphinx plugin to render it for other outputs 😆

Sorry, something went wrong.

Uh oh!

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

savannahostrowski and hugovk reacted with heart emoji
All reactions
  • ❤️ 2 reactions

@savannahostrowski savannahostrowski Oct 9, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this seems like a bug. HelpFormatter accepts color, but it is then seemingly overridden in _get_formatter. This should probably be respected, or we should remove it to avoid confusion.

Sorry, something went wrong.

Uh oh!

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

All reactions
blurb-it Bot and others added 5 commits October 9, 2025 03:06
@blurb-it
📜🤖 Added by blurb_it.
c94021b
@savannahostrowski
Remove parens in blurb
98b78b6
@savannahostrowski
Use formatter class to avoid premature colorization of prog
666dd43
@savannahostrowski
Update blurb
f2c813a
@savannahostrowski
Update test
5ba525a
@python-cla-bot

python-cla-bot Bot commented Oct 9, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

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

hugovk
hugovk approved these changes Oct 9, 2025
View reviewed changes

@hugovk hugovk left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Misc/NEWS.d/next/Library/2025-10-09-03-06-19.gh-issue-139809.lzHJNu.rst Resolved

Uh oh!

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

@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Oct 9, 2025
serhiy-storchaka
serhiy-storchaka approved these changes Oct 9, 2025
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. 👍

There are helpers for temporary monkey-patching in test.support and unittest.mock. You can use them if you prefer.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/test/test_argparse.py Resolved

Uh oh!

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

@savannahostrowski
Move blurb and simplify setUp
86551d1
@savannahostrowski savannahostrowski mentioned this pull request Oct 9, 2025
Investigate argparse formatter handling of color #139862
Closed
@savannahostrowski
Merge branch 'main' into fix-colorize-subparser
8274bc7
@savannahostrowski
savannahostrowski enabled auto-merge (squash) October 9, 2025 16:27
@savannahostrowski
savannahostrowski merged commit 9fc4366 into python:main Oct 9, 2025
4 checks passed

Uh oh!

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

@miss-islington-app

miss-islington-app Bot commented Oct 9, 2025

Copy link
Copy Markdown

Thanks @savannahostrowski for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-app bedevere-app Bot removed the awaiting merge label Oct 9, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 9, 2025
@savannahostrowski @miss-islington
pythonGH-139809: Fix argparse subcommand prog not respecting color en…
c898f24
…vironment variables (pythonGH-139818)

(cherry picked from commit 9fc4366)

Co-authored-by: Savannah Ostrowski <[email protected]>
@bedevere-app

bedevere-app Bot commented Oct 9, 2025

Copy link
Copy Markdown

GH-139866 is a backport of this pull request to the 3.14 branch.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Oct 9, 2025
savannahostrowski added a commit that referenced this pull request Oct 9, 2025
@miss-islington @savannahostrowski
[3.14] GH-139809: Fix argparse subcommand prog not respecting color e…
af1cb1c
…nvironment variables (GH-139818) (#139866)

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

Co-authored-by: Savannah Ostrowski <[email protected]>
gaborbernat
gaborbernat reviewed Oct 9, 2025
View reviewed changes
Comment thread Lib/argparse.py Resolved

Uh oh!

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

@savannahostrowski savannahostrowski mentioned this pull request Oct 12, 2025
_colorize.can_colorize safety in test_argparse #135237
Closed
@savannahostrowski
savannahostrowski deleted the fix-colorize-subparser branch December 4, 2025 22:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@hugovk hugovk hugovk approved these changes
@serhiy-storchaka serhiy-storchaka serhiy-storchaka approved these changes
+1 more reviewer
@gaborbernat gaborbernat gaborbernat 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.

Uh oh!

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

4 participants

@savannahostrowski @gaborbernat @hugovk @serhiy-storchaka
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.