-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
gh-125355: Rewrite parse_intermixed_args() in argparse #125356
gh-125355: Rewrite parse_intermixed_args() in argparse #125356
Conversation
serhiy-storchaka
commented
Oct 12, 2024
•
edited
Loading
edited
- The parser no longer changes temporarily during parsing.
- Default values are not processed twice.
- Required mutually exclusive groups containing positional arguments are now supported.
- The missing arguments report now includes the names of all required optional and positional arguments.
- Unknown options can be intermixed with positional arguments in parse_known_intermixed_args().
- Issue: Rewrite parse_intermixed_args() in argparse #125355
* The parser is no longer temporary modified and can be used concurrently. * Default values are no long handled twice. * Report about missed arguments now contains names of all required optional and positional arguments. * Unknown options can be intermixed with positionals in parse_known_intermixed_args().
@@ -6350,7 +6356,7 @@ def test_missing_argument_name_in_message(self): | |||
with self.assertRaises(ArgumentParserError) as cm: | |||
parser.parse_intermixed_args([]) | |||
msg = str(cm.exception) | |||
self.assertNotRegex(msg, 'req_pos') | |||
self.assertRegex(msg, 'req_pos') |
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.
This was a bug.
self.assertEqual(NS(bar='y', cmd='cmd', foo='x', rest=[1]), args) | ||
self.assertEqual(['--error', '2', '3'], extras) |
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 that was a bug. The result was different depending on the position of unknown option.
parser = ErrorRaisingArgumentParser(prog='PROG') | ||
parser.add_argument('--foo', nargs="*") | ||
parser.add_argument('foo') | ||
with self.assertWarns(UserWarning): | ||
parser.parse_intermixed_args(['hello', '--foo']) |
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.
if (hasattr(namespace, action.dest) | ||
and getattr(namespace, action.dest)==[]): | ||
from warnings import warn | ||
warn('Do not expect %s in %s' % (action.dest, namespace)) | ||
delattr(namespace, action.dest) |
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 do not understand this code. There were no tests, and the purpose is not clear to me. @hpaulj, @bitdancer, could you shed some light on this?
…rmixed-args' into argparse-parse-intermixed-args
Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13. |
…H-125356) * The parser no longer changes temporarily during parsing. * Default values are not processed twice. * Required mutually exclusive groups containing positional arguments are now supported. * The missing arguments report now includes the names of all required optional and positional arguments. * Unknown options can be intermixed with positional arguments in parse_known_intermixed_args(). (cherry picked from commit 759a54d) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Sorry, @serhiy-storchaka, I could not cleanly backport this to
|
GH-125834 is a backport of this pull request to the 3.13 branch. |
) (GH-125834) * The parser no longer changes temporarily during parsing. * Default values are not processed twice. * Required mutually exclusive groups containing positional arguments are now supported. * The missing arguments report now includes the names of all required optional and positional arguments. * Unknown options can be intermixed with positional arguments in parse_known_intermixed_args(). (cherry picked from commit 759a54d) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
…ythonGH-125356) * The parser no longer changes temporarily during parsing. * Default values are not processed twice. * Required mutually exclusive groups containing positional arguments are now supported. * The missing arguments report now includes the names of all required optional and positional arguments. * Unknown options can be intermixed with positional arguments in parse_known_intermixed_args(). (cherry picked from commit 759a54d) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
GH-125839 is a backport of this pull request to the 3.12 branch. |
) (GH-125839) * The parser no longer changes temporarily during parsing. * Default values are not processed twice. * Required mutually exclusive groups containing positional arguments are now supported. * The missing arguments report now includes the names of all required optional and positional arguments. * Unknown options can be intermixed with positional arguments in parse_known_intermixed_args(). (cherry picked from commit 759a54d)
…H-125356) * The parser no longer changes temporarily during parsing. * Default values are not processed twice. * Required mutually exclusive groups containing positional arguments are now supported. * The missing arguments report now includes the names of all required optional and positional arguments. * Unknown options can be intermixed with positional arguments in parse_known_intermixed_args().