From 0ef5d338bdf90821daac7a6ebd6339bda7b6ad7e Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Thu, 15 Sep 2016 15:44:33 +0200 Subject: [PATCH] make sure keyword-only arguments have a default Fixes #1872. This prevents inspect.Parameter.empty from slipping through to the command. --- qutebrowser/commands/command.py | 4 ++++ tests/unit/commands/test_cmdutils.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/qutebrowser/commands/command.py b/qutebrowser/commands/command.py index ea1d0d46b..ce9e26eb8 100644 --- a/qutebrowser/commands/command.py +++ b/qutebrowser/commands/command.py @@ -246,6 +246,10 @@ class Command: continue if self._inspect_special_param(param): continue + if (param.kind == inspect.Parameter.KEYWORD_ONLY and + param.default is inspect.Parameter.empty): + raise TypeError("{}: handler has keyword only argument " + "without default!".format(self.name)) typ = self._get_type(param) is_bool = typ is bool kwargs = self._param_to_argparse_kwargs(param, is_bool) diff --git a/tests/unit/commands/test_cmdutils.py b/tests/unit/commands/test_cmdutils.py index 917b94e3e..1aacf3c33 100644 --- a/tests/unit/commands/test_cmdutils.py +++ b/tests/unit/commands/test_cmdutils.py @@ -349,6 +349,24 @@ class TestRegister: with pytest.raises(IndexError): cmd.get_pos_arg_info(2) + def test_keyword_only_without_default(self): + # https://github.com/The-Compiler/qutebrowser/issues/1872 + def fun(*, target): + """Blah.""" + pass + + with pytest.raises(TypeError): + fun = cmdutils.register()(fun) + + def test_typed_keyword_only_without_default(self): + # https://github.com/The-Compiler/qutebrowser/issues/1872 + def fun(*, target: int): + """Blah.""" + pass + + with pytest.raises(TypeError): + fun = cmdutils.register()(fun) + class TestArgument: