In Python 2.7.3, early in parse_known_args
(called by parse_args
), default values are inserted in the namespace
.
# add any action defaults that aren't present
for action in self._actions:
if action.dest is not SUPPRESS:
if not hasattr(namespace, action.dest):
if action.default is not SUPPRESS:
default = action.default
if isinstance(action.default, basestring): # delayed in 3.3.1
default = self._get_value(action, default)
setattr(namespace, action.dest, default)
If the default
is a string, it is passed through _get_value
which calls the appropriate type
, in your case _check_if_executable_exists
. That produces the error that you see.
In the News for 3.3.1 https://docs.python.org/3.3/whatsnew/changelog.html
Issue #12776, issue #11839: Call argparse type function (specified by add_argument) only once. Before, the type function was called twice in the case where the default was specified and the argument was given as well. This was especially problematic for the FileType type, as a default file would always be opened, even if a file argument was specified on the command line.
With this change, the _get_value
call is postponed to the end of parse_known_args
, and is called only if parsing has not put some other value there (the default is needed).
if isinstance(action.default, basestring): # dropped in 3.3.1
default = self._get_value(action, default)
So your script runs as expected (with '-h') on my development copy. I'm not entirely sure which versions of Python have this correction.
So until you can run a newer Python version, it's your responsibility to ensure that the default
is a valid value. Even with this bug fix, it's a good idea make sure that your defaults are valid before you give them to the add_argument()
call. An invalid default will confuse your user regardless of when it is handled.