Make sure @cmdutils.argument after @register fails

This commit is contained in:
Florian Bruhin 2016-05-10 19:09:07 +02:00
parent 3c586f34ff
commit 9eeaeb95c3
3 changed files with 25 additions and 1 deletions

View File

@ -185,8 +185,10 @@ class argument: # pylint: disable=invalid-name
self._kwargs = kwargs
def __call__(self, func):
funcname = func.__name__
if self._argname not in inspect.signature(func).parameters:
raise ValueError("{} has no argument {}!".format(func.__name__,
raise ValueError("{} has no argument {}!".format(funcname,
self._argname))
# Fill up args which weren't passed
@ -196,6 +198,10 @@ class argument: # pylint: disable=invalid-name
if not hasattr(func, 'qute_args'):
func.qute_args = {}
elif func.qute_args is None:
raise ValueError("@cmdutils.argument got called above (after) "
"@cmdutils.register for {}!".format(funcname))
func.qute_args[self._argname] = command.ArgInfo(**self._kwargs)
return func

View File

@ -128,6 +128,10 @@ class Command:
args = self._inspect_func()
# This is checked by future @cmdutils.argument calls so they fail
# (as they'd be silently ignored otherwise)
self.handler.qute_args = None
if self.completion is not None and len(self.completion) > len(args):
raise ValueError("Got {} completions, but only {} "
"arguments!".format(len(self.completion),

View File

@ -260,3 +260,17 @@ class TestArgument:
'bar': self._arginfo(flag='y')
}
assert fun.qute_args == expected
def test_wrong_order(self):
"""When @cmdutils.argument is used above (after) @register, fail."""
with pytest.raises(ValueError) as excinfo:
@cmdutils.argument('bar', flag='y')
@cmdutils.register()
def fun(bar):
"""Blah."""
pass
text = ("@cmdutils.argument got called above (after) "
"@cmdutils.register for fun!")
assert str(excinfo.value) == text