@@ -140,7 +140,7 @@ class _ABIEncoded(BytesBacked):
140
140
def from_bytes (cls , value : algopy .Bytes | bytes , / ) -> typing .Self :
141
141
"""Construct an instance from the underlying bytes (no validation)"""
142
142
instance = cls ()
143
- instance ._value = as_bytes (value )
143
+ instance ._value = value . value if isinstance (value , Bytes ) else value
144
144
return instance
145
145
146
146
@classmethod
@@ -556,11 +556,11 @@ class Bool(_ABIEncoded):
556
556
_value : bytes
557
557
558
558
# 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 )
561
561
562
562
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
564
564
565
565
def __eq__ (self , other : object ) -> bool :
566
566
try :
@@ -576,8 +576,7 @@ def __bool__(self) -> bool:
576
576
@property
577
577
def native (self ) -> bool :
578
578
"""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
581
580
582
581
def __str__ (self ) -> str :
583
582
return f"{ self .native } "
@@ -669,7 +668,8 @@ def __init__(self, *_items: _TArrayItem):
669
668
f"item must be of type { self ._type_info .item_type !r} , not { item ._type_info !r} "
670
669
)
671
670
672
- self ._value = _encode (items )
671
+ item_list = list (items )
672
+ self ._value = _encode (item_list )
673
673
674
674
def __iter__ (self ) -> Iterator [_TArrayItem ]:
675
675
# """Returns an iterator for the items in the array"""
@@ -854,7 +854,8 @@ def __init__(self, *_items: _TArrayItem):
854
854
raise TypeError (
855
855
f"item must be of type { self ._type_info .item_type !r} , not { item ._type_info !r} "
856
856
)
857
- self ._value = self ._encode_with_length (items )
857
+ item_list = list (items )
858
+ self ._value = self ._encode_with_length (item_list )
858
859
859
860
def __iter__ (self ) -> typing .Iterator [_TArrayItem ]:
860
861
"""Returns an iterator for the items in the array."""
@@ -1294,6 +1295,9 @@ def _find_bool(
1294
1295
is_looking_forward = delta > 0
1295
1296
is_looking_backward = delta < 0
1296
1297
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
+
1297
1301
while True :
1298
1302
curr = index + delta * until
1299
1303
is_curr_at_end = curr == values_length - 1
@@ -1311,12 +1315,16 @@ def _find_bool(
1311
1315
return until
1312
1316
1313
1317
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 :
1315
1321
"""Helper function to find consecutive booleans from current index in a tuple."""
1316
1322
until = 0
1317
1323
is_looking_forward = delta > 0
1318
1324
is_looking_backward = delta < 0
1319
1325
values_length = len (values )
1326
+ if is_homogeneous :
1327
+ return 0 if is_looking_backward else values_length - index - 1
1320
1328
while True :
1321
1329
curr = index + delta * until
1322
1330
is_curr_at_end = curr == values_length - 1
@@ -1438,6 +1446,7 @@ def _encode( # noqa: PLR0912
1438
1446
raise ValueError (
1439
1447
"expected before index should have number of bool mod 8 equal 0"
1440
1448
)
1449
+
1441
1450
after = min (7 , after )
1442
1451
consecutive_bool_list = [values [i ] for i in range (i , i + after + 1 )]
1443
1452
compressed_int = _compress_multiple_bool (consecutive_bool_list )
@@ -1467,14 +1476,16 @@ def _encode( # noqa: PLR0912
1467
1476
return values_length_bytes + b"" .join (heads ) + b"" .join (tails )
1468
1477
1469
1478
1470
- def _decode_tuple_items ( # noqa: PLR0912
1479
+ def _decode_tuple_items ( # noqa: PLR0912, PLR0915
1471
1480
value : bytes , child_types : list [_TypeInfo ]
1472
1481
) -> list [typing .Any ]:
1473
1482
dynamic_segments : list [list [int ]] = [] # Store the start and end of a dynamic element
1474
1483
value_partitions : list [bytes ] = []
1475
1484
i = 0
1476
1485
array_index = 0
1477
1486
1487
+ is_homogeneous_child_types = len (set (child_types )) == 1
1488
+
1478
1489
while i < len (child_types ):
1479
1490
child_type = child_types [i ]
1480
1491
if child_type .is_dynamic :
@@ -1489,8 +1500,12 @@ def _decode_tuple_items( # noqa: PLR0912
1489
1500
value_partitions .append (b"" )
1490
1501
array_index += _ABI_LENGTH_SIZE
1491
1502
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
+ )
1494
1509
1495
1510
if before % 8 != 0 :
1496
1511
raise ValueError ("expected before index should have number of bool mod 8 equal 0" )
0 commit comments