Skip to content

Enable colored output for argparse help in Python 3.14 #19021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 4, 2025

Conversation

cdce8p
Copy link
Collaborator

@cdce8p cdce8p commented May 3, 2025

Support for colored output was just merged in cpython. Maybe a bit early to add it here already but it doesn't hurt either.

Current With color
Without color With color

The color output can be disable using various environment variables.
https://docs.python.org/3.14/using/cmdline.html#using-on-controlling-color

@cdce8p cdce8p changed the title Enable colored output for argparse in 3.14 Enable colored output for argparse in Python 3.14 May 3, 2025
@cdce8p cdce8p force-pushed the argparse-color-output branch from 102b99e to 3368f64 Compare May 3, 2025 09:58
@cdce8p cdce8p changed the title Enable colored output for argparse in Python 3.14 Enable colored output for argparse help in Python 3.14 May 3, 2025

This comment has been minimized.

@cdce8p cdce8p force-pushed the argparse-color-output branch from 3368f64 to 634c4d4 Compare May 3, 2025 11:28

This comment has been minimized.

Copy link
Collaborator

@sterliakov sterliakov left a comment

Choose a reason for hiding this comment

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

This certainly looks nice! Are we sure these changes will be present in final 3.14 release? As usual, there's some chance that unreleased functionality may be removed/renamed/significantly altered.

@cdce8p
Copy link
Collaborator Author

cdce8p commented May 4, 2025

Are we sure these changes will be present in final 3.14 release? As usual, there's some chance that unreleased functionality may be removed/renamed/significantly altered.

There is always the possibility that a new feature gets reverted before the final release, though I don't suspect it happening here. Nevertheless if that should happen, a lot of test cases would fail with 3.14 at which point we could just revert this one.

Copy link
Collaborator

@hamdanal hamdanal left a comment

Choose a reason for hiding this comment

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

Thanks for adding this, I think it is a huge readability upgrade to have colors in the help text of a CLI (the reason I proposed #14455 earlier). I have two suggestions for this change:

  1. Please also change dmypy, stubgen and stubtest parsers
  2. Instead of branching on the python version and passing a keywords dictionary, set the color attribute on the parser instance after initialization, as it is recommended in the argparse documentation.

@cdce8p cdce8p force-pushed the argparse-color-output branch from 954cada to 94052a7 Compare May 4, 2025 14:06
@cdce8p cdce8p requested a review from hamdanal May 4, 2025 14:07
Copy link
Contributor

github-actions bot commented May 4, 2025

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@JelleZijlstra JelleZijlstra merged commit e7405c9 into python:master May 4, 2025
18 checks passed
@cdce8p cdce8p deleted the argparse-color-output branch May 4, 2025 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants