add newline to the end

Signed-off-by: Frost Ming <me@frostming.com>
This commit is contained in:
Frost Ming 2022-12-01 16:49:56 +08:00 committed by Frost Ming
parent 4e10647999
commit 96ace73b1d
No known key found for this signature in database
GPG key ID: 5BFA9CB4DDA943BF
2 changed files with 19 additions and 11 deletions

View file

@ -1318,7 +1318,8 @@ def __call__(self, parser, namespace, values, option_string=None):
for key, value in vars(subnamespace).items(): for key, value in vars(subnamespace).items():
if key != '__defaults__': if key != '__defaults__':
setattr(namespace, key, value) setattr(namespace, key, value)
namespace.__defaults__.update(subnamespace.__defaults__) if hasattr(namespace, '__defaults__'):
namespace.__defaults__.update(subnamespace.__defaults__)
if arg_strings: if arg_strings:
if not hasattr(namespace, _UNRECOGNIZED_ARGS_ATTR): if not hasattr(namespace, _UNRECOGNIZED_ARGS_ATTR):
@ -1432,6 +1433,12 @@ def __contains__(self, key):
return key in self.__dict__ or key in self.__defaults__ return key in self.__dict__ or key in self.__defaults__
def _set_default(namespace, dest, value):
if not hasattr(namespace, '__defaults__'):
setattr(namespace, dest, value)
else:
namespace.__defaults__[dest] = value
class _ActionsContainer(object): class _ActionsContainer(object):
def __init__(self, def __init__(self,
@ -2042,13 +2049,13 @@ def _parse_known_args2(self, args, namespace, intermixed):
# add any action defaults that aren't present # add any action defaults that aren't present
for action in self._actions: for action in self._actions:
if action.dest is not SUPPRESS: if action.dest is not SUPPRESS:
if action.default is not SUPPRESS and action.dest not in namespace: if action.default is not SUPPRESS and not hasattr(namespace, action.dest):
namespace.__defaults__[action.dest] = action.default _set_default(namespace, action.dest, action.default)
# add any parser defaults that aren't present # add any parser defaults that aren't present
for dest in self._defaults: for dest in self._defaults:
if dest not in namespace: if not hasattr(namespace, dest):
namespace.__defaults__[dest] = self._defaults[dest] _set_default(namespace, dest, self._defaults[dest])
# parse the arguments and exit if there are any errors # parse the arguments and exit if there are any errors
if self.exit_on_error: if self.exit_on_error:
@ -2342,11 +2349,12 @@ def consume_positionals(start_index):
# parsing arguments to avoid calling convert functions # parsing arguments to avoid calling convert functions
# twice (which may fail) if the argument was given, but # twice (which may fail) if the argument was given, but
# only if it was defined already in the namespace # only if it was defined already in the namespace
if (isinstance(action.default, str) and if (action.default is not None and
action.dest in namespace and isinstance(action.default, str) and
getattr(namespace, action.dest) is action.default): hasattr(namespace, action.dest) and
namespace.__defaults__[action.dest] = self._get_value( action.default is getattr(namespace, action.dest)):
action, action.default) _set_default(namespace, action.dest,
self._get_value(action, action.default))
if required_actions: if required_actions:
raise ArgumentError(None, _('the following arguments are required: %s') % raise ArgumentError(None, _('the following arguments are required: %s') %

View file

@ -1 +1 @@
Fix an issue that subparsers defaults override the existing values in the argparse Namespace. Fix an issue that subparsers defaults override the existing values in the argparse Namespace.