Skip to content

Commit

Permalink
perf: make ape console --help faster (#2366)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Nov 1, 2024
1 parent aa1c02c commit 2515e64
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/ape/cli/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,12 @@ def __init__(

@property
def base_type(self) -> type["ProviderAPI"]:
# perf: property exists to delay import ProviderAPI at init time.
from ape.api.providers import ProviderAPI

if self._base_type is not None:
return self._base_type

# perf: property exists to delay import ProviderAPI at init time.
from ape.api.providers import ProviderAPI

self._base_type = ProviderAPI
return ProviderAPI

Expand Down
11 changes: 8 additions & 3 deletions src/ape/cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,8 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def parse_args(self, ctx: "Context", args: list[str]) -> list[str]:
from ape.api.providers import ProviderAPI

arguments = args # Renamed for better pdb support.
base_type = ProviderAPI if self._use_cls_types else str
base_type: Optional[type] = None if self._use_cls_types else str
if existing_option := next(
iter(
x
Expand All @@ -85,13 +83,20 @@ def parse_args(self, ctx: "Context", args: list[str]) -> list[str]:
),
None,
):
if base_type is None:
from ape.api.providers import ProviderAPI

base_type = ProviderAPI

# Checking instance above, not sure why mypy still mad.
existing_option.type.base_type = base_type # type: ignore

else:
# Add the option automatically.
# NOTE: Local import here only avoids circular import issues.
from ape.cli.options import NetworkOption

# NOTE: None base-type will default to `ProviderAPI`.
option = NetworkOption(base_type=base_type, callback=self._network_callback)
self.params.append(option)

Expand Down

0 comments on commit 2515e64

Please sign in to comment.