Skip to content

Commit e8ed8d2

Browse files
committed
refactor: simplify size_of implementation
1 parent 01c055a commit e8ed8d2

File tree

1 file changed

+15
-38
lines changed

1 file changed

+15
-38
lines changed
Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,22 @@
1-
import inspect
2-
import typing
1+
import types
32

43
from _algopy_testing.primitives.uint64 import UInt64
54

65

76
def size_of(typ: type | object, /) -> UInt64:
8-
from _algopy_testing.arc4 import _TupleTypeInfo, _TypeInfo, get_max_bytes_static_len
9-
from _algopy_testing.models.account import Account
10-
from _algopy_testing.models.application import Application
11-
from _algopy_testing.models.asset import Asset
7+
from _algopy_testing.arc4 import get_max_bytes_static_len
128
from _algopy_testing.serialize import get_native_to_arc4_serializer
139

14-
# Check for types with _type_info attribute
15-
if hasattr(typ, "_type_info") or isinstance(typ, _TypeInfo):
16-
size = get_max_bytes_static_len(typ if isinstance(typ, _TypeInfo) else typ._type_info)
17-
if size is not None:
18-
return UInt64(size)
19-
else:
20-
raise ValueError(f"{typ} is dynamically sized")
21-
22-
# Fixed-size types
23-
fixed_sizes = {UInt64: 8, Account: 32, Application: 8, Asset: 8, bool: 8}
24-
25-
# Check if type matches any fixed-size type
26-
for cls, size in fixed_sizes.items():
27-
if isinstance(typ, cls) or typ == cls:
28-
return UInt64(size)
29-
30-
# Handle tuple types
31-
is_tuple = (isinstance(typ, type) and issubclass(typ, tuple)) or typing.get_origin(
32-
typ
33-
) is tuple
34-
if is_tuple:
35-
if typing.NamedTuple in getattr(typ, "__orig_bases__", []):
36-
tuple_fields = list(
37-
inspect.get_annotations(typ if isinstance(typ, type) else typ.__class__).values()
38-
)
39-
else:
40-
tuple_fields = list(typing.get_args(typ))
41-
42-
type_infos = [get_native_to_arc4_serializer(f).arc4_type._type_info for f in tuple_fields]
43-
return size_of(_TupleTypeInfo(type_infos))
44-
45-
raise ValueError(f"{typ} is dynamically sized")
10+
if isinstance(typ, types.GenericAlias):
11+
pass
12+
elif not isinstance(typ, type):
13+
typ = type(typ)
14+
15+
if typ is bool: # treat bool on its own as a uint64
16+
typ = UInt64
17+
serializer = get_native_to_arc4_serializer(typ) # type: ignore[arg-type]
18+
type_info = serializer.arc4_type._type_info
19+
size = get_max_bytes_static_len(type_info)
20+
if size is None:
21+
raise ValueError(f"{typ} is dynamically sized")
22+
return UInt64(size)

0 commit comments

Comments
 (0)