Fix handling of optional values in _add_special_arg
That way, we can pass None as a valid value.
This commit is contained in:
parent
34aaca2aa0
commit
23d0dbd998
@ -350,7 +350,7 @@ class Command:
|
|||||||
raise ValueError("Invalid scope {}!".format(scope))
|
raise ValueError("Invalid scope {}!".format(scope))
|
||||||
return objreg.get(name, scope=scope, window=win_id, tab=tab_id)
|
return objreg.get(name, scope=scope, window=win_id, tab=tab_id)
|
||||||
|
|
||||||
def _add_special_arg(self, *, value, param, args, kwargs, optional=False):
|
def _add_special_arg(self, *, value, param, args, kwargs):
|
||||||
"""Add a special argument value to a function call.
|
"""Add a special argument value to a function call.
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
@ -358,18 +358,11 @@ class Command:
|
|||||||
param: The parameter being filled.
|
param: The parameter being filled.
|
||||||
args: The positional argument list. Gets modified directly.
|
args: The positional argument list. Gets modified directly.
|
||||||
kwargs: The keyword argument dict. Gets modified directly.
|
kwargs: The keyword argument dict. Gets modified directly.
|
||||||
optional: Whether the value can be optional.
|
|
||||||
"""
|
"""
|
||||||
if not optional:
|
|
||||||
assert value is not None
|
|
||||||
if param.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD:
|
if param.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD:
|
||||||
if value is not None:
|
args.append(value)
|
||||||
args.append(value)
|
|
||||||
else:
|
|
||||||
args.append(param.default)
|
|
||||||
elif param.kind == inspect.Parameter.KEYWORD_ONLY:
|
elif param.kind == inspect.Parameter.KEYWORD_ONLY:
|
||||||
if value is not None:
|
kwargs[param.name] = value
|
||||||
kwargs[param.name] = value
|
|
||||||
else:
|
else:
|
||||||
raise TypeError("{}: invalid parameter type {} for argument "
|
raise TypeError("{}: invalid parameter type {} for argument "
|
||||||
"{!r}!".format(self.name, param.kind, param.name))
|
"{!r}!".format(self.name, param.kind, param.name))
|
||||||
@ -388,7 +381,7 @@ class Command:
|
|||||||
tab = None
|
tab = None
|
||||||
|
|
||||||
self._add_special_arg(value=tab, param=param, args=args,
|
self._add_special_arg(value=tab, param=param, args=args,
|
||||||
kwargs=kwargs, optional=True)
|
kwargs=kwargs)
|
||||||
|
|
||||||
def _get_param_value(self, param):
|
def _get_param_value(self, param):
|
||||||
"""Get the converted value for an inspect.Parameter."""
|
"""Get the converted value for an inspect.Parameter."""
|
||||||
@ -451,8 +444,13 @@ class Command:
|
|||||||
args=args, kwargs=kwargs)
|
args=args, kwargs=kwargs)
|
||||||
continue
|
continue
|
||||||
elif arg_info.value == usertypes.CommandValue.count:
|
elif arg_info.value == usertypes.CommandValue.count:
|
||||||
self._add_special_arg(value=self._count, param=param,
|
if self._count is None:
|
||||||
args=args, kwargs=kwargs, optional=True)
|
assert param.default is not inspect.Parameter.empty
|
||||||
|
value = param.default
|
||||||
|
else:
|
||||||
|
value = self._count
|
||||||
|
self._add_special_arg(value=value, param=param,
|
||||||
|
args=args, kwargs=kwargs)
|
||||||
continue
|
continue
|
||||||
elif arg_info.value == usertypes.CommandValue.win_id:
|
elif arg_info.value == usertypes.CommandValue.win_id:
|
||||||
self._add_special_arg(value=win_id, param=param,
|
self._add_special_arg(value=win_id, param=param,
|
||||||
|
Loading…
Reference in New Issue
Block a user