diff --git a/ape_alchemy/__init__.py b/ape_alchemy/__init__.py index 11d1dab..714c211 100644 --- a/ape_alchemy/__init__.py +++ b/ape_alchemy/__init__.py @@ -1,41 +1,25 @@ from ape import plugins -from .provider import Alchemy - -NETWORKS = { - "ethereum": [ - "mainnet", - "sepolia", - ], - "arbitrum": [ - "mainnet", - "sepolia", - ], - "base": [ - "mainnet", - "sepolia", - ], - "fantom": [ - "opera", - "testnet", - ], - "optimism": [ - "mainnet", - "sepolia", - ], - "polygon": [ - "mainnet", - "amoy", - ], - "polygon-zkevm": [ - "mainnet", - "cardona", - ], -} - @plugins.register(plugins.ProviderPlugin) def providers(): + from ._utils import NETWORKS + from .provider import Alchemy + for ecosystem_name in NETWORKS: for network_name in NETWORKS[ecosystem_name]: yield ecosystem_name, network_name, Alchemy + + +def __getattr__(name: str): + if name == "NETWORKS": + from ._utils import NETWORKS + + return NETWORKS + + elif name == "Alchemy": + from .provider import Alchemy + + return Alchemy + + raise AttributeError(name) diff --git a/ape_alchemy/_utils.py b/ape_alchemy/_utils.py new file mode 100644 index 0000000..5547f95 --- /dev/null +++ b/ape_alchemy/_utils.py @@ -0,0 +1,30 @@ +NETWORKS = { + "ethereum": [ + "mainnet", + "sepolia", + ], + "arbitrum": [ + "mainnet", + "sepolia", + ], + "base": [ + "mainnet", + "sepolia", + ], + "fantom": [ + "opera", + "testnet", + ], + "optimism": [ + "mainnet", + "sepolia", + ], + "polygon": [ + "mainnet", + "amoy", + ], + "polygon-zkevm": [ + "mainnet", + "cardona", + ], +} diff --git a/ape_alchemy/provider.py b/ape_alchemy/provider.py index 4149d3c..a428ef4 100644 --- a/ape_alchemy/provider.py +++ b/ape_alchemy/provider.py @@ -1,6 +1,6 @@ import os from collections.abc import Iterable -from typing import Any, Optional +from typing import TYPE_CHECKING, Any, Optional from ape.api import ReceiptAPI, TraceAPI, TransactionAPI, UpstreamProvider from ape.exceptions import ( @@ -10,9 +10,7 @@ VirtualMachineError, ) from ape.logging import logger -from ape.types import BlockID from ape_ethereum.provider import Web3Provider -from ape_ethereum.transactions import AccessList from eth_pydantic_types import HexBytes from eth_typing import HexStr from requests import HTTPError @@ -28,6 +26,11 @@ from .exceptions import AlchemyFeatureNotAvailable, AlchemyProviderError, MissingProjectKeyError from .trace import AlchemyTransactionTrace +if TYPE_CHECKING: + from ape.types import BlockID + from ape_ethereum.transactions import AccessList + + # The user must either set one of these or an ENV VAR of the pattern: # WEB3___PROJECT_ID or WEB3___API_KEY DEFAULT_ENVIRONMENT_VARIABLE_NAMES = ("WEB3_ALCHEMY_PROJECT_ID", "WEB3_ALCHEMY_API_KEY") @@ -205,8 +208,8 @@ def get_virtual_machine_error(self, exception: Exception, **kwargs) -> VirtualMa return VirtualMachineError(message=message, txn=txn) def create_access_list( - self, transaction: TransactionAPI, block_id: Optional[BlockID] = None - ) -> list[AccessList]: + self, transaction: TransactionAPI, block_id: Optional["BlockID"] = None + ) -> list["AccessList"]: if self.network.ecosystem.name == "polygon-zkevm": # The error is only 400 with no info otherwise. raise APINotImplementedError() diff --git a/setup.cfg b/setup.cfg index 84f6a9e..83a070c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,7 +1,8 @@ [flake8] max-line-length = 100 -ignore = E704,W503,PYD002 +ignore = E704,W503,PYD002,TC003,TC006 exclude = venv* docs build +type-checking-pydantic-enabled = True diff --git a/tests/conftest.py b/tests/conftest.py index 2844b00..9598eaa 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,11 +1,13 @@ import os +from typing import TYPE_CHECKING import ape import pytest from requests import HTTPError, Response from web3 import Web3 -from ape_alchemy.provider import Alchemy +if TYPE_CHECKING: + from ape_alchemy.provider import Alchemy FEATURE_NOT_AVAILABLE_BECAUSE_OF_TIER_RESPONSE = ( "trace_transaction is not available on the Free tier - " @@ -91,5 +93,5 @@ def feature_not_available_http_error(mocker, request): @pytest.fixture -def alchemy_provider(networks) -> Alchemy: +def alchemy_provider(networks) -> "Alchemy": return networks.ethereum.sepolia.get_provider("alchemy") diff --git a/tests/test_integration.py b/tests/test_integration.py index 5ac0f3b..db5c5e3 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -4,7 +4,7 @@ from ape.exceptions import APINotImplementedError from ape.utils import ZERO_ADDRESS -from ape_alchemy import NETWORKS +from ape_alchemy._utils import NETWORKS from ape_alchemy.provider import NETWORKS_SUPPORTING_WEBSOCKETS, Alchemy