Skip to content

Commit

Permalink
refactor: getattr overload move to mixin (#1810)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Jan 4, 2024
1 parent 2c30c3a commit d7643f9
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/ape/api/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from ape.utils import (
DEFAULT_TRANSACTION_ACCEPTANCE_TIMEOUT,
BaseInterfaceModel,
ExtraAttributesMixin,
ExtraModelAttributes,
ManagerAccessMixin,
abstractmethod,
Expand Down Expand Up @@ -66,7 +67,7 @@ class ProxyInfoAPI(BaseModel):
"""The address of the implementation contract."""


class EcosystemAPI(BaseInterfaceModel):
class EcosystemAPI(ExtraAttributesMixin, BaseInterfaceModel):
"""
A set of related networks, such as Ethereum.
"""
Expand Down
3 changes: 2 additions & 1 deletion src/ape/api/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from ape.logging import logger
from ape.utils import (
BaseInterfaceModel,
ExtraAttributesMixin,
ExtraModelAttributes,
abstractmethod,
cached_property,
Expand Down Expand Up @@ -324,7 +325,7 @@ def _create_source_dict(
return source_dict # {source_id: Source}


class DependencyAPI(BaseInterfaceModel):
class DependencyAPI(ExtraAttributesMixin, BaseInterfaceModel):
"""
A base-class for dependency sources, such as GitHub or IPFS.
"""
Expand Down
3 changes: 2 additions & 1 deletion src/ape/api/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
)
from ape.utils import (
BaseInterfaceModel,
ExtraAttributesMixin,
ExtraModelAttributes,
abstractmethod,
cached_property,
Expand Down Expand Up @@ -248,7 +249,7 @@ def _set_description(self):
self._bar.set_description(f"Confirmations ({self._confs}/{self._req_confs})")


class ReceiptAPI(BaseInterfaceModel):
class ReceiptAPI(ExtraAttributesMixin, BaseInterfaceModel):
"""
An abstract class to represent a transaction receipt. The receipt
contains information about the transaction, such as the status
Expand Down
9 changes: 7 additions & 2 deletions src/ape/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@
)
from ape.types.signatures import MessageSignature, SignableMessage, TransactionSignature
from ape.types.trace import CallTreeNode, ControlFlow, GasReport, SourceTraceback, TraceFrame
from ape.utils import BaseInterfaceModel, ExtraModelAttributes, cached_property
from ape.utils import (
BaseInterfaceModel,
ExtraAttributesMixin,
ExtraModelAttributes,
cached_property,
)
from ape.utils.misc import ZERO_ADDRESS, to_int

if TYPE_CHECKING:
Expand Down Expand Up @@ -250,7 +255,7 @@ def __eq__(self, other: Any) -> bool:
return True


class ContractLog(BaseContractLog):
class ContractLog(ExtraAttributesMixin, BaseContractLog):
"""
An instance of a log from a contract.
"""
Expand Down
2 changes: 2 additions & 0 deletions src/ape/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from ape.utils.basemodel import (
BaseInterface,
BaseInterfaceModel,
ExtraAttributesMixin,
ExtraModelAttributes,
ManagerAccessMixin,
injected_before_use,
Expand Down Expand Up @@ -75,6 +76,7 @@
"DEFAULT_TEST_HD_PATH",
"DEFAULT_TRANSACTION_ACCEPTANCE_TIMEOUT",
"EMPTY_BYTES32",
"ExtraAttributesMixin",
"expand_environment_variables",
"extract_nested_value",
"ExtraModelAttributes",
Expand Down
7 changes: 7 additions & 0 deletions src/ape/utils/basemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@ class BaseModel(EthpmTypesBaseModel):

model_config = ConfigDict(arbitrary_types_allowed=True)


class ExtraAttributesMixin:
"""
A mixin to use on models that provide ``ExtraModelAttributes``.
**NOTE**: Must come _before_ your base-model class in subclass tuple to function.
"""

def __ape_extra_attributes__(self) -> Iterator[ExtraModelAttributes]:
"""
Override this method to supply extra attributes
Expand Down
9 changes: 5 additions & 4 deletions src/ape_cache/query.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from pathlib import Path
from typing import Any, Dict, Iterator, List, Optional
from typing import Any, Dict, Iterator, List, Optional, cast

from sqlalchemy import create_engine, func
from sqlalchemy.engine import CursorResult
from sqlalchemy.sql import column, insert, select
from sqlalchemy.sql.expression import Insert, Select

from ape.api import BlockAPI, QueryAPI, QueryType
from ape.api import BlockAPI, QueryAPI, QueryType, TransactionAPI
from ape.api.networks import LOCAL_NETWORK_NAME
from ape.api.query import BaseInterfaceModel, BlockQuery, BlockTransactionQuery, ContractEventQuery
from ape.exceptions import QueryEngineError
Expand Down Expand Up @@ -415,7 +415,8 @@ def _get_block_txns_data(
) -> Optional[List[Dict[str, Any]]]:
new_result = []
table_columns = [c.key for c in Transactions.__table__.columns] # type: ignore
for val in [m for m in result]:
txns: List[TransactionAPI] = cast(List[TransactionAPI], result)
for val in [m for m in txns]:
new_dict = {
k: v
for k, v in val.model_dump(mode="json", by_alias=False).items()
Expand All @@ -432,7 +433,7 @@ def _get_block_txns_data(
new_dict["receiver"] = b""
elif col == "block_hash":
new_dict["block_hash"] = query.block_id
elif col == "signature":
elif col == "signature" and val.signature is not None:
new_dict["signature"] = val.signature.encode_rsv()
elif col not in new_dict:
new_dict[col] = None
Expand Down

0 comments on commit d7643f9

Please sign in to comment.