From a5429c54fe99abfc21e41194b34556cc8ba46171 Mon Sep 17 00:00:00 2001 From: antazoey Date: Thu, 31 Oct 2024 09:15:37 -0500 Subject: [PATCH] perf: only load necessary plugins for CLI (#2358) --- src/ape/_cli.py | 4 +--- src/ape/plugins/_utils.py | 7 +++++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/ape/_cli.py b/src/ape/_cli.py index 545fed61c2..d8a193ee41 100644 --- a/src/ape/_cli.py +++ b/src/ape/_cli.py @@ -65,7 +65,6 @@ def parse_args(self, ctx: "Context", args: list[str]) -> list[str]: def format_commands(self, ctx, formatter) -> None: from ape.plugins._utils import PluginMetadataList - from ape.utils.basemodel import ManagerAccessMixin as access commands = [] for subcommand in self.list_commands(ctx): @@ -86,8 +85,7 @@ def format_commands(self, ctx, formatter) -> None: "Plugin": [], "3rd-Party Plugin": [], } - plugin_manager = access.plugin_manager - pl_metadata = PluginMetadataList.load(plugin_manager, include_available=False) + pl_metadata = PluginMetadataList.from_package_names(f"ape_{c[0]}" for c in commands) for cli_name, cmd in commands: help = cmd.get_short_help_str(limit) plugin = pl_metadata.get_plugin(cli_name, check_available=False) diff --git a/src/ape/plugins/_utils.py b/src/ape/plugins/_utils.py index 123580e288..d161284386 100644 --- a/src/ape/plugins/_utils.py +++ b/src/ape/plugins/_utils.py @@ -4,7 +4,7 @@ from enum import Enum from functools import cached_property from shutil import which -from typing import Any, Optional +from typing import TYPE_CHECKING, Any, Optional from urllib.parse import urlparse import click @@ -19,6 +19,9 @@ from ape.utils.misc import _get_distributions, get_package_version, log_instead_of_fail from ape.version import version as ape_version_str +if TYPE_CHECKING: + from ape.managers.plugins import PluginManager + # Plugins maintained OSS by ApeWorX (and trusted) # Use `uv pip` if installed, otherwise `python -m pip` PIP_COMMAND = ["uv", "pip"] if which("uv") else [sys.executable, "-m", "pip"] @@ -202,7 +205,7 @@ class PluginMetadataList(BaseModel): third_party: "PluginGroup" @classmethod - def load(cls, plugin_manager, include_available: bool = True): + def load(cls, plugin_manager: "PluginManager", include_available: bool = True): plugins = plugin_manager.registered_plugins if include_available: plugins = plugins.union(github_client.available_plugins)