Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Support for lazy loading commands #97

Open
svew opened this issue Dec 2, 2024 · 0 comments
Open

[Feature Request] Support for lazy loading commands #97

svew opened this issue Dec 2, 2024 · 0 comments

Comments

@svew
Copy link

svew commented Dec 2, 2024

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant