-
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 4 commits
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: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,10 @@ | |
|
||
import pytest | ||
from _algopy_testing import arc4 | ||
from algopy_testing import AlgopyTestContext, algopy_testing_context | ||
from algosdk import abi | ||
|
||
from tests.artifacts.Tuples.contract import TuplesContract | ||
from tests.util import int_to_bytes | ||
|
||
_abi_string = "hello" | ||
|
@@ -15,6 +17,14 @@ | |
_arc4_uint8 = arc4.UInt8(42) | ||
_arc4_bool = arc4.Bool(True) | ||
|
||
|
||
# New fixture | ||
@pytest.fixture() | ||
def context() -> typing.Generator[AlgopyTestContext, None, None]: | ||
with algopy_testing_context() as ctx: | ||
yield ctx | ||
|
||
|
||
_test_data = [ | ||
( | ||
abi.ABIType.from_string("(uint8,bool,bool)"), | ||
|
@@ -279,3 +289,8 @@ def _compare_abi_and_arc4_values( | |
assert arc4_value.native == abi_value | ||
else: | ||
assert arc4_value.bytes == int_to_bytes(abi_value, len(arc4_value.bytes)) | ||
|
||
|
||
def test_tuple_return_with_primitive_type(context: AlgopyTestContext) -> None: # noqa: ARG001 | ||
boblat marked this conversation as resolved.
Show resolved
Hide resolved
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. 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. @daniel-makerx The error is unique to versions of python starting from 3.13 and higher python/cpython#101162 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. Added a ci to run in matrix, here is a run from temporary commit reverting original code back you can see several tests failing due to new behaviour in 3.13 https://github.com/algorandfoundation/algorand-python-testing/actions/runs/15044342246/job/42283130835?pr=39 |
||
contract = TuplesContract() | ||
contract.test_tuple_with_primitive_type() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from algopy import ( | ||
ARC4Contract, | ||
UInt64, | ||
arc4, | ||
) | ||
|
||
|
||
class TuplesContract(ARC4Contract, avm_version=11): | ||
@arc4.abimethod() | ||
def test_tuple_with_primitive_type(self) -> tuple[UInt64, bool]: | ||
return (UInt64(0), True) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#pragma version 11 | ||
#pragma typetrack false | ||
|
||
// algopy.arc4.ARC4Contract.approval_program() -> uint64: | ||
main: | ||
// tests/artifacts/Tuples/contract.py:8 | ||
// class TuplesContract(ARC4Contract, avm_version=11): | ||
txn NumAppArgs | ||
bz main_bare_routing@6 | ||
pushbytes 0x7229d79a // method "test_tuple_with_primitive_type()(uint64,bool)" | ||
txna ApplicationArgs 0 | ||
match main_test_tuple_with_primitive_type_route@3 | ||
|
||
main_after_if_else@10: | ||
// tests/artifacts/Tuples/contract.py:8 | ||
// class TuplesContract(ARC4Contract, avm_version=11): | ||
pushint 0 // 0 | ||
return | ||
|
||
main_test_tuple_with_primitive_type_route@3: | ||
// tests/artifacts/Tuples/contract.py:9 | ||
// @arc4.abimethod() | ||
txn OnCompletion | ||
! | ||
assert // OnCompletion is not NoOp | ||
txn ApplicationID | ||
assert // can only call when not creating | ||
pushbytes 0x151f7c75000000000000000080 | ||
log | ||
pushint 1 // 1 | ||
return | ||
|
||
main_bare_routing@6: | ||
// tests/artifacts/Tuples/contract.py:8 | ||
// class TuplesContract(ARC4Contract, avm_version=11): | ||
txn OnCompletion | ||
bnz main_after_if_else@10 | ||
txn ApplicationID | ||
! | ||
assert // can only call when creating | ||
pushint 1 // 1 | ||
return |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{ | ||
"hints": { | ||
"test_tuple_with_primitive_type()(uint64,bool)": { | ||
"call_config": { | ||
"no_op": "CALL" | ||
} | ||
} | ||
}, | ||
"source": { | ||
"approval": "I3ByYWdtYSB2ZXJzaW9uIDExCiNwcmFnbWEgdHlwZXRyYWNrIGZhbHNlCgovLyBhbGdvcHkuYXJjNC5BUkM0Q29udHJhY3QuYXBwcm92YWxfcHJvZ3JhbSgpIC0+IHVpbnQ2NDoKbWFpbjoKICAgIC8vIHRlc3RzL2FydGlmYWN0cy9UdXBsZXMvY29udHJhY3QucHk6OAogICAgLy8gY2xhc3MgVHVwbGVzQ29udHJhY3QoQVJDNENvbnRyYWN0LCBhdm1fdmVyc2lvbj0xMSk6CiAgICB0eG4gTnVtQXBwQXJncwogICAgYnogbWFpbl9iYXJlX3JvdXRpbmdANgogICAgcHVzaGJ5dGVzIDB4NzIyOWQ3OWEgLy8gbWV0aG9kICJ0ZXN0X3R1cGxlX3dpdGhfcHJpbWl0aXZlX3R5cGUoKSh1aW50NjQsYm9vbCkiCiAgICB0eG5hIEFwcGxpY2F0aW9uQXJncyAwCiAgICBtYXRjaCBtYWluX3Rlc3RfdHVwbGVfd2l0aF9wcmltaXRpdmVfdHlwZV9yb3V0ZUAzCgptYWluX2FmdGVyX2lmX2Vsc2VAMTA6CiAgICAvLyB0ZXN0cy9hcnRpZmFjdHMvVHVwbGVzL2NvbnRyYWN0LnB5OjgKICAgIC8vIGNsYXNzIFR1cGxlc0NvbnRyYWN0KEFSQzRDb250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global": { | ||
"num_byte_slices": 0, | ||
"num_uints": 0 | ||
}, | ||
"local": { | ||
"num_byte_slices": 0, | ||
"num_uints": 0 | ||
} | ||
}, | ||
"schema": { | ||
"global": { | ||
"declared": {}, | ||
"reserved": {} | ||
}, | ||
"local": { | ||
"declared": {}, | ||
"reserved": {} | ||
} | ||
}, | ||
"contract": { | ||
"name": "TuplesContract", | ||
"methods": [ | ||
{ | ||
"name": "test_tuple_with_primitive_type", | ||
"args": [], | ||
"readonly": false, | ||
"returns": { | ||
"type": "(uint64,bool)" | ||
} | ||
} | ||
], | ||
"networks": {} | ||
}, | ||
"bare_call_config": { | ||
"no_op": "CREATE" | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.