Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-141939: Add colors to interpolated values in argparse#141940
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
Uh oh!
There was an error while loading. Please reload this page.
Conversation
alexprengere commented Nov 25, 2025 • edited by bedevere-app bot
Loading Uh oh!
There was an error while loading. Please reload this page.
edited by bedevere-app bot
Uh oh!
There was an error while loading. Please reload this page.
mpkocher commented Nov 25, 2025
Consider adding a test or extending an existing test. |
alexprengere commented Nov 25, 2025
Of course 😉 (this is a draft PR). |
5415561 to 4cd4e41Compare4cd4e41 to a3b5687Comparealexprengere commented Nov 27, 2025
I updated the code and tests 😉 I had to change a bit the logic introduced in #141680 as introducing a new color for interpolated values, that would be different from the For I hope this is clear, I can amend this if needed. |
alexprengere commented Nov 28, 2025 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
For the record the PR could be slightly improved with the formatting of ![]() The issue is that the use of |
savannahostrowski left a comment
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.
A couple of comments but I spent some time playing around with this and I think it looks pretty good. I think that we're early enough in the release cycle that we can see what, if any, feedback we get on the use of YELLOW for default, type and interpolated values. Some folks may find it overly busy but personally, I think having some colour treatment here does help with the overall readability.
FWIW, I also compared this to rich-argparse and click. Neither seem to do this type of interpolation in help text. Not necessarily a point for or against doing this but just wanted to note it!
Misc/NEWS.d/next/Library/2025-11-28-08-25-19.gh-issue-141939.BXPnFj.rst Outdated Show resolvedHide resolved
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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 |
alexprengere commented Dec 2, 2025
Thanks a lot for the review @savannahostrowski! For the record, I was willing to also change: # currentparams[choices] =', '.join(map(str, params['choices'])) # possibledef_color(s): returnf"{t.interpolated_value}{s}{t.reset}"params['choices'] =', '.join(map(_color, params['choices']))But this introduced more ANSI escape codes, and I kept hitting the wrapping bug as described in #142035: ![]() So I left that part alone for now, but I can revisit if we ever find a solution. |
Thanks for making the requested changes! @savannahostrowski: please review the changes made to this pull request. |
savannahostrowski left a comment
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 for this! I'll let Hugo also take a look before merging but this LGTM!
mpkocher commented Dec 2, 2025
@alexprengere A lot of attention to detail on this issue and it shows. Nice job.
As a separate issue, when |
hugovk left a comment
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! Please could you fix the merge conflict?
alexprengere commented Dec 12, 2025
It shoud be fine now 👍 |
savannahostrowski commented Dec 12, 2025
Thanks again for working on this @alexprengere! Appreciate it! |
6d644e4 into python:mainUh oh!
There was an error while loading. Please reload this page.
| t=self._theme | ||
| forname, valueinparams.items(): | ||
| params[name] =f"{t.interpolated_value}{value}{t.reset}" | ||
| returnhelp_string%params |
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.
The above change looks to be giving me issues at this line.
An MVE looks like this:
my_parser.add_argument( "--foo", type=int, default=1234, help=f"""0x%(default)x""", ) The above works in 3.14.2, but not in 3.15 alpha3. I apologize if my code is doing something incorrect.
The error I get is
Traceback (most recent call last): File "[...]/3.15.0a3/lib/python3.15/argparse.py", line 1793, in _check_help formatter._expand_help(action) ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^ File "[...]/3.15.0a3/lib/python3.15/argparse.py", line 696, in _expand_help return help_string % params ~~~~~~~~~~~~^~~~~~~~ TypeError: %x format: an integer is required, not str 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.
IMO your code is correct. Here is the basic issue:
# before 3.15a3>>>"""0x%(default)x"""%{'default': 1234} '0x4d2'# after 3.15a3>>>"""0x%(default)x"""%{'default': f"\x1b[1;31m1234"} Traceback (mostrecentcalllast): File"<python-input-14>", line1, in<module>"""0x%(default)x"""%{'default': f"\x1b[1;31m1234"} ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~TypeError: %xformat: anintegerisrequired, notstrThe value to interpolate is converted to a (colored) string first, then passed to the % formatting, where it fails due to a type mismatch (with x it expects an int).
If we want to keep this feature, I think the coloring needs to happen in help_string, so we do not fail on such cases, as we preserve the types of interpolated values. This is a bit more complex, but was already done for ArgumentDefaultsHelpFormatter in #141680 for _get_help_string.
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.
For the record I created #142980 to track this.


--help#141939