@@ -140,7 +140,7 @@ class _ABIEncoded(BytesBacked):
140140 def from_bytes (cls , value : algopy .Bytes | bytes , / ) -> typing .Self :
141141 """Construct an instance from the underlying bytes (no validation)"""
142142 instance = cls ()
143- instance ._value = as_bytes (value )
143+ instance ._value = value . value if isinstance (value , Bytes ) else value
144144 return instance
145145
146146 @classmethod
@@ -556,11 +556,11 @@ class Bool(_ABIEncoded):
556556 _value : bytes
557557
558558 # True value is encoded as having a 1 on the most significant bit (0x80 = 128)
559- _true_int_value = 128
560- _false_int_value = 0
559+ _true_byte_value = int_to_bytes ( 128 , 1 )
560+ _false_byte_value = int_to_bytes ( 0 , 1 )
561561
562562 def __init__ (self , value : bool = False , / ) -> None : # noqa: FBT001, FBT002
563- self ._value = int_to_bytes ( self ._true_int_value if value else self ._false_int_value , 1 )
563+ self ._value = self ._true_byte_value if value else self ._false_byte_value
564564
565565 def __eq__ (self , other : object ) -> bool :
566566 try :
@@ -576,8 +576,7 @@ def __bool__(self) -> bool:
576576 @property
577577 def native (self ) -> bool :
578578 """Return the bool representation of the value after ARC4 decoding."""
579- int_value = int .from_bytes (self ._value )
580- return int_value == self ._true_int_value
579+ return self ._value == self ._true_byte_value
581580
582581 def __str__ (self ) -> str :
583582 return f"{ self .native } "
@@ -669,7 +668,8 @@ def __init__(self, *_items: _TArrayItem):
669668 f"item must be of type { self ._type_info .item_type !r} , not { item ._type_info !r} "
670669 )
671670
672- self ._value = _encode (items )
671+ item_list = list (items )
672+ self ._value = _encode (item_list )
673673
674674 def __iter__ (self ) -> Iterator [_TArrayItem ]:
675675 # """Returns an iterator for the items in the array"""
@@ -854,7 +854,8 @@ def __init__(self, *_items: _TArrayItem):
854854 raise TypeError (
855855 f"item must be of type { self ._type_info .item_type !r} , not { item ._type_info !r} "
856856 )
857- self ._value = self ._encode_with_length (items )
857+ item_list = list (items )
858+ self ._value = self ._encode_with_length (item_list )
858859
859860 def __iter__ (self ) -> typing .Iterator [_TArrayItem ]:
860861 """Returns an iterator for the items in the array."""
@@ -1294,6 +1295,9 @@ def _find_bool(
12941295 is_looking_forward = delta > 0
12951296 is_looking_backward = delta < 0
12961297 values_length = len (values ) if isinstance (values , tuple | list ) else values .length .value
1298+ if isinstance (values , (StaticArray | DynamicArray | list )):
1299+ return 0 if is_looking_backward else values_length - index - 1
1300+
12971301 while True :
12981302 curr = index + delta * until
12991303 is_curr_at_end = curr == values_length - 1
@@ -1311,12 +1315,16 @@ def _find_bool(
13111315 return until
13121316
13131317
1314- def _find_bool_types (values : typing .Sequence [_TypeInfo ], index : int , delta : int ) -> int :
1318+ def _find_bool_types (
1319+ values : typing .Sequence [_TypeInfo ], index : int , delta : int , * , is_homogeneous : bool = False
1320+ ) -> int :
13151321 """Helper function to find consecutive booleans from current index in a tuple."""
13161322 until = 0
13171323 is_looking_forward = delta > 0
13181324 is_looking_backward = delta < 0
13191325 values_length = len (values )
1326+ if is_homogeneous :
1327+ return 0 if is_looking_backward else values_length - index - 1
13201328 while True :
13211329 curr = index + delta * until
13221330 is_curr_at_end = curr == values_length - 1
@@ -1438,6 +1446,7 @@ def _encode( # noqa: PLR0912
14381446 raise ValueError (
14391447 "expected before index should have number of bool mod 8 equal 0"
14401448 )
1449+
14411450 after = min (7 , after )
14421451 consecutive_bool_list = [values [i ] for i in range (i , i + after + 1 )]
14431452 compressed_int = _compress_multiple_bool (consecutive_bool_list )
@@ -1467,14 +1476,16 @@ def _encode( # noqa: PLR0912
14671476 return values_length_bytes + b"" .join (heads ) + b"" .join (tails )
14681477
14691478
1470- def _decode_tuple_items ( # noqa: PLR0912
1479+ def _decode_tuple_items ( # noqa: PLR0912, PLR0915
14711480 value : bytes , child_types : list [_TypeInfo ]
14721481) -> list [typing .Any ]:
14731482 dynamic_segments : list [list [int ]] = [] # Store the start and end of a dynamic element
14741483 value_partitions : list [bytes ] = []
14751484 i = 0
14761485 array_index = 0
14771486
1487+ is_homogeneous_child_types = len (set (child_types )) == 1
1488+
14781489 while i < len (child_types ):
14791490 child_type = child_types [i ]
14801491 if child_type .is_dynamic :
@@ -1489,8 +1500,12 @@ def _decode_tuple_items( # noqa: PLR0912
14891500 value_partitions .append (b"" )
14901501 array_index += _ABI_LENGTH_SIZE
14911502 elif isinstance (child_type , _BoolTypeInfo ):
1492- before = _find_bool_types (child_types , i , - 1 )
1493- after = _find_bool_types (child_types , i , 1 )
1503+ before = _find_bool_types (
1504+ child_types , index = i , delta = - 1 , is_homogeneous = is_homogeneous_child_types
1505+ )
1506+ after = _find_bool_types (
1507+ child_types , index = i , delta = 1 , is_homogeneous = is_homogeneous_child_types
1508+ )
14941509
14951510 if before % 8 != 0 :
14961511 raise ValueError ("expected before index should have number of bool mod 8 equal 0" )
0 commit comments