Skip to content

Commit e971ad6

Browse files
aorumbayevdaniel-makerx
authored andcommitted
fix: fixes type checking of tuples with primitive types
Adds checks to ensure that type validation applies to classes only.
1 parent edc96e7 commit e971ad6

File tree

1 file changed

+57
-52
lines changed

1 file changed

+57
-52
lines changed

src/_algopy_testing/serialize.py

+57-52
Original file line numberDiff line numberDiff line change
@@ -25,66 +25,71 @@ def identity(i: _T) -> _T:
2525
return i
2626

2727

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
2929
from _algopy_testing import arc4
3030
from _algopy_testing.models import Account
3131
from _algopy_testing.primitives import BigUInt, Bytes, ImmutableArray, String
3232
from _algopy_testing.protocols import UInt64Backed
3333

34-
if issubclass(typ, arc4._ABIEncoded):
34+
origin_typ = typing.get_origin(typ)
35+
36+
if inspect.isclass(typ) and issubclass(typ, arc4._ABIEncoded):
3537
return _Serializer(
3638
native_type=typ, arc4_type=typ, native_to_arc4=identity, arc4_to_native=identity
3739
)
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)):
8893
if typing.NamedTuple in getattr(typ, "__orig_bases__", []):
8994
tuple_fields: Sequence[type] = list(inspect.get_annotations(typ).values())
9095
else:

0 commit comments

Comments
 (0)