Skip to content

Commit

Permalink
fix: treat ARC4 Tuples with mutable elements as mutable overall
Browse files Browse the repository at this point in the history
  • Loading branch information
achidlow committed Mar 26, 2024
1 parent d52d4a6 commit 7f7a4b6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/puya/awst/wtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,18 @@ def from_types(cls, types: Iterable[WType]) -> typing.Self:
types = tuple(types)
if not types:
raise ValueError("arc4.Tuple needs types")
immutable = True
for typ in types:
if not is_arc4_encoded_type(typ):
raise ValueError(f"Invalid type for arc4.Tuple: {typ}")
# this seems counterintuitive, but is necessary.
# despite the overall collection remaining stable, since ARC4 types
# are encoded as a single value, if items within the tuple can be mutated,
# then the overall value is also mutable
immutable = immutable and typ.immutable
name = f"arc4.tuple<{','.join([t.name for t in types])}>"
python_name = f"{constants.CLS_ARC4_TUPLE}[{', '.join(map(str, types))}]"
return cls(name=name, stub_name=python_name, types=types)
return cls(name=name, stub_name=python_name, types=types, immutable=immutable)


@typing.final
Expand Down
10 changes: 10 additions & 0 deletions tests/test_expected_output/arc4.test
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,13 @@ def testC(x: C) -> None: ## E: UFixedNxM scale must be >=8 and <=64 bits, and be
@subroutine
def testD(x: D) -> None: ## E: BigUFixedNxM scale must be >64 and <=512 bits, and be a multiple of 8
assert x


## case: test_arc4_tuple_immutable
from algopy import arc4

class NotOkay(arc4.ARC4Contract):
@arc4.abimethod
def not_okay(self) -> None:
t = arc4.Tuple((arc4.UInt64(1), arc4.UInt8(1)))
t[0] = arc4.UInt64(2) # type: ignore ## E: expression is not valid as assignment target (algopy.arc4.Tuple[algopy.arc4.UInt64, algopy.arc4.UInt8] is immutable)

0 comments on commit 7f7a4b6

Please sign in to comment.