@@ -25,66 +25,71 @@ def identity(i: _T) -> _T:
25
25
return i
26
26
27
27
28
- def get_native_to_arc4_serializer (typ : type [_T ]) -> _Serializer : # type: ignore[type-arg] # noqa: PLR0911
28
+ def get_native_to_arc4_serializer (typ : type [_T ]) -> _Serializer : # type: ignore[type-arg] # noqa: PLR0911, PLR0912
29
29
from _algopy_testing import arc4
30
30
from _algopy_testing .models import Account
31
31
from _algopy_testing .primitives import BigUInt , Bytes , ImmutableArray , String
32
32
from _algopy_testing .protocols import UInt64Backed
33
33
34
- if issubclass (typ , arc4 ._ABIEncoded ):
34
+ origin_typ = typing .get_origin (typ )
35
+
36
+ if inspect .isclass (typ ) and issubclass (typ , arc4 ._ABIEncoded ):
35
37
return _Serializer (
36
38
native_type = typ , arc4_type = typ , native_to_arc4 = identity , arc4_to_native = identity
37
39
)
38
- if issubclass (typ , bool ):
39
- return _Serializer (
40
- native_type = typ ,
41
- arc4_type = arc4 .Bool ,
42
- native_to_arc4 = arc4 .Bool ,
43
- arc4_to_native = lambda n : n .native ,
44
- )
45
- if issubclass (typ , UInt64Backed ):
46
- return _Serializer (
47
- native_type = typ ,
48
- arc4_type = arc4 .UInt64 ,
49
- native_to_arc4 = lambda n : arc4 .UInt64 (n .int_ ),
50
- arc4_to_native = lambda a : typ .from_int (a .native ),
51
- )
52
- if issubclass (typ , BigUInt ):
53
- return _Serializer (
54
- native_type = typ ,
55
- arc4_type = arc4 .UInt512 ,
56
- native_to_arc4 = arc4 .UInt512 ,
57
- arc4_to_native = lambda a : a .native ,
58
- )
59
- if issubclass (typ , Account ):
60
- return _Serializer (
61
- native_type = typ ,
62
- arc4_type = arc4 .Address ,
63
- native_to_arc4 = arc4 .Address ,
64
- arc4_to_native = lambda a : a .native ,
65
- )
66
- if issubclass (typ , UInt64 ):
67
- return _Serializer (
68
- native_type = typ ,
69
- arc4_type = arc4 .UInt64 ,
70
- native_to_arc4 = arc4 .UInt64 ,
71
- arc4_to_native = lambda a : a .native ,
72
- )
73
- if issubclass (typ , Bytes ):
74
- return _Serializer (
75
- native_type = typ ,
76
- arc4_type = arc4 .DynamicBytes ,
77
- native_to_arc4 = arc4 .DynamicBytes ,
78
- arc4_to_native = lambda a : a .native ,
79
- )
80
- if issubclass (typ , String ):
81
- return _Serializer (
82
- native_type = typ ,
83
- arc4_type = arc4 .String ,
84
- native_to_arc4 = arc4 .String ,
85
- arc4_to_native = lambda a : a .native ,
86
- )
87
- if issubclass (typ , tuple ) or typing .get_origin (typ ) is tuple :
40
+ # For types that are expected to be simple classes for these specific checks
41
+ if inspect .isclass (typ ):
42
+ if issubclass (typ , bool ):
43
+ return _Serializer (
44
+ native_type = typ ,
45
+ arc4_type = arc4 .Bool ,
46
+ native_to_arc4 = arc4 .Bool ,
47
+ arc4_to_native = lambda n : n .native ,
48
+ )
49
+ if issubclass (typ , UInt64Backed ):
50
+ return _Serializer (
51
+ native_type = typ ,
52
+ arc4_type = arc4 .UInt64 ,
53
+ native_to_arc4 = lambda n : arc4 .UInt64 (n .int_ ),
54
+ arc4_to_native = lambda a : typ .from_int (a .native ),
55
+ )
56
+ if issubclass (typ , BigUInt ):
57
+ return _Serializer (
58
+ native_type = typ ,
59
+ arc4_type = arc4 .UInt512 ,
60
+ native_to_arc4 = arc4 .UInt512 ,
61
+ arc4_to_native = lambda a : a .native ,
62
+ )
63
+ if issubclass (typ , Account ):
64
+ return _Serializer (
65
+ native_type = typ ,
66
+ arc4_type = arc4 .Address ,
67
+ native_to_arc4 = arc4 .Address ,
68
+ arc4_to_native = lambda a : a .native ,
69
+ )
70
+ if issubclass (typ , UInt64 ):
71
+ return _Serializer (
72
+ native_type = typ ,
73
+ arc4_type = arc4 .UInt64 ,
74
+ native_to_arc4 = arc4 .UInt64 ,
75
+ arc4_to_native = lambda a : a .native ,
76
+ )
77
+ if issubclass (typ , Bytes ):
78
+ return _Serializer (
79
+ native_type = typ ,
80
+ arc4_type = arc4 .DynamicBytes ,
81
+ native_to_arc4 = arc4 .DynamicBytes ,
82
+ arc4_to_native = lambda a : a .native ,
83
+ )
84
+ if issubclass (typ , String ):
85
+ return _Serializer (
86
+ native_type = typ ,
87
+ arc4_type = arc4 .String ,
88
+ native_to_arc4 = arc4 .String ,
89
+ arc4_to_native = lambda a : a .native ,
90
+ )
91
+
92
+ if origin_typ is tuple or (inspect .isclass (typ ) and issubclass (typ , tuple )):
88
93
if typing .NamedTuple in getattr (typ , "__orig_bases__" , []):
89
94
tuple_fields : Sequence [type ] = list (inspect .get_annotations (typ ).values ())
90
95
else :
0 commit comments