Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-65865: Raise early errors for invalid help strings in argparse #124899

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
Oct 12, 2024

Conversation

serhiy-storchaka
Copy link
Member

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

Lib/argparse.py Outdated Show resolved Hide resolved
@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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.