Skip to content

[Feature Request] Support for lazy loading commands #97

Open
@svew

Description

@svew

I have set up a Click CLI for my team that lazily loads commands, which is going to be very useful for scaling up how many commands our team can add to the tool without slowing the whole thing down. However, it doesn't seem to work with Trogon, which I'd very much like to give us the option of using.

image

The way this works for us mechanically is somewhat custom, though recommended by the Click documentation. Essentially, instead of loading python modules containing commands thru imports, we load them dynamically when the command or its info is needed. This LazyCommand class is a dummy wrapper around the real command, and only knows the command's name and short_help.

It looks like from the code (looking at introspect.py), Trogon obtains info about the Click command/group objects from their fields, not from their getter functions. Lazy loading relies on these getter functions being called to know when it needs to actual load the module and supply correct information about the command.

I'm thinking instead of accessing the Click command's fields directly to instead call the to_info_dict function , which seems to return all necessary info, and is interceptible by lazy loading. Or at least call it once so that lazy loading can work

class Command(BaseCommand):
    ...
    def to_info_dict(self, ctx: Context) -> t.Dict[str, t.Any]:
        info_dict = super().to_info_dict(ctx)
        info_dict.update(
            params=[param.to_info_dict() for param in self.get_params(ctx)],
            help=self.help,
            epilog=self.epilog,
            short_help=self.short_help,
            hidden=self.hidden,
            deprecated=self.deprecated,
        )
        return info_dict

I do have concerns though that the Trogon app might load every command and its info all at once anyway instead of doing so lazily, which would somewhat defeat the point...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions