Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.3k
Description
Bug report
Bug description:
When using argparse to define a BooleanOptionAction that starts with "--no-", it is not possible to set the flag as True. This is counterintuitive, as one would expect that passing the argument should set the flag toTrue, regardless of the name of the argument.
The bug appears to originate fromthis line of code:
setattr(namespace,self.dest,notoption_string.startswith('--no-'))
Because BooleanOptionAction always generates another argument that starts with "--no-", the code here attempts to check for the case where the user passed in that generated argument. It doesn't consider the possibility that the original argument could also start with "--no-".
This code here demonstrates the bug:
importargparseparser=argparse.ArgumentParser()parser.add_argument('--no-foo',action=argparse.BooleanOptionalAction)assert(parser.parse_args(['--no-no-foo']).no_foo==False)# Passesassert(parser.parse_args(['--no-foo']).no_foo==True)# Fails
While I tested this on 3.10, the argparse snippet above is from the main branch, which suggests the bug is in the latest version.
CPython versions tested on:
3.10
Operating systems tested on:
Linux
Linked PRs
Metadata
Metadata
Assignees
Projects
Status