Skip to content

Commit

Permalink
fix: mypy type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jnoortheen committed Mar 16, 2020
1 parent d690638 commit 522954c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
13 changes: 7 additions & 6 deletions arger/decorator.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from argparse import ArgumentParser
from typing import Any, Callable, Dict, TypeVar, Optional
from typing import Any, Callable, Dict, Optional, TypeVar

from .parser import opterate


CMD = "commands"
F = TypeVar('F', bound=Callable[..., Any])

Expand Down Expand Up @@ -42,7 +43,7 @@ def install(code:int):
def __init__(self, desc: Optional[str] = None, add_help=True):
self.desc = desc
self.add_help = add_help
self.funcs = {} # type: Dict[str, Any]
self.funcs = {} # type: Dict[str, Any]

@property
def first_func(self):
Expand All @@ -64,12 +65,12 @@ def dispatch(self, *args):
raise NotImplementedError("No function added.")

parser = self.get_parser()
args = vars(parser.parse_args(args))
kwargs = vars(parser.parse_args(args)) # type: Dict[str, Any]
if len(self.funcs) == 1:
return self.first_func(**args)
return self.first_func(**kwargs)

func = self.funcs[args[CMD]]
return func(**args)
func = self.funcs[kwargs[CMD]]
return func(**kwargs)

def __call__(self, func: F) -> F:
"""Decorator.
Expand Down
2 changes: 2 additions & 0 deletions tests/examples/arger_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
arg = Arger("Test Commands")


# pylint: disable=redefined-builtin
# noinspection PyShadowingBuiltins
@arg
def list(dirname):
"""List contents
Expand Down
26 changes: 26 additions & 0 deletions tests/test_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest
from arger import Arger

from .examples.doc_types import google_doc


@pytest.fixture
def arg():
return Arger("Test")


def test_noargs(capsys, arg):
"""Ensure a method that expects no arguments runs properly when it receives
none."""
arg(google_doc) # equivalent of calling with decorator
with pytest.raises(SystemExit):
arg.dispatch() # start function

capture = capsys.readouterr()
assert (
capture.err
== """\
usage: pytest [-h] [-k KW1] [-w] param1 param2
pytest: error: the following arguments are required: param1, param2
"""
)

0 comments on commit 522954c

Please sign in to comment.