diff --git a/simplecli/simplecli.py b/simplecli/simplecli.py index e8127b8..dba053a 100644 --- a/simplecli/simplecli.py +++ b/simplecli/simplecli.py @@ -410,19 +410,18 @@ def wrap(func: Callable[..., Any]) -> Callable[..., Any]: def code_to_ordered_params(code: Callable[..., Any]) -> OrderedDict: signature = inspect.signature(code) - empty = inspect._empty - return OrderedDict( - ( - k, - Param( - name=v.name, - default=Empty if v.default is empty else v.default, - annotation=Empty if v.annotation is empty else v.annotation, - kind=v.kind, - ), + result = OrderedDict() + + for k, v in signature.parameters.items(): + if v.annotation is inspect._empty: + exit("ERROR: All wrapped function parameters need type hints!") + result[k] = Param( + name=v.name, + default=Empty if v.default is inspect._empty else v.default, + annotation=v.annotation, + kind=v.kind, ) - for k, v in signature.parameters.items() - ) + return result def process_comment( diff --git a/tests/test_wrap.py b/tests/test_wrap.py index 695c72b..634bbfa 100644 --- a/tests/test_wrap.py +++ b/tests/test_wrap.py @@ -233,3 +233,36 @@ def test_directly_called_wrap(monkeypatch): assert nt.code(1, foo="test") == "2 test!" assert nt.code(test_id=2, foo="blarg") == "3 blarg!" + + +def test_wrap_no_typehint_(monkeypatch): + monkeypatch.setattr(sys, "argv", ["filename", "123"]) + + def code1(foo): + pass + + with pytest.raises(SystemExit, match="parameters need type hints!"): + code1.__globals__["__name__"] = "__main__" + simplecli.wrap(code1) + + +def test_wrap_no_typehint_no_arg(monkeypatch): + monkeypatch.setattr(sys, "argv", ["filename"]) + + def code1(foo): + pass + + with pytest.raises(SystemExit, match="ERROR: All wrapped function "): + code1.__globals__["__name__"] = "__main__" + simplecli.wrap(code1) + + +def test_wrap_no_typehint_kwarg(monkeypatch): + monkeypatch.setattr(sys, "argv", ["filename", "--foo"]) + + def code1(foo): + pass + + with pytest.raises(SystemExit, match="function parameters need type"): + code1.__globals__["__name__"] = "__main__" + simplecli.wrap(code1)