Skip to content

Commit

Permalink
refactor: improve error handling for Tuple.set (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
shyba committed Jan 30, 2023
1 parent ed346ad commit f16fa75
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Unreleased

## Added
* Improved error handling for tuple type mismatch: added information on position and expected type. ([#655](https://github.com/algorand/pyteal/pull/655))

## Fixed

Expand Down
13 changes: 7 additions & 6 deletions pyteal/ast/abi/tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,17 +341,18 @@ def set(self, *values):
if len(values) == 1 and isinstance(values[0], ComputedValue):
return self._set_with_computed_type(values[0])

for value in values:
if not isinstance(value, BaseType):
raise TealInputError(f"Expected BaseType, got {value}")

myTypes = self.type_spec().value_type_specs()
if len(myTypes) != len(values):
raise TealInputError(
f"Incorrect length for values. Expected {len(myTypes)}, got {len(values)}"
)
if not all(myTypes[i] == values[i].type_spec() for i in range(len(myTypes))):
raise TealInputError("Input values do not match type")
for index, (value, myType) in enumerate(zip(values, myTypes)):
if not isinstance(value, BaseType):
raise TealInputError(f"Expected BaseType, got {value}")
if myType != value.type_spec():
raise TealInputError(
f"Input values do not match type at {index=}: {value.type_spec()} != {myType}"
)
return self._stored_value.store(_encode_tuple(values))

def encode(self) -> Expr:
Expand Down
2 changes: 1 addition & 1 deletion pyteal/ast/abi/tuple_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ def test_Tuple_set():
with pytest.raises(pt.TealInputError):
tupleValue.set(uint8, uint16, uint32, uint32)

with pytest.raises(pt.TealInputError):
with pytest.raises(pt.TealInputError, match="at index=1: uint32 != uint16"):
tupleValue.set(uint8, uint32, uint16)

with pytest.raises(pt.TealInputError):
Expand Down

0 comments on commit f16fa75

Please sign in to comment.