Skip to content

Commit

Permalink
refactor: make ARC4 struct inherit _ABIEncoded
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-makerx committed Aug 21, 2024
1 parent a4e96ad commit 19d5f99
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
13 changes: 6 additions & 7 deletions src/algopy_testing/arc4.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def _new_parameterized_class(cls: type, type_params: Sequence[type], type_info:

def _check_is_arc4(items: Sequence[typing.Any]) -> Sequence[_ABIEncoded]:
for item in items:
if not isinstance(item, _ABIEncoded | Struct):
if not isinstance(item, _ABIEncoded):
raise TypeError("expected ARC4 type")
return items

Expand Down Expand Up @@ -1090,10 +1090,10 @@ def _tuple_type_from_struct(struct: type[Struct]) -> type[Tuple]: # type: ignor
return _parameterize_type(Tuple, *field_types)


class Struct(MutableBytes, metaclass=_StructMeta):
class Struct(MutableBytes, _ABIEncoded, metaclass=_StructMeta): # type: ignore[misc]
"""Base class for ARC4 Struct types."""

_type_info: typing.ClassVar[_StructTypeInfo]
_type_info: typing.ClassVar[_StructTypeInfo] # type: ignore[misc]

def __init_subclass__(cls) -> None:
dataclasses.dataclass(cls)
Expand Down Expand Up @@ -1200,9 +1200,8 @@ def emit(event: str | Struct, /, *args: object) -> None:
def native_value_to_arc4(value: object) -> _ABIEncoded: # noqa: PLR0911
import algopy

if isinstance(value, _ABIEncoded | Struct):
# Struct still matches the _ABIEncoded protocol
return value # type: ignore[return-value]
if isinstance(value, _ABIEncoded):
return value
if isinstance(value, bool):
return Bool(value)
if isinstance(value, algopy.UInt64):
Expand Down Expand Up @@ -1341,7 +1340,7 @@ def _encode( # noqa: PLR0912
)
while i < values_length:
value = values[i]
assert isinstance(value, _ABIEncoded | Struct), "expected ARC4 value"
assert isinstance(value, _ABIEncoded), "expected ARC4 value"
is_dynamic_index.append(value._type_info.is_dynamic)
if is_dynamic_index[-1]:
heads.append(b"\x00\x00")
Expand Down
4 changes: 2 additions & 2 deletions src/algopy_testing/decorators/arc4.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def _generate_arc4_signature_from_fn(fn: typing.Callable[_P, _R], arc4_name: str


def _type_to_arc4(annotation: types.GenericAlias | type | None) -> str: # noqa: PLR0911, PLR0912
from algopy_testing.arc4 import Struct, _ABIEncoded
from algopy_testing.arc4 import _ABIEncoded
from algopy_testing.gtxn import Transaction, TransactionBase
from algopy_testing.models import Account, Application, Asset

Expand All @@ -308,7 +308,7 @@ def _type_to_arc4(annotation: types.GenericAlias | type | None) -> str: # noqa:
raise TypeError(f"expected type: {annotation!r}")

# arc4 types
if issubclass(annotation, _ABIEncoded | Struct):
if issubclass(annotation, _ABIEncoded):
return annotation._type_info.arc4_name
# txn types
if issubclass(annotation, Transaction):
Expand Down

0 comments on commit 19d5f99

Please sign in to comment.