-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from 1 commit
71fdd5f
8189ec8
49592d7
fd24e5f
06b2a7f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also add |
||
if typing.NamedTuple in getattr(typ, "__orig_bases__", []): | ||
tuple_fields: Sequence[type] = list(inspect.get_annotations(typ).values()) | ||
else: | ||
|
Uh oh!
There was an error while loading. Please reload this page.