Skip to content

Commit

Permalink
fix: ability to pass kw_only flag to dataclass when defining struct s…
Browse files Browse the repository at this point in the history
…ubclass (#23)

* fix: ability to pass kw_only flag to dataclass when defining struct subclass

* chore: add dependabot yaml

* chore: adding ability to pass args to struct init subclass
  • Loading branch information
aorumbayev authored Sep 3, 2024
1 parent e036d9e commit 24bcf9d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
15 changes: 15 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: 2
updates:
- package-ecosystem: "pip"
directory: "/"
schedule:
interval: "weekly"
commit-message:
prefix: "chore(deps)"
groups:
all:
patterns:
- "*"
update-types:
- "minor"
- "patch"
4 changes: 2 additions & 2 deletions src/_algopy_testing/arc4.py
Original file line number Diff line number Diff line change
Expand Up @@ -1053,8 +1053,8 @@ class Struct(MutableBytes, _ABIEncoded, metaclass=_StructMeta): # type: ignore[

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

def __init_subclass__(cls) -> None:
dataclasses.dataclass(cls)
def __init_subclass__(cls, *args: typing.Any, **kwargs: dict[str, typing.Any]) -> None:
dataclasses.dataclass(cls, *args, **kwargs)
cls._type_info = _StructTypeInfo(cls)

def __post_init__(self) -> None:
Expand Down
20 changes: 20 additions & 0 deletions tests/arc4/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
_arc4_bool = arc4.Bool(True)


class StructWithKwOnly(arc4.Struct, kw_only=True):
a: arc4.UInt64
b: arc4.UInt64
c: arc4.Bool
d: arc4.String


class Swapped(arc4.Struct):
b: arc4.UInt64
c: arc4.Bool
Expand Down Expand Up @@ -329,6 +336,19 @@ def test_from_bytes(abi_type: abi.ABIType, abi_value: tuple, arc4_type: type[Swa
assert len(abi_value) == len(arc4_result)


def test_struct_kw_only() -> None:
struct = StructWithKwOnly(
a=arc4.UInt64(1), b=arc4.UInt64(2), c=arc4.Bool(True), d=arc4.String("hello")
)
assert struct.a == 1
assert struct.b == 2
assert struct.c.native
assert struct.d == "hello"

with pytest.raises(TypeError, match="takes 1 positional argument but 5 were given"):
StructWithKwOnly(arc4.UInt64(1), arc4.UInt64(2), arc4.Bool(True), arc4.String("hello")) # type: ignore[misc]


def _compare_abi_and_arc4_values(
arc4_value: typing.Any,
abi_value: typing.Any,
Expand Down

0 comments on commit 24bcf9d

Please # to comment.