Skip to content

Commit

Permalink
Fixes #1 - Fail fast on missing type hints (#7)
Browse files Browse the repository at this point in the history
Clearer messaging regarding missing type hints
  • Loading branch information
inno authored May 19, 2024
1 parent ab93820 commit d803dc8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
23 changes: 11 additions & 12 deletions simplecli/simplecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
33 changes: 33 additions & 0 deletions tests/test_wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit d803dc8

Please sign in to comment.