Skip to content

Commit

Permalink
feat: add get method to ABIList (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Apr 26, 2024
1 parent 3945491 commit ee92374
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.6.0
hooks:
- id: check-yaml

- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.10.1
- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort

- repo: https://github.com/psf/black
rev: 24.2.0
rev: 24.4.2
hooks:
- id: black
name: black
Expand All @@ -21,7 +21,7 @@ repos:
- id: flake8

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.9.0
rev: v1.10.0
hooks:
- id: mypy
additional_dependencies: [types-requests, types-setuptools, pydantic]
Expand Down
17 changes: 10 additions & 7 deletions ethpm_types/contract_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,15 @@ def __getitem__(self, selector):
raise NotImplementedError(f"Cannot use {type(selector)} as a selector.")

@__getitem__.register
def __getitem_int(self, selector: int):
def __getitem_int(self, selector: int) -> ABILIST_T:
return super().__getitem__(selector)

@__getitem__.register
def __getitem_slice(self, selector: slice):
def __getitem_slice(self, selector: slice) -> List[ABILIST_T]:
return super().__getitem__(selector)

@__getitem__.register
def __getitem_str(self, selector: str):
def __getitem_str(self, selector: str) -> ABILIST_T:
try:
if "(" in selector:
# String-style selector e.g. `method(arg0)`.
Expand All @@ -186,7 +186,7 @@ def __getitem_str(self, selector: str):
raise KeyError(selector)

@__getitem__.register
def __getitem_bytes(self, selector: bytes):
def __getitem_bytes(self, selector: bytes) -> ABILIST_T:
try:
if self._selector_hash_fn:
return next(
Expand All @@ -203,15 +203,15 @@ def __getitem_bytes(self, selector: bytes):
raise KeyError(selector)

@__getitem__.register
def __getitem_method_abi(self, selector: MethodABI):
def __getitem_method_abi(self, selector: MethodABI) -> ABILIST_T:
return self.__getitem__(selector.selector)

@__getitem__.register
def __getitem_event_abi(self, selector: EventABI):
def __getitem_event_abi(self, selector: EventABI) -> ABILIST_T:
return self.__getitem__(selector.selector)

@singledispatchmethod
def __contains__(self, selector):
def __contains__(self, selector) -> bool: # type: ignore[override]
raise NotImplementedError(f"Cannot use {type(selector)} as a selector.")

@__contains__.register
Expand All @@ -230,6 +230,9 @@ def __contains_method_abi(self, selector: MethodABI) -> bool:
def __contains_event_abi(self, selector: EventABI) -> bool:
return self._contains(selector)

def get(self, item, default: Optional[ABILIST_T] = None) -> Optional[ABILIST_T]:
return self[item] if item in self else default

def _contains(self, selector: Union[str, bytes, MethodABI, EventABI]) -> bool:
try:
_ = self[selector]
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
"hypothesis-jsonschema==0.19.0", # Fuzzes based on a json schema
],
"lint": [
"black>=24.2.0,<25", # Auto-formatter and linter
"mypy>=1.9.0,<2", # Static type analyzer
"black>=24.4.2,<25", # Auto-formatter and linter
"mypy>=1.10.0,<2", # Static type analyzer
"types-setuptools", # Needed for mypy type shed
"types-requests", # Needed for mypy type shed
"flake8>=7.0.0,<8", # Style linter
"flake8-breakpoint>=1.1.0,<2", # Detect breakpoints left in code
"flake8-print>=5.0.0,<6", # Detect print statements left in code
"isort>=5.10.1,<6", # Import sorting linter
"isort>=5.13.2,<6", # Import sorting linter
"mdformat>=0.7.17", # Auto-formatter for markdown
"mdformat-gfm>=0.3.5", # Needed for formatting GitHub-flavored markdown
"mdformat-frontmatter>=0.4.1", # Needed for frontmatters-style headers in issue templates
Expand Down
11 changes: 11 additions & 0 deletions tests/test_abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
ReceiveABI,
UnprocessedABI,
)
from ethpm_types.contract_type import ABIList


class TestABIType:
Expand Down Expand Up @@ -189,3 +190,13 @@ def test_schema(self):
actual = UnprocessedABI.model_json_schema()
assert not hasattr(actual, "$defs")
assert UnprocessedABI.__name__ in actual["title"]


class TestABIList:
def test_get(self):
signature = "transfer(address to, uint256 value)"
method_abi = MethodABI.from_signature(signature)
abi_ls = ABIList((method_abi,))
# Show the .get() method works.
actual = abi_ls.get("transfer")
assert actual.signature == signature

0 comments on commit ee92374

Please sign in to comment.