Skip to content

Commit 3eb4f04

Browse files
committed
refactor: update params to constructor of ReferenceArray and ImmutableArray to be Iterable for consistency
1 parent f3e6736 commit 3eb4f04

33 files changed

+3681
-376
lines changed

pyproject.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ dependencies = [
3232
"coincurve>=19.0.1",
3333
# TODO: uncomment below and remove direct git reference once puya 5.0 is released
3434
# "algorand-python>=3",
35-
"algorand-python@git+https://github.com/algorandfoundation/[email protected].10#subdirectory=stubs",
35+
"algorand-python@git+https://github.com/algorandfoundation/[email protected].11#subdirectory=stubs",
3636
]
3737

3838
[project.urls]
@@ -54,7 +54,7 @@ python = "3.12"
5454
dependencies = [
5555
# TODO: uncomment below and remove direct git reference once puya 5.0 is released
5656
# "puyapy>=5",
57-
"puyapy@git+https://github.com/algorandfoundation/[email protected].10",
57+
"puyapy@git+https://github.com/algorandfoundation/[email protected].11",
5858
"pytest>=7.4",
5959
"pytest-mock>=3.10.0",
6060
"pytest-xdist[psutil]>=3.3",
@@ -138,7 +138,7 @@ dependencies = [
138138
"algokit-utils>=3.0.0",
139139
# TODO: uncomment below and remove direct git reference once puya 5.0 is released
140140
# "puyapy>=5",
141-
"puyapy@git+https://github.com/algorandfoundation/[email protected].10",
141+
"puyapy@git+https://github.com/algorandfoundation/[email protected].11",
142142
]
143143

144144
[tool.hatch.envs.test.scripts]
@@ -191,7 +191,7 @@ post-install-commands = [
191191
dependencies = [
192192
# TODO: uncomment below and remove direct git reference once puya 5.0 is released
193193
# "algorand-python>=3",
194-
"algorand-python@git+https://github.com/algorandfoundation/[email protected].10#subdirectory=stubs",
194+
"algorand-python@git+https://github.com/algorandfoundation/[email protected].11#subdirectory=stubs",
195195
"pytest>=7.4",
196196
"pytest-mock>=3.10.0",
197197
"pytest-xdist[psutil]>=3.3",

src/_algopy_testing/primitives/array.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,22 +281,25 @@ class ImmutableArray(Serializable, typing.Generic[_TArrayItem], metaclass=_Immut
281281
_element_type: typing.ClassVar[type]
282282

283283
# ensure type is fully parameterized by looking up type from metaclass
284-
def __new__(cls, *items: _TArrayItem) -> typing.Self:
284+
285+
def __new__(cls, values: Iterable[_TArrayItem] = ()) -> typing.Self:
285286
from _algopy_testing.serialize import type_of
286287

287288
try:
288289
assert cls._element_type
289290
except AttributeError:
290291
try:
292+
items = list(values)
291293
item = items[0]
292294
except IndexError:
293295
raise TypeError("array must have an item type") from None
294296
cls = cls[type_of(item)]
295297
instance = super().__new__(cls)
296298
return instance
297299

298-
def __init__(self, *items: _TArrayItem):
300+
def __init__(self, values: Iterable[_TArrayItem] = ()):
299301
super().__init__()
302+
items = list(values)
300303
for item in items:
301304
if not isinstance(item, typing.get_origin(self._element_type) or self._element_type):
302305
raise TypeError(f"expected items of type {self._element_type}")
@@ -347,7 +350,7 @@ def _from_iter(self, items: Iterable[_TArrayItem]) -> "ImmutableArray[_TArrayIte
347350
preserved."""
348351
el_type = self._element_type
349352
typ = ImmutableArray[el_type] # type: ignore[valid-type]
350-
return typ(*items)
353+
return typ(items)
351354

352355
def __bool__(self) -> bool:
353356
return bool(self._items)
@@ -361,8 +364,8 @@ def from_bytes(cls, value: bytes, /) -> typing.Self:
361364

362365

363366
class ReferenceArray(Reversible[_TArrayItem]):
364-
def __init__(self, *items: _TArrayItem):
365-
self._items = list(items)
367+
def __init__(self, values: Iterable[_TArrayItem] = ()):
368+
self._items = list(values)
366369

367370
def __iter__(self) -> Iterator[_TArrayItem]:
368371
return iter(list(self._items))
@@ -391,10 +394,10 @@ def pop(self) -> _TArrayItem:
391394
return self._items.pop()
392395

393396
def copy(self) -> "ReferenceArray[_TArrayItem]":
394-
return ReferenceArray(*self._items)
397+
return ReferenceArray(self._items)
395398

396399
def freeze(self) -> ImmutableArray[_TArrayItem]:
397-
return ImmutableArray(*self._items)
400+
return ImmutableArray(self._items)
398401

399402
def __bool__(self) -> bool:
400403
return bool(self._items)
@@ -488,7 +491,7 @@ def copy(self) -> typing.Self:
488491
return self.__class__.from_bytes(self.serialize())
489492

490493
def freeze(self) -> ImmutableArray[_TArrayItem]:
491-
return ImmutableArray(*self._items)
494+
return ImmutableArray(self._items)
492495

493496
def _from_iter(self, items: Iterable[_TArrayItem]) -> "Array[_TArrayItem]":
494497
"""Returns a new array populated with items, also ensures element type info is

src/_algopy_testing/serialize.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ def get_native_to_arc4_serializer( # noqa: PLR0911
8181
),
8282
arc4_to_native=lambda arr: (
8383
typ([element_serializer.arc4_to_native(e) for e in arr])
84-
if issubclass(typ, Array)
85-
else typ(*[element_serializer.arc4_to_native(e) for e in arr])
8684
),
8785
)
8886
if issubclass(typ, FixedArray | ImmutableFixedArray):
Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
#pragma version 10
2+
#pragma typetrack false
3+
4+
// algopy.arc4.ARC4Contract.approval_program() -> uint64:
5+
main:
6+
intcblock 8 3 1 0
7+
bytecblock 0x000000000000000100000000000000020000000000000003
8+
// tests/artifacts/Arrays/immutable.py:479
9+
// class DynamicArrayInitContract(arc4.ARC4Contract):
10+
txn NumAppArgs
11+
bz main___algopy_default_create@11
12+
txn OnCompletion
13+
!
14+
assert // OnCompletion must be NoOp
15+
txn ApplicationID
16+
assert
17+
pushbytess 0x8434ba2a 0x63aabba0 0xdbaf034e 0xa9d14554 // method "test_immutable_array_init()void", method "test_immutable_array_init_without_type_generic()void", method "test_reference_array_init()void", method "test_reference_array_init_without_type_generic()void"
18+
txna ApplicationArgs 0
19+
match test_immutable_array_init test_immutable_array_init_without_type_generic test_reference_array_init test_reference_array_init_without_type_generic
20+
err
21+
22+
main___algopy_default_create@11:
23+
txn OnCompletion
24+
!
25+
txn ApplicationID
26+
!
27+
&&
28+
return // on error: OnCompletion must be NoOp && can only call when creating
29+
30+
31+
// tests.artifacts.Arrays.immutable.DynamicArrayInitContract.test_immutable_array_init[routing]() -> void:
32+
test_immutable_array_init:
33+
// tests/artifacts/Arrays/immutable.py:490
34+
// a3 = ImmutableArray[UInt64](ReferenceArray((UInt64(1), UInt64(2), UInt64(3))))
35+
bytec_0 // 0x000000000000000100000000000000020000000000000003
36+
pop
37+
// tests/artifacts/Arrays/immutable.py:480
38+
// @arc4.abimethod()
39+
intc_2 // 1
40+
return
41+
42+
43+
// tests.artifacts.Arrays.immutable.DynamicArrayInitContract.test_immutable_array_init_without_type_generic[routing]() -> void:
44+
test_immutable_array_init_without_type_generic:
45+
// tests/artifacts/Arrays/immutable.py:512
46+
// a3 = ImmutableArray(ReferenceArray((UInt64(1), UInt64(2), UInt64(3))))
47+
bytec_0 // 0x000000000000000100000000000000020000000000000003
48+
pop
49+
// tests/artifacts/Arrays/immutable.py:502
50+
// @arc4.abimethod()
51+
intc_2 // 1
52+
return
53+
54+
55+
// tests.artifacts.Arrays.immutable.DynamicArrayInitContract.test_reference_array_init[routing]() -> void:
56+
test_reference_array_init:
57+
// tests/artifacts/Arrays/immutable.py:526
58+
// a1 = ReferenceArray[UInt64]((UInt64(1), UInt64(2), UInt64(3)))
59+
bytec_0 // 0x000000000000000100000000000000020000000000000003
60+
// tests/artifacts/Arrays/immutable.py:528
61+
// a2 = ReferenceArray[UInt64](FixedArray((UInt64(1), UInt64(2), UInt64(3))))
62+
dup
63+
pop
64+
dupn 2
65+
// tests/artifacts/Arrays/immutable.py:529
66+
// assert a1.length == a2.length
67+
len
68+
intc_0 // 8
69+
/
70+
intc_1 // 3
71+
==
72+
assert
73+
// tests/artifacts/Arrays/immutable.py:530
74+
// assert a1[0] == a2[0]
75+
dup
76+
intc_3 // 0
77+
extract_uint64
78+
intc_2 // 1
79+
==
80+
assert
81+
// tests/artifacts/Arrays/immutable.py:531
82+
// assert a1[1] == a2[1]
83+
dup
84+
intc_0 // 8
85+
extract_uint64
86+
pushint 2 // 2
87+
==
88+
assert
89+
// tests/artifacts/Arrays/immutable.py:532
90+
// assert a1[2] == a2[2]
91+
pushint 16 // 16
92+
extract_uint64
93+
intc_1 // 3
94+
==
95+
assert
96+
// tests/artifacts/Arrays/immutable.py:534
97+
// a3 = ReferenceArray[UInt64](ImmutableArray((UInt64(1), UInt64(2), UInt64(3))))
98+
bytec_0 // 0x000000000000000100000000000000020000000000000003
99+
pop
100+
dupn 2
101+
// tests/artifacts/Arrays/immutable.py:536
102+
// assert a1[0] == a3[0]
103+
intc_3 // 0
104+
extract_uint64
105+
intc_2 // 1
106+
==
107+
assert
108+
// tests/artifacts/Arrays/immutable.py:537
109+
// assert a1[1] == a3[1]
110+
dup
111+
intc_0 // 8
112+
extract_uint64
113+
pushint 2 // 2
114+
==
115+
assert
116+
// tests/artifacts/Arrays/immutable.py:538
117+
// assert a1[2] == a3[2]
118+
pushint 16 // 16
119+
extract_uint64
120+
intc_1 // 3
121+
==
122+
assert
123+
// tests/artifacts/Arrays/immutable.py:526
124+
// a1 = ReferenceArray[UInt64]((UInt64(1), UInt64(2), UInt64(3)))
125+
bytec_0 // 0x000000000000000100000000000000020000000000000003
126+
pop
127+
bytec_0 // 0x000000000000000100000000000000020000000000000003
128+
pop
129+
// tests/artifacts/Arrays/immutable.py:541
130+
// assert a1.length == a4.length
131+
dup
132+
len
133+
intc_0 // 8
134+
/
135+
intc_1 // 3
136+
==
137+
assert
138+
// tests/artifacts/Arrays/immutable.py:542
139+
// assert a1[0] == a4[0]
140+
dup
141+
intc_3 // 0
142+
extract_uint64
143+
intc_2 // 1
144+
==
145+
assert
146+
// tests/artifacts/Arrays/immutable.py:543
147+
// assert a1[1] == a4[1]
148+
dup
149+
intc_0 // 8
150+
extract_uint64
151+
pushint 2 // 2
152+
==
153+
assert
154+
// tests/artifacts/Arrays/immutable.py:544
155+
// assert a1[2] == a4[2]
156+
pushint 16 // 16
157+
extract_uint64
158+
intc_1 // 3
159+
==
160+
// tests/artifacts/Arrays/immutable.py:524
161+
// @arc4.abimethod()
162+
return
163+
164+
165+
// tests.artifacts.Arrays.immutable.DynamicArrayInitContract.test_reference_array_init_without_type_generic[routing]() -> void:
166+
test_reference_array_init_without_type_generic:
167+
// tests/artifacts/Arrays/immutable.py:548
168+
// a1 = ReferenceArray((UInt64(1), UInt64(2), UInt64(3)))
169+
bytec_0 // 0x000000000000000100000000000000020000000000000003
170+
// tests/artifacts/Arrays/immutable.py:550
171+
// a2 = ReferenceArray(FixedArray((UInt64(1), UInt64(2), UInt64(3))))
172+
dup
173+
pop
174+
dupn 2
175+
// tests/artifacts/Arrays/immutable.py:551
176+
// assert a1.length == a2.length
177+
len
178+
intc_0 // 8
179+
/
180+
intc_1 // 3
181+
==
182+
assert
183+
// tests/artifacts/Arrays/immutable.py:552
184+
// assert a1[0] == a2[0]
185+
dup
186+
intc_3 // 0
187+
extract_uint64
188+
intc_2 // 1
189+
==
190+
assert
191+
// tests/artifacts/Arrays/immutable.py:553
192+
// assert a1[1] == a2[1]
193+
dup
194+
intc_0 // 8
195+
extract_uint64
196+
pushint 2 // 2
197+
==
198+
assert
199+
// tests/artifacts/Arrays/immutable.py:554
200+
// assert a1[2] == a2[2]
201+
pushint 16 // 16
202+
extract_uint64
203+
intc_1 // 3
204+
==
205+
assert
206+
// tests/artifacts/Arrays/immutable.py:556
207+
// a3 = ReferenceArray(ImmutableArray((UInt64(1), UInt64(2), UInt64(3))))
208+
bytec_0 // 0x000000000000000100000000000000020000000000000003
209+
pop
210+
dupn 2
211+
// tests/artifacts/Arrays/immutable.py:558
212+
// assert a1[0] == a3[0]
213+
intc_3 // 0
214+
extract_uint64
215+
intc_2 // 1
216+
==
217+
assert
218+
// tests/artifacts/Arrays/immutable.py:559
219+
// assert a1[1] == a3[1]
220+
dup
221+
intc_0 // 8
222+
extract_uint64
223+
pushint 2 // 2
224+
==
225+
assert
226+
// tests/artifacts/Arrays/immutable.py:560
227+
// assert a1[2] == a3[2]
228+
pushint 16 // 16
229+
extract_uint64
230+
intc_1 // 3
231+
==
232+
assert
233+
// tests/artifacts/Arrays/immutable.py:548
234+
// a1 = ReferenceArray((UInt64(1), UInt64(2), UInt64(3)))
235+
bytec_0 // 0x000000000000000100000000000000020000000000000003
236+
pop
237+
bytec_0 // 0x000000000000000100000000000000020000000000000003
238+
pop
239+
// tests/artifacts/Arrays/immutable.py:563
240+
// assert a1.length == a4.length
241+
dup
242+
len
243+
intc_0 // 8
244+
/
245+
intc_1 // 3
246+
==
247+
assert
248+
// tests/artifacts/Arrays/immutable.py:564
249+
// assert a1[0] == a4[0]
250+
dup
251+
intc_3 // 0
252+
extract_uint64
253+
intc_2 // 1
254+
==
255+
assert
256+
// tests/artifacts/Arrays/immutable.py:565
257+
// assert a1[1] == a4[1]
258+
dup
259+
intc_0 // 8
260+
extract_uint64
261+
pushint 2 // 2
262+
==
263+
assert
264+
// tests/artifacts/Arrays/immutable.py:566
265+
// assert a1[2] == a4[2]
266+
pushint 16 // 16
267+
extract_uint64
268+
intc_1 // 3
269+
==
270+
// tests/artifacts/Arrays/immutable.py:546
271+
// @arc4.abimethod()
272+
return

0 commit comments

Comments
 (0)