You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
argparse: creating an already defined subparsers does not raises an exception #59061
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2012-05-19.12:27:50.832>labels= ['type-feature', 'library', '3.11']
title='argparse: creating an already defined subparsers does not raises an exception'updated_at=<Date2021-12-02.23:11:09.228>user='https://bugs.python.org/eacb'
However, since there is no del_parser method, I'm inclined to think that this is not a bug. That is, the ability to overwrite a previously defined subparser could be considered a feature.
If Steven does consider it a bug, I think it can only be fixed in 3.3, since to do otherwise could needlessly break currently-working programs. If it is not a bug, add_parser's behavior when a parser already exist should be documented.
bethard commented on Jul 21, 2012
bethardmannequin
Mannequin
More actions
Yeah, overwriting the existing parser is probably not typically what the user intended.
However, I could see someone doing this if, say, they had a parser designed by another module writer, and they wanted to use it but just change one of the sub-parsers or something like that. But a "set_parser" method would probably make more sense for this use case than "add_parser".
I guess my recommendation is to add a "set_parser" method that replaces the old parser (like "add_parser" does now), and then issue deprecation warnings in "add_parser" if a parser with the given name is present (like the patch does, but with deprecation warnings instead of exceptions).
We could then turn the warnings into errors in a few releases.
Activity
eacb commentedon May 19, 2012
With this patch, it raises an ArgumentException, instead of overwriting previous subparser without notice.
Regards.
bitdancer commentedon May 22, 2012
Thanks for the report and patch.
However, since there is no del_parser method, I'm inclined to think that this is not a bug. That is, the ability to overwrite a previously defined subparser could be considered a feature.
If Steven does consider it a bug, I think it can only be fixed in 3.3, since to do otherwise could needlessly break currently-working programs. If it is not a bug, add_parser's behavior when a parser already exist should be documented.
bethard commentedon Jul 21, 2012
Yeah, overwriting the existing parser is probably not typically what the user intended.
However, I could see someone doing this if, say, they had a parser designed by another module writer, and they wanted to use it but just change one of the sub-parsers or something like that. But a "set_parser" method would probably make more sense for this use case than "add_parser".
I guess my recommendation is to add a "set_parser" method that replaces the old parser (like "add_parser" does now), and then issue deprecation warnings in "add_parser" if a parser with the given name is present (like the patch does, but with deprecation warnings instead of exceptions).
We could then turn the warnings into errors in a few releases.
bradengroom commentedon Oct 28, 2018
#10159
iritkatriel commentedon Dec 2, 2021
The open patch deprecates overriding the parser with add_parser, but it does not add the new set_parser method that Steven suggested.
serhiy-storchaka commentedon Oct 22, 2024
Adding a parser with a name or an alias that matches an existing parser was forbidden in bpo-39716 (gh-83897).