diff --git a/docs/methoddocs/utils.md b/docs/methoddocs/utils.md index 9eb96e3cee..09bafdfbf3 100644 --- a/docs/methoddocs/utils.md +++ b/docs/methoddocs/utils.md @@ -1,8 +1,65 @@ # ape.utils +## ABI + +```{eval-rst} +.. automodule:: ape.utils.abi + :members: + :show-inheritance: +``` + +## Basemodel + +```{eval-rst} +.. automodule:: ape.utils.basemodel + :members: + :show-inheritance: +``` + +## Miscellaneous + +```{eval-rst} +.. automodule:: ape.utils.misc + :members: + :show-inheritance: +``` + +## OS + +```{eval-rst} +.. automodule:: ape.utils.os + :members: + :show-inheritance: +``` + +## Process + +```{eval-rst} +.. automodule:: ape.utils.process + :members: + :show-inheritance: +``` + +## RPC + +```{eval-rst} +.. automodule:: ape.utils.rpc + :members: + :show-inheritance: +``` + +## Testing + +```{eval-rst} +.. automodule:: ape.utils.testing + :members: + :show-inheritance: +``` + +## Trace + ```{eval-rst} -.. automodule:: ape.utils +.. automodule:: ape.utils.trace :members: :show-inheritance: - :exclude-members: abstractmethod, dataclass, __init__ ``` diff --git a/src/ape/api/__init__.py b/src/ape/api/__init__.py index 8ce7a497fd..b3c2bbfb75 100644 --- a/src/ape/api/__init__.py +++ b/src/ape/api/__init__.py @@ -1,27 +1,79 @@ -from .accounts import ( - AccountAPI, - AccountContainerAPI, - ImpersonatedAccount, - TestAccountAPI, - TestAccountContainerAPI, -) -from .address import Address -from .compiler import CompilerAPI -from .config import ConfigDict, ConfigEnum, PluginConfig -from .convert import ConverterAPI -from .explorers import ExplorerAPI -from .networks import ( - EcosystemAPI, - ForkedNetworkAPI, - NetworkAPI, - ProviderContextManager, - create_network_type, -) -from .projects import DependencyAPI, ProjectAPI -from .providers import BlockAPI, ProviderAPI, SubprocessProvider, TestProviderAPI, UpstreamProvider -from .query import QueryAPI, QueryType -from .trace import TraceAPI -from .transactions import ReceiptAPI, TransactionAPI +def __getattr__(name: str): + if name in ( + "AccountAPI", + "AccountContainerAPI", + "ImpersonatedAccount", + "TestAccountAPI", + "TestAccountContainerAPI", + ): + import ape.api.accounts as accounts_module + + return getattr(accounts_module, name) + + elif name in ("Address",): + import ape.api.address as address_module + + return getattr(address_module, name) + + elif name in ("CompilerAPI",): + import ape.api.compiler as compiler_module + + return getattr(compiler_module, name) + + elif name in ("ConfigDict", "ConfigEnum", "PluginConfig"): + import ape.api.config as config_module + + return getattr(config_module, name) + + elif name in ("ConverterAPI",): + import ape.api.convert as convert_module + + return getattr(convert_module, name) + + elif name in ("ExplorerAPI",): + import ape.api.explorers as explorer_module + + return getattr(explorer_module, name) + + elif name in ("BlockAPI, ProviderAPI, SubprocessProvider, TestProviderAPI, UpstreamProvider"): + import ape.api.providers as provider_module + + return getattr(provider_module, name) + + elif name in ( + "EcosystemAPI", + "ForkedNetworkAPI", + "NetworkAPI", + "ProviderContextManager", + "create_network_type", + ): + import ape.api.networks as network_module + + return getattr(network_module, name) + + elif name in ("DependencyAPI", "ProjectAPI"): + import ape.api.projects as project_module + + return getattr(project_module, name) + + elif name in ("QueryAPI", "QueryType"): + import ape.api.query as query_module + + return getattr(query_module, name) + + elif name in ("TraceAPI",): + import ape.api.trace as trace_module + + return getattr(trace_module, name) + + elif name in ("ReceiptAPI", "TransactionAPI"): + import ape.api.transactions as tx_module + + return getattr(tx_module, name) + + else: + raise AttributeError(name) + __all__ = [ "AccountAPI", diff --git a/src/ape/types/__init__.py b/src/ape/types/__init__.py index 0b4cc07792..4b97d97bfa 100644 --- a/src/ape/types/__init__.py +++ b/src/ape/types/__init__.py @@ -1,42 +1,97 @@ -from eth_pydantic_types import HexBytes -from ethpm_types import ( - ABI, - Bytecode, - Checksum, - Compiler, - ContractType, - PackageManifest, - PackageMeta, - Source, -) -from ethpm_types.source import Closure - -from ape.types.address import AddressType, RawAddress -from ape.types.basic import HexInt, _LazySequence -from ape.types.coverage import ( - ContractCoverage, - ContractSourceCoverage, - CoverageProject, - CoverageReport, - CoverageStatement, -) -from ape.types.events import ContractLog, ContractLogContainer, LogFilter, MockContractLog -from ape.types.gas import AutoGasLimit, GasLimit -from ape.types.signatures import MessageSignature, SignableMessage, TransactionSignature -from ape.types.trace import ContractFunctionPath, ControlFlow, GasReport, SourceTraceback -from ape.types.units import CurrencyValue, CurrencyValueComparable -from ape.types.vm import BlockID, ContractCode, SnapshotID -from ape.utils.basemodel import ( - BaseInterface, - BaseInterfaceModel, - BaseModel, - ExtraAttributesMixin, - ExtraModelAttributes, - ManagerAccessMixin, - get_attribute_with_extras, - get_item_with_extras, - only_raise_attribute_error, -) +def __getattr__(name: str): + if name in ("HexBytes",): + from eth_pydantic_types import HexBytes + + return HexBytes + + elif name in ( + "ABI", + "Bytecode", + "Checksum", + "Compiler", + "ContractType", + "PackageManifest", + "PackageMeta", + "Source", + ): + import ethpm_types + + return getattr(ethpm_types, name) + + elif name in ("Closure",): + from ethpm_types.source import Closure + + return Closure + + elif name in ("AddressType", "RawAddress"): + import ape.types.address as address_module + + return getattr(address_module, name) + + elif name in ("HexInt", "_LazySequence"): + import ape.types.basic as basic_module + + return getattr(basic_module, name) + + elif name in ( + "ContractCoverage", + "ContractSourceCoverage", + "CoverageProject", + "CoverageReport", + "CoverageStatement", + ): + import ape.types.coverage as coverage_module + + return getattr(coverage_module, name) + + elif name in ("ContractLog", "ContractLogContainer", "LogFilter", "MockContractLog"): + import ape.types.events as events_module + + return getattr(events_module, name) + + elif name in ("AutoGasLimit", "GasLimit"): + import ape.types.gas as gas_module + + return getattr(gas_module, name) + + elif name in ("MessageSignature", "SignableMessage", "TransactionSignature"): + import ape.types.signatures as sig_module + + return getattr(sig_module, name) + + elif name in ("ContractFunctionPath", "ControlFlow", "GasReport", "SourceTraceback"): + import ape.types.trace as trace_module + + return getattr(trace_module, name) + + elif name in ("CurrencyValue", "CurrencyValueComparable"): + import ape.types.units as units_module + + return getattr(units_module, name) + + elif name in ("BlockID", "ContractCode", "SnapshotID"): + import ape.types.vm as vm_module + + return getattr(vm_module, name) + + elif name in ( + "BaseInterface", + "BaseInterfaceModel", + "BaseModel", + "ExtraAttributesMixin", + "ExtraModelAttributes", + "ManagerAccessMixin", + "get_attribute_with_extras", + "get_item_with_extras", + "only_raise_attribute_error", + ): + import ape.utils.basemodel as basemodel_module + + return getattr(basemodel_module, name) + + else: + raise AttributeError(name) + __all__ = [ "_LazySequence", diff --git a/src/ape/utils/__init__.py b/src/ape/utils/__init__.py index db5f197e05..ed8cc1bd87 100644 --- a/src/ape/utils/__init__.py +++ b/src/ape/utils/__init__.py @@ -1,76 +1,115 @@ -from abc import abstractmethod - -from ape.utils.abi import ( - LogInputABICollection, - Struct, - StructParser, - is_array, - is_dynamic_sized_type, - is_named_tuple, - is_struct, - returns_array, -) -from ape.utils.basemodel import ( - BaseInterface, - BaseInterfaceModel, - ExtraAttributesMixin, - ExtraModelAttributes, - ManagerAccessMixin, - injected_before_use, - only_raise_attribute_error, -) -from ape.utils.misc import ( - DEFAULT_LIVE_NETWORK_BASE_FEE_MULTIPLIER, - DEFAULT_LOCAL_TRANSACTION_ACCEPTANCE_TIMEOUT, - DEFAULT_TRANSACTION_ACCEPTANCE_TIMEOUT, - EMPTY_BYTES32, - LOCAL_NETWORK_NAME, - SOURCE_EXCLUDE_PATTERNS, - ZERO_ADDRESS, - add_padding_to_strings, - as_our_module, - cached_property, - extract_nested_value, - gas_estimation_error_message, - get_current_timestamp_ms, - get_package_version, - is_evm_precompile, - is_zero_hex, - load_config, - log_instead_of_fail, - nonreentrant, - pragma_str_to_specifier_set, - raises_not_implemented, - run_until_complete, - singledispatchmethod, - to_int, -) -from ape.utils.os import ( - clean_path, - create_tempdir, - expand_environment_variables, - extract_archive, - get_all_files_in_directory, - get_full_extension, - get_package_path, - get_relative_path, - in_tempdir, - path_match, - run_in_tempdir, - use_temp_sys_path, -) -from ape.utils.process import JoinableQueue, spawn -from ape.utils.rpc import USER_AGENT, RPCHeaders, allow_disconnected, stream_response -from ape.utils.testing import ( - DEFAULT_NUMBER_OF_TEST_ACCOUNTS, - DEFAULT_TEST_ACCOUNT_BALANCE, - DEFAULT_TEST_CHAIN_ID, - DEFAULT_TEST_HD_PATH, - DEFAULT_TEST_MNEMONIC, - GeneratedDevAccount, - generate_dev_accounts, -) -from ape.utils.trace import USER_ASSERT_TAG, TraceStyles, parse_coverage_tables, parse_gas_table +def __getattr__(name: str): + if name == "abstractmethod": + from abc import abstractmethod + + return abstractmethod + + elif name in ( + "LogInputABICollection", + "Struct", + "StructParser", + "is_array", + "is_dynamic_sized_type", + "is_named_tuple", + "is_struct", + "returns_array", + ): + import ape.utils.abi as abi_module + + return getattr(abi_module, name) + + elif name in ( + "BaseInterface", + "BaseInterfaceModel", + "ExtraAttributesMixin", + "ExtraModelAttributes", + "ManagerAccessMixin", + "injected_before_use", + "only_raise_attribute_error", + ): + import ape.utils.basemodel as basemodel_module + + return getattr(basemodel_module, name) + + elif name in ( + "DEFAULT_LIVE_NETWORK_BASE_FEE_MULTIPLIER", + "DEFAULT_LOCAL_TRANSACTION_ACCEPTANCE_TIMEOUT", + "DEFAULT_TRANSACTION_ACCEPTANCE_TIMEOUT", + "EMPTY_BYTES32", + "LOCAL_NETWORK_NAME", + "SOURCE_EXCLUDE_PATTERNS", + "ZERO_ADDRESS", + "add_padding_to_strings", + "as_our_module", + "cached_property", + "extract_nested_value", + "gas_estimation_error_message", + "get_current_timestamp_ms", + "get_package_version", + "is_evm_precompile", + "is_zero_hex", + "load_config", + "log_instead_of_fail", + "nonreentrant", + "pragma_str_to_specifier_set", + "raises_not_implemented", + "run_until_complete", + "singledispatchmethod", + "to_int", + ): + import ape.utils.misc as misc_module + + return getattr(misc_module, name) + + elif name in ( + "clean_path", + "create_tempdir", + "expand_environment_variables", + "extract_archive", + "get_all_files_in_directory", + "get_full_extension", + "get_package_path", + "get_relative_path", + "in_tempdir", + "path_match", + "run_in_tempdir", + "use_temp_sys_path", + ): + import ape.utils.os as os_module + + return getattr(os_module, name) + + elif name in ("JoinableQueue", "spawn"): + import ape.utils.process as process_module + + return getattr(process_module, name) + + elif name in ("USER_AGENT", "RPCHeaders", "allow_disconnected", "stream_response"): + import ape.utils.rpc as rpc_module + + return getattr(rpc_module, name) + + elif name in ( + "DEFAULT_NUMBER_OF_TEST_ACCOUNTS", + "DEFAULT_TEST_ACCOUNT_BALANCE", + "DEFAULT_TEST_CHAIN_ID", + "DEFAULT_TEST_HD_PATH", + "DEFAULT_TEST_MNEMONIC", + "GeneratedDevAccount", + "generate_dev_accounts", + ): + import ape.utils.testing as testing_module + + return getattr(testing_module, name) + + elif name in ("USER_ASSERT_TAG", "TraceStyles", "parse_coverage_tables", "parse_gas_table"): + import ape.utils.trace as trace_module + + return getattr(trace_module, name) + + else: + raise AttributeError(name) + __all__ = [ "abstractmethod", diff --git a/tests/performance/test_project.py b/tests/performance/test_project.py index 870b9b0071..1060114702 100644 --- a/tests/performance/test_project.py +++ b/tests/performance/test_project.py @@ -1,4 +1,5 @@ def test_get_contract(benchmark, project_with_contracts): + _ = project_with_contracts.Other # Ensure compiled first. benchmark.pedantic( lambda *args, **kwargs: project_with_contracts.get_contract(*args, **kwargs), args=(("Other",),),