Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename pyteal/ast/abi/tuple.py functions to follow PEP 8 conventions #461

Merged
merged 2 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pyteal/ast/abi/array_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pyteal.ast.naryexpr import Concat

from pyteal.ast.abi.type import TypeSpec, BaseType, ComputedValue
from pyteal.ast.abi.tuple import encodeTuple
from pyteal.ast.abi.tuple import _encode_tuple
from pyteal.ast.abi.bool import Bool, BoolTypeSpec
from pyteal.ast.abi.uint import Uint16, Uint16TypeSpec
from pyteal.ast.abi.util import substring_for_decoding
Expand Down Expand Up @@ -135,7 +135,7 @@ def set(self, values: Sequence[T]) -> Expr:
)
)

encoded = encodeTuple(values)
encoded = _encode_tuple(values)

if self.type_spec().is_length_dynamic():
length_tmp = Uint16()
Expand Down
4 changes: 2 additions & 2 deletions pyteal/ast/abi/array_dynamic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pyteal as pt
from pyteal import abi
from pyteal.ast.abi.util import substring_for_decoding
from pyteal.ast.abi.tuple import encodeTuple
from pyteal.ast.abi.tuple import _encode_tuple
from pyteal.ast.abi.array_base_test import STATIC_TYPES, DYNAMIC_TYPES
from pyteal.ast.abi.type_test import ContainerType

Expand Down Expand Up @@ -121,7 +121,7 @@ def test_DynamicArray_set_values():
expectedExpr = value.stored_value.store(
pt.Concat(
pt.Seq(length_tmp.set(len(values)), length_tmp.encode()),
encodeTuple(values),
_encode_tuple(values),
)
)
expected, _ = expectedExpr.__teal__(options)
Expand Down
4 changes: 2 additions & 2 deletions pyteal/ast/abi/array_static_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pyteal as pt
from pyteal import abi
from pyteal.ast.abi.util import substring_for_decoding
from pyteal.ast.abi.tuple import encodeTuple
from pyteal.ast.abi.tuple import _encode_tuple
from pyteal.ast.abi.bool import _bool_sequence_length
from pyteal.ast.abi.type_test import ContainerType
from pyteal.ast.abi.array_base_test import STATIC_TYPES, DYNAMIC_TYPES
Expand Down Expand Up @@ -167,7 +167,7 @@ def test_StaticArray_set_values():
assert expr.type_of() == pt.TealType.none
assert not expr.has_return()

expectedExpr = value.stored_value.store(encodeTuple(values))
expectedExpr = value.stored_value.store(_encode_tuple(values))
expected, _ = expectedExpr.__teal__(options)
expected.addIncoming()
expected = pt.TealBlock.NormalizeBlocks(expected)
Expand Down
24 changes: 12 additions & 12 deletions pyteal/ast/abi/tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from pyteal.ast.abi.util import substring_for_decoding


def encodeTuple(values: Sequence[BaseType]) -> Expr:
def _encode_tuple(values: Sequence[BaseType]) -> Expr:
heads: List[Expr] = []
head_length_static: int = 0

Expand Down Expand Up @@ -112,24 +112,24 @@ def encodeTuple(values: Sequence[BaseType]) -> Expr:
return Concat(*toConcat)


def indexTuple(
valueTypes: Sequence[TypeSpec], encoded: Expr, index: int, output: BaseType
def _index_tuple(
value_types: Sequence[TypeSpec], encoded: Expr, index: int, output: BaseType
) -> Expr:
if not (0 <= index < len(valueTypes)):
if not (0 <= index < len(value_types)):
raise ValueError("Index outside of range")

offset = 0
ignoreNext = 0
lastBoolStart = 0
lastBoolLength = 0
for i, typeBefore in enumerate(valueTypes[:index]):
for i, typeBefore in enumerate(value_types[:index]):
if ignoreNext > 0:
ignoreNext -= 1
continue

if typeBefore == BoolTypeSpec():
lastBoolStart = offset
lastBoolLength = _consecutive_bool_type_spec_num(valueTypes, i)
lastBoolLength = _consecutive_bool_type_spec_num(value_types, i)
offset += _bool_sequence_length(lastBoolLength)
ignoreNext = lastBoolLength - 1
continue
Expand All @@ -140,7 +140,7 @@ def indexTuple(

offset += typeBefore.byte_length_static()

valueType = valueTypes[index]
valueType = value_types[index]
if output.type_spec() != valueType:
raise TypeError("Output type does not match value type")

Expand All @@ -158,13 +158,13 @@ def indexTuple(
hasNextDynamicValue = False
nextDynamicValueOffset = offset + 2
ignoreNext = 0
for i, typeAfter in enumerate(valueTypes[index + 1 :], start=index + 1):
for i, typeAfter in enumerate(value_types[index + 1 :], start=index + 1):
if ignoreNext > 0:
ignoreNext -= 1
continue

if type(typeAfter) is BoolTypeSpec:
boolLength = _consecutive_bool_type_spec_num(valueTypes, i)
boolLength = _consecutive_bool_type_spec_num(value_types, i)
nextDynamicValueOffset += _bool_sequence_length(boolLength)
ignoreNext = boolLength - 1
continue
Expand All @@ -189,7 +189,7 @@ def indexTuple(
start_index = Int(offset)
length = Int(valueType.byte_length_static())

if index + 1 == len(valueTypes):
if index + 1 == len(value_types):
if offset == 0:
# This is the first and only value in the tuple, so decode all of encoded
return output.decode(encoded)
Expand Down Expand Up @@ -333,7 +333,7 @@ def set(self, *values):
)
if not all(myTypes[i] == values[i].type_spec() for i in range(len(myTypes))):
raise TealInputError("Input values do not match type")
return self.stored_value.store(encodeTuple(values))
return self.stored_value.store(_encode_tuple(values))

def encode(self) -> Expr:
return self.stored_value.load()
Expand Down Expand Up @@ -379,7 +379,7 @@ def produced_type_spec(self) -> TypeSpec:
return self.tuple.type_spec().value_type_specs()[self.index]

def store_into(self, output: T) -> Expr:
return indexTuple(
return _index_tuple(
self.tuple.type_spec().value_type_specs(),
self.tuple.encode(),
self.index,
Expand Down
16 changes: 8 additions & 8 deletions pyteal/ast/abi/tuple_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pyteal as pt
from pyteal import abi
from pyteal.ast.abi.tuple import encodeTuple, indexTuple, TupleElement
from pyteal.ast.abi.tuple import _encode_tuple, _index_tuple, TupleElement
from pyteal.ast.abi.bool import _encode_bool_sequence
from pyteal.ast.abi.util import substring_for_decoding
from pyteal.ast.abi.type_test import ContainerType
Expand Down Expand Up @@ -196,7 +196,7 @@ class EncodeTest(NamedTuple):
]

for i, test in enumerate(tests):
expr = encodeTuple(test.types)
expr = _encode_tuple(test.types)
assert expr.type_of() == pt.TealType.bytes
assert not expr.has_return()

Expand Down Expand Up @@ -430,7 +430,7 @@ class IndexTest(NamedTuple):

for i, test in enumerate(tests):
output = test.types[test.typeIndex].new_instance()
expr = indexTuple(test.types, encoded, test.typeIndex, output)
expr = _index_tuple(test.types, encoded, test.typeIndex, output)
assert expr.type_of() == pt.TealType.none
assert not expr.has_return()

Expand All @@ -446,17 +446,17 @@ class IndexTest(NamedTuple):
assert actual == expected, "Test at index {} failed".format(i)

with pytest.raises(ValueError):
indexTuple(test.types, encoded, len(test.types), output)
_index_tuple(test.types, encoded, len(test.types), output)

with pytest.raises(ValueError):
indexTuple(test.types, encoded, -1, output)
_index_tuple(test.types, encoded, -1, output)

otherType = abi.Uint64()
if output.type_spec() == otherType.type_spec():
otherType = abi.Uint16()

with pytest.raises(TypeError):
indexTuple(test.types, encoded, test.typeIndex, otherType)
_index_tuple(test.types, encoded, test.typeIndex, otherType)


def test_TupleTypeSpec_eq():
Expand Down Expand Up @@ -672,7 +672,7 @@ def test_Tuple_set():
assert expr.type_of() == pt.TealType.none
assert not expr.has_return()

expectedExpr = tupleValue.stored_value.store(encodeTuple([uint8, uint16, uint32]))
expectedExpr = tupleValue.stored_value.store(_encode_tuple([uint8, uint16, uint32]))
expected, _ = expectedExpr.__teal__(options)
expected.addIncoming()
expected = pt.TealBlock.NormalizeBlocks(expected)
Expand Down Expand Up @@ -813,7 +813,7 @@ def test_TupleElement_store_into():
assert expr.type_of() == pt.TealType.none
assert not expr.has_return()

expectedExpr = indexTuple(test, tupleValue.encode(), j, output)
expectedExpr = _index_tuple(test, tupleValue.encode(), j, output)
expected, _ = expectedExpr.__teal__(options)
expected.addIncoming()
expected = pt.TealBlock.NormalizeBlocks(expected)
Expand Down