55from _algopy_testing .primitives .bytes import Bytes
66from _algopy_testing .primitives .uint64 import UInt64
77from _algopy_testing .protocols import BytesBacked , Serializable , UInt64Backed
8+ from _algopy_testing .serialize import (
9+ deserialize_from_bytes ,
10+ serialize_to_bytes ,
11+ )
812
913_TValue = typing .TypeVar ("_TValue" )
1014SerializableValue = int | bytes
@@ -21,12 +25,16 @@ def serialize(value: _TValue) -> SerializableValue:
2125 return value .bytes .value
2226 elif isinstance (value , Serializable ):
2327 return value .serialize ()
28+ elif isinstance (value , tuple ):
29+ return serialize_to_bytes (value )
2430 else :
2531 raise TypeError (f"Unsupported type: { type (value )} " )
2632
2733
2834def deserialize (typ : type [_TValue ], value : SerializableValue ) -> _TValue :
29- if issubclass (typ , bool ):
35+ if (typing .get_origin (typ ) is tuple or issubclass (typ , tuple )) and isinstance (value , bytes ):
36+ return () if not value else deserialize_from_bytes (typ , value ) # type: ignore[return-value]
37+ elif issubclass (typ , bool ):
3038 return value != 0 # type: ignore[return-value]
3139 elif issubclass (typ , UInt64 | Bytes ):
3240 return typ (value ) # type: ignore[arg-type, return-value]
@@ -55,7 +63,7 @@ def cast_from_bytes(typ: type[_TValue], value: bytes) -> _TValue:
5563 """
5664 from _algopy_testing .utils import as_int64
5765
58- if issubclass (typ , bool | UInt64Backed | UInt64 ):
66+ if isinstance ( typ , type ) and issubclass (typ , bool | UInt64Backed | UInt64 ):
5967 if len (value ) > 8 :
6068 raise ValueError ("uint64 value too big" )
6169 serialized : SerializableValue = int .from_bytes (value )
0 commit comments