KEMBAR78
gh-65865: Raise early errors for invalid help strings in argparse by serhiy-storchaka · Pull Request #124899 · python/cpython · GitHub
Skip to content

Conversation

@serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Oct 2, 2024

@serhiy-storchaka serhiy-storchaka merged commit eb2d268 into python:main Oct 12, 2024
34 checks passed
@serhiy-storchaka serhiy-storchaka deleted the argparse-check-help branch October 12, 2024 10:10
mdboom added a commit to mdboom/pyperformance that referenced this pull request Oct 15, 2024
Since python/cpython#124899,
argparse help strings are more strict, and will fail
with invalid `%` format strings.
mdboom added a commit to python/pyperformance that referenced this pull request Oct 15, 2024
Since python/cpython#124899,
argparse help strings are more strict, and will fail
with invalid `%` format strings.
@brandtbucher
Copy link
Member

Just want to share that the new errors seems a bit cryptic. In pyperformance, we had argparsers suddenly start failing with:

Traceback (most recent call last):
  File "/home/benchmarking/actions-runner/_work/benchmarking/benchmarking/cpython/Lib/argparse.py", line 1659, in _check_help
    formatter._expand_help(action)
    ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^
  File "/home/benchmarking/actions-runner/_work/benchmarking/benchmarking/cpython/Lib/argparse.py", line 604, in _expand_help
    return help_string % params
           ~~~~~~~~~~~~^~~~~~~~
TypeError: %o format: an integer is required, not dict

The issue was that the help string for one argument contained the phrase 90% of, which is super subtle. I wonder if maybe a better error message could be produced here?

@serhiy-storchaka
Copy link
Member Author

This is not specific to arparse. You get the same error message with pure printf-style error formatting:

>>> '90% of' % {'a': 1}
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    '90% of' % {'a': 1}
    ~~~~~~~~~^~~~~~~~~~
TypeError: %o format: an integer is required, not dict

Please open a new issue if you have ideas how to improve this.

In argparse, the full traceback refers also to the line in your code that adds an invalid help string, so get a guide where to search for error.

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.

3 participants