Uh oh!
There was an error while loading. Please reload this page.
gh-130645: Add colour to argparse help - #132323
Conversation
bedevere-bot
commented
Apr 9, 2025
🤖 New build scheduled with the buildbot fleet by @hugovk for commit 3d174b7 🤖 Results will be shown at: https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F132323%2Fmerge If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
When you're done making the requested changes, leave the comment: |
savannahostrowski
left a comment
There was a problem hiding this comment.
A comment about the approach and whether we can simplify a bit.
hugovk
commented
Apr 30, 2025
Many argparse CLIs I see already use keywords for most parameters, but I think we should make these new ones keyword-only, especially for new bools, as a long list of
What do you think? |
Commandeering this to make the beta 1 cut. Code owner's feedback can be addressed in follow-up PRs.
python/cpython#132323 added an optional `color` argument to ArgumentParser. As a side effect the help formatters are now called with two new keyword arguments `prefix_chars` and `color`. Add `**kwargs` to the custom `AugmentedHelpFormatter` and pass it through to the super class.
this patch breaks $ ./prefix/bin/python3 virtualenv.pyz venvvvvTraceback (most recent call last): File "<frozen runpy>", line 198, in _run_module_as_main File "<frozen runpy>", line 88, in _run_code File "/home/asottile/workspace/cpython/virtualenv.pyz/__main__.py", line 178, in <module> run() ~~~^^ File "/home/asottile/workspace/cpython/virtualenv.pyz/__main__.py", line 174, in run run_virtualenv() ~~~~~~~~~~~~~~^^ File "/home/asottile/workspace/cpython/virtualenv.pyz/virtualenv/__main__.py", line 20, in run session = cli_run(args, options, env) File "/home/asottile/workspace/cpython/virtualenv.pyz/virtualenv/run/__init__.py", line 31, in cli_run of_session = session_via_cli(args, options, setup_logging, env) File "/home/asottile/workspace/cpython/virtualenv.pyz/virtualenv/run/__init__.py", line 49, in session_via_cli parser, elements = build_parser(args, options, setup_logging, env) ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/asottile/workspace/cpython/virtualenv.pyz/virtualenv/run/__init__.py", line 64, in build_parser add_version_flag(parser) ~~~~~~~~~~~~~~~~^^^^^^^^ File "/home/asottile/workspace/cpython/virtualenv.pyz/virtualenv/run/__init__.py", line 138, in add_version_flag parser.add_argument( ~~~~~~~~~~~~~~~~~~~^ "--version", ^^^^^^^^^^^^ ...<2 lines>... help="display the version of the virtualenv package and its location, then exit", ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "/home/asottile/workspace/cpython/prefix/lib/python3.14/argparse.py", line 1566, in add_argument formatter = self._get_formatter() File "/home/asottile/workspace/cpython/prefix/lib/python3.14/argparse.py", line 2733, in _get_formatter return self.formatter_class( ~~~~~~~~~~~~~~~~~~~~^ prog=self.prog, ^^^^^^^^^^^^^^^ prefix_chars=self.prefix_chars, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ color=self.color, ^^^^^^^^^^^^^^^^^ ) ^TypeError: HelpFormatter.__init__() got an unexpected keyword argument 'prefix_chars'it looks like adding the parameters to the help formatter here breaks the subclass there cc @gaborbernat |
cdce8p
commented
May 4, 2025
It broke mypy as well. The fix was trivial though: python/mypy#19020 |
The-Compiler
commented
May 8, 2025
This breaks |
python/cpython#132323 passes prefix_chars= (and other arguments) to the formatter_class, but the custom HelpFormatter only accepted prog=. Accept any keyword arguments and pass them on to the parent class. Fixestox-dev#3523
python/cpython#132323 passes prefix_chars= (and other arguments) to the formatter_class, but the custom HelpFormatter only accepted prog=. Accept any keyword arguments and pass them on to the parent class. Fixestox-dev#3523
hugovk
commented
May 8, 2025
We'll fix it for beta 2: #133653. Fixes (temporary or not) is helpful in other tools to enable testing beta 1, thanks! |
…13947) These are - `suggest_on_error`, added by python/cpython#124456, made keyword-only by python/cpython#133302 - `color`, added by python/cpython#132323 Co-authored-by: Sebastian Rittau <srittau@rittau.biz> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
…ython#13947) These are - `suggest_on_error`, added by python/cpython#124456, made keyword-only by python/cpython#133302 - `color`, added by python/cpython#132323 Co-authored-by: Sebastian Rittau <srittau@rittau.biz> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Add
colorparameter toargparse.ArgumentParser, which by default isFalse. When set toTrue, the help text is in colour if allowed (for example, we're not piping,NO_COLOR=1is not set).Example output
Using this script:
Performance
With the same script and PGO+LTO on macOS, running it once (
time ./python.exe argparse-cli.py --help) and 100 times (time ./100.sh):main(3feac7a):PR and colour not enabled:
PR and colour enabled:
Something like 0.65 ms difference per run between before and after, averaging from
100.sh.argparsehelp #130645📚 Documentation preview 📚: https://cpython-previews--132323.org.readthedocs.build/