Skip to content

Commit 8f6517f

Browse files
authored
perf: make plugin load faster (#116)
1 parent f04c451 commit 8f6517f

File tree

5 files changed

+36
-29
lines changed

5 files changed

+36
-29
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v4.6.0
3+
rev: v5.0.0
44
hooks:
55
- id: check-yaml
66

@@ -10,7 +10,7 @@ repos:
1010
- id: isort
1111

1212
- repo: https://github.com/psf/black
13-
rev: 24.8.0
13+
rev: 24.10.0
1414
hooks:
1515
- id: black
1616
name: black
@@ -22,13 +22,13 @@ repos:
2222
additional_dependencies: [flake8-breakpoint, flake8-print, flake8-pydantic]
2323

2424
- repo: https://github.com/pre-commit/mirrors-mypy
25-
rev: v1.11.1
25+
rev: v1.13.0
2626
hooks:
2727
- id: mypy
2828
additional_dependencies: [types-PyYAML, types-requests, types-setuptools, pydantic]
2929

3030
- repo: https://github.com/executablebooks/mdformat
31-
rev: 0.7.17
31+
rev: 0.7.18
3232
hooks:
3333
- id: mdformat
3434
additional_dependencies: [mdformat-gfm, mdformat-frontmatter, mdformat-pyproject]

ape_foundry/__init__.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,22 @@
44
"""
55

66
from ape import plugins
7-
from ape.api.networks import LOCAL_NETWORK_NAME
8-
from ape_ethereum.ecosystem import NETWORKS
9-
10-
from .provider import (
11-
FoundryForkProvider,
12-
FoundryNetworkConfig,
13-
FoundryProvider,
14-
FoundryProviderError,
15-
FoundrySubprocessError,
16-
)
177

188

199
@plugins.register(plugins.Config)
2010
def config_class():
11+
from .provider import FoundryNetworkConfig
12+
2113
return FoundryNetworkConfig
2214

2315

2416
@plugins.register(plugins.ProviderPlugin)
2517
def providers():
18+
from ape.api.networks import LOCAL_NETWORK_NAME
19+
from ape_ethereum.ecosystem import NETWORKS
20+
21+
from .provider import FoundryForkProvider, FoundryProvider
22+
2623
yield "ethereum", LOCAL_NETWORK_NAME, FoundryProvider
2724

2825
for network in NETWORKS:
@@ -61,6 +58,12 @@ def providers():
6158
yield "blast", "sepolia-fork", FoundryForkProvider
6259

6360

61+
def __getattr__(name: str):
62+
import ape_foundry.provider as module
63+
64+
return getattr(module, name)
65+
66+
6467
__all__ = [
6568
"FoundryNetworkConfig",
6669
"FoundryProvider",

ape_foundry/provider.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import shutil
44
from bisect import bisect_right
55
from subprocess import PIPE, call
6-
from typing import Literal, Optional, Union, cast
6+
from typing import TYPE_CHECKING, Literal, Optional, Union, cast
77

88
from ape.api import (
99
BlockAPI,
@@ -23,7 +23,6 @@
2323
VirtualMachineError,
2424
)
2525
from ape.logging import logger
26-
from ape.types import AddressType, BlockID, ContractCode, SnapshotID
2726
from ape.utils import cached_property
2827
from ape_ethereum.provider import Web3Provider
2928
from ape_ethereum.trace import TraceApproach, TransactionTrace
@@ -54,6 +53,10 @@
5453
except ImportError:
5554
Optimism = None # type: ignore
5655

56+
if TYPE_CHECKING:
57+
from ape.types import AddressType, BlockID, ContractCode, SnapshotID
58+
59+
5760
EPHEMERAL_PORTS_START = 49152
5861
EPHEMERAL_PORTS_END = 60999
5962
DEFAULT_PORT = 8545
@@ -136,7 +139,7 @@ class FoundryProvider(SubprocessProvider, Web3Provider, TestProviderAPI):
136139
_did_warn_wrong_node = False
137140

138141
@property
139-
def unlocked_accounts(self) -> list[AddressType]:
142+
def unlocked_accounts(self) -> list["AddressType"]:
140143
return list(self.account_manager.test_accounts._impersonated_accounts)
141144

142145
@property
@@ -485,7 +488,7 @@ def build_command(self) -> list[str]:
485488

486489
return cmd
487490

488-
def set_balance(self, account: AddressType, amount: Union[int, float, str, bytes]):
491+
def set_balance(self, account: "AddressType", amount: Union[int, float, str, bytes]):
489492
is_str = isinstance(amount, str)
490493
is_key_word = is_str and " " in amount # type: ignore
491494
_is_hex = is_str and not is_key_word and amount.startswith("0x") # type: ignore
@@ -516,19 +519,19 @@ def mine(self, num_blocks: int = 1):
516519
def snapshot(self) -> str:
517520
return self.make_request("evm_snapshot", [])
518521

519-
def restore(self, snapshot_id: SnapshotID) -> bool:
522+
def restore(self, snapshot_id: "SnapshotID") -> bool:
520523
snapshot_id = to_hex(snapshot_id) if isinstance(snapshot_id, int) else snapshot_id
521524
result = self.make_request("evm_revert", [snapshot_id])
522525
return result is True
523526

524-
def unlock_account(self, address: AddressType) -> bool:
527+
def unlock_account(self, address: "AddressType") -> bool:
525528
self.make_request("anvil_impersonateAccount", [address])
526529
return True
527530

528-
def relock_account(self, address: AddressType):
531+
def relock_account(self, address: "AddressType"):
529532
self.make_request("anvil_stopImpersonatingAccount", [address])
530533

531-
def get_balance(self, address: AddressType, block_id: Optional[BlockID] = None) -> int:
534+
def get_balance(self, address: "AddressType", block_id: Optional["BlockID"] = None) -> int:
532535
if result := self.make_request("eth_getBalance", [address, block_id]):
533536
return int(result, 16) if isinstance(result, str) else result
534537

@@ -645,7 +648,7 @@ def _extract_custom_error(self, **kwargs) -> str:
645648
def set_block_gas_limit(self, gas_limit: int) -> bool:
646649
return self.make_request("evm_setBlockGasLimit", [hex(gas_limit)]) is True
647650

648-
def set_code(self, address: AddressType, code: ContractCode) -> bool:
651+
def set_code(self, address: "AddressType", code: "ContractCode") -> bool:
649652
if isinstance(code, bytes):
650653
code = code.hex()
651654

@@ -658,7 +661,7 @@ def set_code(self, address: AddressType, code: ContractCode) -> bool:
658661
self.make_request("anvil_setCode", [address, code])
659662
return True
660663

661-
def set_storage(self, address: AddressType, slot: int, value: HexBytes):
664+
def set_storage(self, address: "AddressType", slot: int, value: HexBytes):
662665
self.make_request(
663666
"anvil_setStorageAt",
664667
[
@@ -712,7 +715,7 @@ def evm_version(self) -> Optional[str]:
712715

713716
return self.settings.evm_version
714717

715-
def get_block(self, block_id: BlockID) -> BlockAPI:
718+
def get_block(self, block_id: "BlockID") -> BlockAPI:
716719
if isinstance(block_id, str) and block_id.isnumeric():
717720
block_id = int(block_id)
718721

setup.cfg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ exclude =
44
venv*
55
docs
66
build
7-
ignore = E704,W503,PYD002
7+
ignore = E704,W503,PYD002,TC003,TC006
88
per-file-ignores =
99
# The traces have to be formatted this way for the tests.
1010
tests/expected_traces.py: E501
11+
type-checking-pydantic-enabled = True

setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
"ape-optimism", # For Optimism integration tests
1616
],
1717
"lint": [
18-
"black>=24.8.0,<25", # Auto-formatter and linter
19-
"mypy>=1.11.1,<2", # Static type analyzer
18+
"black>=24.10.0,<25", # Auto-formatter and linter
19+
"mypy>=1.13.0,<2", # Static type analyzer
2020
"types-setuptools", # Needed for mypy type shed
2121
"types-requests", # Needed for mypy type shed
2222
"types-PyYAML", # Needed for mypy type shed
@@ -25,7 +25,7 @@
2525
"flake8-print>=5.0.0,<6", # Detect print statements left in code
2626
"flake8-pydantic", # For detecting issues with Pydantic models
2727
"isort>=5.13.2,<6", # Import sorting linter
28-
"mdformat>=0.7.17", # Auto-formatter for markdown
28+
"mdformat>=0.7.18", # Auto-formatter for markdown
2929
"mdformat-gfm>=0.3.5", # Needed for formatting GitHub-flavored markdown
3030
"mdformat-frontmatter>=0.4.1", # Needed for frontmatters-style headers in issue templates
3131
"mdformat-pyproject>=0.0.1", # Allows configuring in pyproject.toml

0 commit comments

Comments
 (0)