-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
bpo-44986: Fixed bug with date formats like %Y-%m-%d in help messages of argparse #27923
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
|
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA). Recognized GitHub usernameWe couldn't find a bugs.python.org (b.p.o) account corresponding to the following GitHub usernames: This might be simply due to a missing "GitHub Name" entry in one's b.p.o account settings. This is necessary for legal reasons before we can look at this contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. You can check yourself to see if the CLA has been received. Thanks again for the contribution, we look forward to reviewing it! |
|
I don't know if a human will ever read this, but if so I have already signed the CLA yesterday and in the check yourself page still says that I have not signed it. Is it normal that it takes so long? |
…lace escaped %% in the expand help method
|
This PR is stale because it has been open for 30 days with no activity. |
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.
Ran the argparse test module:
Ran 1670 tests in 3.185s
OK
== Tests result: SUCCESS ==
And example in bug ok.
|
@SengerM Can you add the unit tests for this case? |
|
Most changes to Python require a NEWS entry. Please add it using the blurb_it web app or the blurb command-line tool. |
…to escape `%` in help messages of `argparse`
|
Most changes to Python require a NEWS entry. Please add it using the blurb_it web app or the blurb command-line tool. |
|
Dear @furkanonder , sorry for the long delay. I added the test case. I see there are fails in the tests for macOS, but they don't seem related to my changes. |
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.
I know this PR has been sitting for a little while, but I pulled it down and rebased it with the main, and it still resolves the bug. Just a couple of comments but overall, this LGTM.
| return self._get_help_string(action) % params | ||
| expanded_help = self._get_help_string(action) | ||
| for param, value in params.items(): | ||
| if f'%({param})s' in expanded_help: |
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.
I think this conditional is redundant, right? Doesn't replace() handle validating that the string is contained by expanded_help?
| version = '' | ||
|
|
||
| class TestHelpWithPercentageSymbols(HelpTestCase): | ||
| """Test a help message including % symbols""" |
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.
| """Test a help message including % symbols""" | |
| """Test help output that includes % symbols""" |
|
|
||
| class TestHelpWithPercentageSymbols(HelpTestCase): | ||
| """Test a help message including % symbols""" | ||
| parser_signature = Sig(prog='PROG', description='Just a test code.') |
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.
| parser_signature = Sig(prog='PROG', description='Just a test code.') | |
| parser_signature = Sig(prog='PROG', description='Just a test program.') |
| parser_signature = Sig(prog='PROG', description='Just a test code.') | ||
| argument_signatures = [ | ||
| Sig('--somearg', metavar='somearg', | ||
| help='Now you dont need to escape this: %, and you will not get nonsensical errors!'), |
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.
| help='Now you dont need to escape this: %, and you will not get nonsensical errors!'), | |
| help='No escape required for %'), |
| argument_signatures = [ | ||
| Sig('--somearg', metavar='somearg', | ||
| help='Now you dont need to escape this: %, and you will not get nonsensical errors!'), | ||
| Sig('--date', metavar='when', help='A date in format %Y-%m-%d', |
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.
| Sig('--date', metavar='when', help='A date in format %Y-%m-%d', | |
| Sig('--date', metavar='when', help='A date in %Y-%m-%d format', |
| Sig('--date', metavar='when', help='A date in format %Y-%m-%d', | ||
| dest=f'date', type=str, required=True), | ||
| Sig('bar', nargs='?', type=int, default=42, | ||
| help='the bar to %(prog)s (default: %(default)s)'), |
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.
| help='the bar to %(prog)s (default: %(default)s)'), | |
| help='The bar to %(prog)s (default: %(default)s)'), |
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.
It's not that simple.
What if the replacement value contains %% or %(prog)s? Or only the part of it, and the other part is contained in the help string? E.g. help='%(default)s(prog)s', where default='%'.
What if the help string contains %(...)r or %(...)a instead of %(...)s?
|
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 |
|
You can implement a "safe" help formatter as a separate class, but this is not easy task. I am not sure that |
Fixed bug with date formats like %Y-%m-%d in help messages of argparse
Now this code does not produce any error when invoked with the
-hoption and produces the expected result:https://bugs.python.org/issue44986