-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
Description
I'm building a custom argparse/configparser combined config manager to replace a deprecated dependency used in a project I'm porting to Python 3 (I did find ConfigArgParse though I would rather to not use a third party lib for this yet).
When constructing the ArgumentParser and adding arguments to it, I was puzzled by the following exception being raised after calling add_argument
:
TypeError: __init__() got an unexpected keyword argument 'type'
The minimal code to reproduce the error I got is:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo', default='False', action='store_true', type=bool)
Complete exception:
/usr/lib/python3.8/argparse.py in add_argument(self, *args, **kwargs)
1378 if not callable(action_class):
1379 raise ValueError('unknown action "%s"' % (action_class,))
-> 1380 action = action_class(**kwargs)
1381
1382 # raise an error if the action type is not callable
TypeError: __init__() got an unexpected keyword argument 'type'
After some investigation, I found out that the code raising this exception is the argparse._StoreTrueAction
when called with the parameters passed to add_argument
. I did some googling and I did find this related stackoverflow post, where the OP seems to be confused by wanting to use store_true
with int/str options. My case is different in that my options are indeed bool options, and I do want the parser to store_true
when the option is passed, and to treat the default 'False'
as bool.
A fair question you may ask is why my default is the string 'False'
instead of the False
value: this is a side-effect of grabbing all the config options from config files (using ConfigParser
), and having the default values being fetched as strings. While I could convert this value to bool before passing it to add_argument
, I was making use of the following documented feature of argparse:
If the default value is a string, the parser parses the value as if it were a command-line argument. In particular, the parser applies any type conversion argument, if provided, before setting the attribute on the Namespace return value.
I'm aware this can be solved with a workaround in my code, and in a way the 'False'
default is incorrect, so I would totally understand if this issue is closed as Invalid. Thanks!
- CPython versions tested on: 3.8
- Operating system and architecture: Linux 5.4.0-124-generic x86_64
Metadata
Metadata
Assignees
Labels
Projects
Status