diff options
author | Eric Lin <anselor@gmail.com> | 2020-06-12 20:44:10 -0400 |
---|---|---|
committer | anselor <anselor@gmail.com> | 2020-08-04 13:38:08 -0400 |
commit | 6da2cf30311f97d23a7121f8c02f9123674194b4 (patch) | |
tree | 9d6780afcb0742b94f86b6f8f775220691896cd3 /cmd2/command_definition.py | |
parent | e1087b8f29341397b09e9a0722a77c025ab20d23 (diff) | |
download | cmd2-git-6da2cf30311f97d23a7121f8c02f9123674194b4.tar.gz |
Some minor cleanup of how imports work. Fixed issue with help documentation for CommandSet commands.
Issue #943
Diffstat (limited to 'cmd2/command_definition.py')
-rw-r--r-- | cmd2/command_definition.py | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/cmd2/command_definition.py b/cmd2/command_definition.py index f08040bb..a235525d 100644 --- a/cmd2/command_definition.py +++ b/cmd2/command_definition.py @@ -29,10 +29,15 @@ Registered command tuples. (command, do_ function, complete_ function, help_ fun """ -class _PartialPassthru(functools.partial): +def _partial_passthru(func: Callable, *args, **kwargs) -> functools.partial: """ - Wrapper around partial function that passes through getattr, setattr, and dir to the wrapped function. - This allows for CommandSet functions to be wrapped while maintaining the decorated properties + Constructs a partial function that passes arguments through to the wrapped function. + Must construct a new type every time so that each wrapped function's __doc__ can be copied correctly. + + :param func: wrapped function + :param args: positional arguments + :param kwargs: keyword arguments + :return: partial function that exposes attributes of wrapped function """ def __getattr__(self, item): return getattr(self.func, item) @@ -43,6 +48,16 @@ class _PartialPassthru(functools.partial): def __dir__(self) -> Iterable[str]: return dir(self.func) + passthru_type = type('PassthruPartial' + func.__name__, + (functools.partial,), + { + '__getattr__': __getattr__, + '__setattr__': __setattr__, + '__dir__': __dir__, + }) + passthru_type.__doc__ = func.__doc__ + return passthru_type(func, *args, **kwargs) + def register_command(cmd_func: Callable[['Cmd', Union['Statement', 'argparse.Namespace']], None]): """ |