Description
When using the @with_argparser
decorator inside any class that subclasses cmd2.Cmd
, mypy will flag this as an error, stating that the callable returned by the wrapper function has been given a wrong argument type.
Minimal example:
import argparse
import cmd2
class MyOwnCommandUI(cmd2.Cmd):
parser = argparse.ArgumentParser()
@cmd2.with_argparser(parser)
def do_something(self, args: argparse.Namespace) -> None:
...
mypy 1.4.1 will issue the following error:
error: Argument 1 has incompatible type "Callable[[MyOwnCommandUI, Namespace], None]";
expected "Callable[[Cmd, Namespace], bool | None] | Callable[[CommandSet, Namespace], bool | None] |
Callable[[Cmd, Namespace], bool] | Callable[[CommandSet, Namespace], bool] | Callable[[Cmd, Namespace], None] |
Callable[[CommandSet, Namespace], None]" [arg-type]
In fact, this behavior can also be seen when running mypy against the cmd2 decorator example and other examples using the decorator.
It appears to me that the problem here is that the ArgparseCommandFunc
type alias only allows for cmd2.Cmd
instances, but not subclasses thereof. This seems to be related to what is described in this StackOverflow issue that describes a roughly similar issue, I believe: How to make Mypy deal with subclasses in functions as expected
Now, I have no idea whether this is by design or if this can even be fixed with reasonable amount of effort, but I figured I'd bring it up for potential future versions. If there is something that I can do about it on my end (outside of # type: ignore
of course), I would be interested in that as well!