Skip to content

Commit 24bcf9d

Browse files
authored
fix: ability to pass kw_only flag to dataclass when defining struct subclass (#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
1 parent e036d9e commit 24bcf9d

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

.github/dependabot.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "pip"
4+
directory: "/"
5+
schedule:
6+
interval: "weekly"
7+
commit-message:
8+
prefix: "chore(deps)"
9+
groups:
10+
all:
11+
patterns:
12+
- "*"
13+
update-types:
14+
- "minor"
15+
- "patch"

src/_algopy_testing/arc4.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,8 +1053,8 @@ class Struct(MutableBytes, _ABIEncoded, metaclass=_StructMeta): # type: ignore[
10531053

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

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

10601060
def __post_init__(self) -> None:

tests/arc4/test_struct.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
_arc4_bool = arc4.Bool(True)
1717

1818

19+
class StructWithKwOnly(arc4.Struct, kw_only=True):
20+
a: arc4.UInt64
21+
b: arc4.UInt64
22+
c: arc4.Bool
23+
d: arc4.String
24+
25+
1926
class Swapped(arc4.Struct):
2027
b: arc4.UInt64
2128
c: arc4.Bool
@@ -329,6 +336,19 @@ def test_from_bytes(abi_type: abi.ABIType, abi_value: tuple, arc4_type: type[Swa
329336
assert len(abi_value) == len(arc4_result)
330337

331338

339+
def test_struct_kw_only() -> None:
340+
struct = StructWithKwOnly(
341+
a=arc4.UInt64(1), b=arc4.UInt64(2), c=arc4.Bool(True), d=arc4.String("hello")
342+
)
343+
assert struct.a == 1
344+
assert struct.b == 2
345+
assert struct.c.native
346+
assert struct.d == "hello"
347+
348+
with pytest.raises(TypeError, match="takes 1 positional argument but 5 were given"):
349+
StructWithKwOnly(arc4.UInt64(1), arc4.UInt64(2), arc4.Bool(True), arc4.String("hello")) # type: ignore[misc]
350+
351+
332352
def _compare_abi_and_arc4_values(
333353
arc4_value: typing.Any,
334354
abi_value: typing.Any,

0 commit comments

Comments
 (0)