Skip to content

Commit

Permalink
fix: Issue preventing ContractContainer.source_id from working (#2052)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed May 3, 2024
1 parent df95b4e commit 1b85796
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repos:
- id: isort

- repo: https://github.com/psf/black
rev: 24.4.1
rev: 24.4.2
hooks:
- id: black
name: black
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"hypothesis-jsonschema==0.19.0", # JSON Schema fuzzer extension
],
"lint": [
"black>=24.4.1,<25", # Auto-formatter and linter
"black>=24.4.2,<25", # Auto-formatter and linter
"mypy>=1.10.0,<2", # Static type analyzer
"types-PyYAML", # Needed due to mypy typeshed
"types-requests", # Needed due to mypy typeshed
Expand Down
18 changes: 15 additions & 3 deletions src/ape/utils/basemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,12 @@ def __contains__(self, item) -> bool:

@only_raise_attribute_error
def __getattr__(self, item):
return self._callback(item)
res = self._callback(item)
if res is None:
# attr-lookups cannot return None!
raise AttributeError(item)

return res

def __getitem__(self, item):
return self._callback(item)
Expand All @@ -226,7 +231,14 @@ class ExtraModelAttributes(EthpmTypesBaseModel):
"""

attributes: Union[Any, Callable[[], Any], Callable[[str], Any]]
"""The attributes."""
"""The attributes. The following types are supported:
1. A model or dictionary to lookup attributes.
2. A callable with no arguments, for lazily evaluating a model or dictionary
for lookup.
3. A callable with a single argument that is the attribute name. This style
of lookup cannot be used for optionals.
"""

include_getattr: bool = True
"""Whether to use these in ``__getattr__``."""
Expand Down Expand Up @@ -289,7 +301,7 @@ def _get(self, name: str) -> Optional[Any]:
return attrs.get(name) if hasattr(attrs, "get") else getattr(attrs, name, None)

def _attrs(self) -> Any:
if hasattr(self.attributes, "__contains__"):
if not isinstance(self.attributes, Callable): # type: ignore
# Dict or model that can do a lookup.
return self.attributes

Expand Down
7 changes: 7 additions & 0 deletions tests/functional/test_contract_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,10 @@ def test_decode_input(contract_container, calldata):
def test_declare(contract_container, sender):
receipt = contract_container.declare(sender=sender)
assert not receipt.failed


def test_source_id(contract_container):
actual = contract_container.source_id
expected = contract_container.contract_type.source_id
# Is just a pass-through (via extras-model), but making sure it works.
assert actual == expected

0 comments on commit 1b85796

Please sign in to comment.