def test_inequality_no_decorator(self): def foo(arg1: str, arg2: str) -> typing.Tuple[str, str]: return (arg1, arg2) def bar(arg1, arg2): return (arg1, arg2) self.assertNotEquals(inspect_object(foo), inspect_object(bar))
def test_inequality_decorated(self): def foo(arg1: str, arg2: str) -> typing.Tuple[str, str]: return (arg1, arg2) @argument("arg1", type=int) @argument("arg2", type=int) def bar(arg1, arg2): return (arg1, arg2) self.assertNotEquals(inspect_object(foo), inspect_object(bar))
def test_equality_no_decorator(self): def foo(arg1: typing.Any, arg2: str) -> typing.Tuple[str, str]: return (arg1, arg2) @argument("arg1", type=typing.Any) @argument("arg2", type=str) def bar(arg1, arg2): return (arg1, arg2) self.assertEquals(inspect_object(foo), inspect_object(bar))
def test_equality_decorated(self): @argument("arg1", description="arg1 desc") @argument("arg2", description="arg2 desc") def foo(arg1: typing.Any, arg2: str) -> typing.Tuple[str, str]: return (arg1, arg2) @argument("arg1", type=typing.Any, description="arg1 desc") @argument("arg2", type=str, description="arg2 desc") def bar(arg1, arg2): return (arg1, arg2) self.assertEquals(inspect_object(foo), inspect_object(bar))
def __init__(self, fn): self._built_in = False self._fn = fn if not callable(fn): raise ValueError("fn argument must be a callable") self._obj_metadata = inspect_object(fn) self._is_super_command = len(self.metadata.subcommands) > 0 self._subcommand_names = [] # We never expect a function to be passed here that has a self argument # In that case, we should get a bound method if "self" in self.metadata.arguments and not inspect.ismethod( self._fn): raise ValueError("Expecting either a function (eg. bar) or " "a bound method (eg. Foo().bar). " "You passed what appears to be an unbound method " "(eg. Foo.bar) it has a 'self' argument: %s" % function_to_str(fn)) if not self.metadata.command: raise ValueError("function or class {} needs to be annotated with " "@command".format(function_to_str(fn))) # If this is a super command, we need a completer for sub-commands if self.super_command: self._commands_completer = WordCompleter([], ignore_case=True, sentence=True) for _, inspection in self.metadata.subcommands: _sub_name = inspection.command.name self._commands_completer.words.append(_sub_name) self._commands_completer.meta_dict[_sub_name] = dedent( inspection.command.help).strip() self._subcommand_names.append(_sub_name)
def test_inspect_class(self): @command class SuperCommand: """SuperHelp""" @command def my_function(self, arg1: str, argument_2: int): """HelpMessage""" pass data = inspect_object(SuperCommand) cmd = data.command args = data.arguments self.assertEqual("super-command", cmd.name) self.assertEqual("SuperHelp", cmd.help) self.assertEqual(0, len(args.keys())) self.assertEqual(1, len(data.subcommands)) subcmd_attr, subcmd_insp = data.subcommands[0] self.assertEqual("my_function", subcmd_attr) subcmd = subcmd_insp.command self.assertEqual("my-function", subcmd.name) self.assertEqual("HelpMessage", subcmd.help) subargs = subcmd_insp.arguments self.assertEqual(2, len(subargs)) self.assertTrue("arg1" in subargs.keys()) self.assertTrue("argument-2" in subargs.keys())
def test_inspect_no_docstring(self): @command class SuperCommand: """SuperHelp""" @command def my_function(self, arg1: str): pass data = inspect_object(SuperCommand) self.assertListEqual([], data.subcommands)
def wrapper(*args: Any, **kwargs: Dict[str, Any]): warning: str = ("[WARNING] The `{command}` command is deprecated " "and will be eventually removed".format( command=inspect_object(command).command.name)) cprint(warning, "yellow") if message is not None: cprint(message, "yellow") elif superseded_by is not None: cprint("Use `{}` command instead".format(superseded_by), "yellow") else: assert False, "Unreachable" return command(*args, **kwargs)
def test_inspect_function1(self): @command def my_function(arg1: str, argument_2: int): """HelpMessage""" pass data = inspect_object(my_function) cmd = data.command args = data.arguments self.assertEqual("my-function", cmd.name) self.assertEqual("HelpMessage", cmd.help) self.assertEqual(2, len(args)) self.assertTrue("arg1" in args.keys()) self.assertTrue("argument-2" in args.keys())