|
1 | | -import inspect |
2 | | -import typing |
| 1 | +import types |
3 | 2 |
|
4 | 3 | from _algopy_testing.primitives.uint64 import UInt64 |
5 | 4 |
|
6 | 5 |
|
7 | 6 | 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 |
12 | 8 | from _algopy_testing.serialize import get_native_to_arc4_serializer |
13 | 9 |
|
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