Skip to content

fix: fixes type checking of tuples with primitive types #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 16, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 57 additions & 52 deletions src/_algopy_testing/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,66 +25,71 @@ def identity(i: _T) -> _T:
return i


def get_native_to_arc4_serializer(typ: type[_T]) -> _Serializer: # type: ignore[type-arg] # noqa: PLR0911
def get_native_to_arc4_serializer(typ: type[_T]) -> _Serializer: # type: ignore[type-arg] # noqa: PLR0911, PLR0912
from _algopy_testing import arc4
from _algopy_testing.models import Account
from _algopy_testing.primitives import BigUInt, Bytes, ImmutableArray, String
from _algopy_testing.protocols import UInt64Backed

if issubclass(typ, arc4._ABIEncoded):
origin_typ = typing.get_origin(typ)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: I think this should be declared closer to the usage or perhaps even inlined as it is used in only one spot.


if inspect.isclass(typ) and issubclass(typ, arc4._ABIEncoded):
return _Serializer(
native_type=typ, arc4_type=typ, native_to_arc4=identity, arc4_to_native=identity
)
if issubclass(typ, bool):
return _Serializer(
native_type=typ,
arc4_type=arc4.Bool,
native_to_arc4=arc4.Bool,
arc4_to_native=lambda n: n.native,
)
if issubclass(typ, UInt64Backed):
return _Serializer(
native_type=typ,
arc4_type=arc4.UInt64,
native_to_arc4=lambda n: arc4.UInt64(n.int_),
arc4_to_native=lambda a: typ.from_int(a.native),
)
if issubclass(typ, BigUInt):
return _Serializer(
native_type=typ,
arc4_type=arc4.UInt512,
native_to_arc4=arc4.UInt512,
arc4_to_native=lambda a: a.native,
)
if issubclass(typ, Account):
return _Serializer(
native_type=typ,
arc4_type=arc4.Address,
native_to_arc4=arc4.Address,
arc4_to_native=lambda a: a.native,
)
if issubclass(typ, UInt64):
return _Serializer(
native_type=typ,
arc4_type=arc4.UInt64,
native_to_arc4=arc4.UInt64,
arc4_to_native=lambda a: a.native,
)
if issubclass(typ, Bytes):
return _Serializer(
native_type=typ,
arc4_type=arc4.DynamicBytes,
native_to_arc4=arc4.DynamicBytes,
arc4_to_native=lambda a: a.native,
)
if issubclass(typ, String):
return _Serializer(
native_type=typ,
arc4_type=arc4.String,
native_to_arc4=arc4.String,
arc4_to_native=lambda a: a.native,
)
if issubclass(typ, tuple) or typing.get_origin(typ) is tuple:
# For types that are expected to be simple classes for these specific checks
if inspect.isclass(typ):
if issubclass(typ, bool):
return _Serializer(
native_type=typ,
arc4_type=arc4.Bool,
native_to_arc4=arc4.Bool,
arc4_to_native=lambda n: n.native,
)
if issubclass(typ, UInt64Backed):
return _Serializer(
native_type=typ,
arc4_type=arc4.UInt64,
native_to_arc4=lambda n: arc4.UInt64(n.int_),
arc4_to_native=lambda a: typ.from_int(a.native),
)
if issubclass(typ, BigUInt):
return _Serializer(
native_type=typ,
arc4_type=arc4.UInt512,
native_to_arc4=arc4.UInt512,
arc4_to_native=lambda a: a.native,
)
if issubclass(typ, Account):
return _Serializer(
native_type=typ,
arc4_type=arc4.Address,
native_to_arc4=arc4.Address,
arc4_to_native=lambda a: a.native,
)
if issubclass(typ, UInt64):
return _Serializer(
native_type=typ,
arc4_type=arc4.UInt64,
native_to_arc4=arc4.UInt64,
arc4_to_native=lambda a: a.native,
)
if issubclass(typ, Bytes):
return _Serializer(
native_type=typ,
arc4_type=arc4.DynamicBytes,
native_to_arc4=arc4.DynamicBytes,
arc4_to_native=lambda a: a.native,
)
if issubclass(typ, String):
return _Serializer(
native_type=typ,
arc4_type=arc4.String,
native_to_arc4=arc4.String,
arc4_to_native=lambda a: a.native,
)

if origin_typ is tuple or (inspect.isclass(typ) and issubclass(typ, tuple)):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also add inspect.isclass(typ) clause to the next if statement on line 117 just to be safe?

if typing.NamedTuple in getattr(typ, "__orig_bases__", []):
tuple_fields: Sequence[type] = list(inspect.get_annotations(typ).values())
else:
Expand Down