jwutils.Cmd.add_subcommands(): Collect only Cmd

This commit fixes tightening type check fallout.

add_subcommands() with a string pattern collects every class of the calling
module whose name matches the pattern. inspect.getmembers() yields plain
objects, so the unannotated collection list is inferred as list[object],
which is not compatible with the declared list[type[Cmd]] parameter type,
and make check fails on it.

Annotate the collection list as list[type[Cmd]] and narrow it to subclasses
of Cmd via issubclass(). This makes the implementation match the declared
signature and stops classes lacking the Cmd interface from being registered
as subcommands, where they would break later when instantiated and treated
as Cmd in __add_cmd_to_parser().

Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.85.1
This commit is contained in:
Jan Lindemann 2026-09-11 14:59:28 +02:00
commit 63c0ed3951
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61

View file

@ -45,9 +45,9 @@ class Cmd(abc.ABC): # export
def add_subcommands(self, cmd: str | type[Cmd] | list[type[Cmd]]) -> None:
if isinstance(cmd, str):
sc = []
sc: list[type[Cmd]] = []
for name, obj in inspect.getmembers(sys.modules[self.__class__.__module__]):
if inspect.isclass(obj):
if inspect.isclass(obj) and issubclass(obj, Cmd):
if re.search(cmd, str(obj)):
sc.append(obj)
log.slog(log.DEBUG, f"Found subcommand {obj}")