-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
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
Conversation
|
Consider adding a test or extending an existing test. |
|
Of course 😉 (this is a draft PR). |
5415561 to
4cd4e41
Compare
4cd4e41 to
a3b5687
Compare
|
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. |
|
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 resolved
Hide resolved
|
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 |
|
Thanks a lot for the review @savannahostrowski! For the record, I was willing to also change: # current
params[choices] = ', '.join(map(str, params['choices']))
# possible
def _color(s):
return f"{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!
|
@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?
|
It shoud be fine now 👍 |
|
Thanks again for working on this @alexprengere! Appreciate it! |
| t = self._theme | ||
| for name, value in params.items(): | ||
| params[name] = f"{t.interpolated_value}{value}{t.reset}" | ||
| return help_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 (most recent call last):
File "<python-input-14>", line 1, in <module>
"""0x%(default)x""" % {'default': f"\x1b[1;31m1234"}
~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
TypeError: %x format: an integer is required, not strThe 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