From 99c5c8e93889fca48545d981bac909086dc0614e Mon Sep 17 00:00:00 2001 From: Hang Su Date: Fri, 30 Sep 2022 13:38:14 -0400 Subject: [PATCH] new test case and corner case for array casees --- pyteal/ast/abi/util.py | 19 ++++++++-- pyteal/ast/abi/util_test.py | 75 +++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 3 deletions(-) diff --git a/pyteal/ast/abi/util.py b/pyteal/ast/abi/util.py index 3c5af2f9b..e851d15c3 100644 --- a/pyteal/ast/abi/util.py +++ b/pyteal/ast/abi/util.py @@ -507,6 +507,8 @@ def type_spec_is_assignable(a: TypeSpec, b: TypeSpec) -> bool: ArrayTypeSpec, StaticArrayTypeSpec, DynamicArrayTypeSpec, + StringTypeSpec, + AddressTypeSpec, ) match a, b: @@ -532,9 +534,20 @@ def type_spec_is_assignable(a: TypeSpec, b: TypeSpec) -> bool: return a.length_static() == b.length_static() case DynamicArrayTypeSpec(), DynamicArrayTypeSpec(): return True - case _: - return False - case ArrayTypeSpec(), _: + return False + case (ArrayTypeSpec(), _) | (_, ArrayTypeSpec()): + if isinstance(b, ArrayTypeSpec): + a, b = b, a + match a, b: + case DynamicArrayTypeSpec(), StringTypeSpec(): + a, b = cast(DynamicArrayTypeSpec, a), cast(StringTypeSpec, b) + return a.value_type_spec() == b.value_type_spec() + case StaticArrayTypeSpec(), AddressTypeSpec(): + a, b = cast(StaticArrayTypeSpec, a), cast(AddressTypeSpec, b) + return ( + a.value_type_spec() == b.value_type_spec() + and a.length_static() == b.length_static() + ) return False if isinstance(a, type(b)): diff --git a/pyteal/ast/abi/util_test.py b/pyteal/ast/abi/util_test.py index 4efb274da..f7dafc79f 100644 --- a/pyteal/ast/abi/util_test.py +++ b/pyteal/ast/abi/util_test.py @@ -781,6 +781,21 @@ class NamedTDecl(abi.NamedTuple): c: abi.Field[abi.Transaction] +class NamedTComp0(abi.NamedTuple): + a0: abi.Field[abi.String] + a1: abi.Field[abi.StaticArray[abi.Byte, Literal[32]]] + + +class NamedTComp1(abi.NamedTuple): + b0: abi.Field[abi.DynamicBytes] + b1: abi.Field[abi.Address] + + +class NamedTComp2(abi.NamedTuple): + b1: abi.Field[abi.Address] + b0: abi.Field[abi.DynamicBytes] + + TYPE_SPEC_ASSIGNABLE_CASES = [ (abi.PaymentTransactionTypeSpec(), abi.TransactionTypeSpec(), True), ( @@ -841,6 +856,66 @@ class NamedTDecl(abi.NamedTuple): abi.type_spec_from_annotation(NamedTDecl), True, ), + ( + abi.type_spec_from_annotation(abi.StaticBytes[Literal[7]]), + abi.type_spec_from_annotation(abi.StaticArray[abi.Byte, Literal[11]]), + False, + ), + ( + abi.type_spec_from_annotation(NamedTDecl), + abi.type_spec_from_annotation(NamedTDecl), + True, + ), + ( + abi.type_spec_from_annotation(abi.String), + abi.type_spec_from_annotation(abi.DynamicBytes), + True, + ), + ( + abi.type_spec_from_annotation(abi.DynamicArray[abi.Byte]), + abi.type_spec_from_annotation(abi.String), + True, + ), + ( + abi.type_spec_from_annotation(abi.DynamicArray[abi.Uint32]), + abi.type_spec_from_annotation(abi.String), + False, + ), + ( + abi.type_spec_from_annotation(abi.Address), + abi.type_spec_from_annotation(abi.StaticArray[abi.Byte, Literal[32]]), + True, + ), + ( + abi.type_spec_from_annotation(abi.StaticBytes[Literal[32]]), + abi.type_spec_from_annotation(abi.Address), + True, + ), + ( + abi.type_spec_from_annotation(abi.StaticBytes[Literal[33]]), + abi.type_spec_from_annotation(abi.Address), + False, + ), + ( + abi.type_spec_from_annotation(NamedTComp0), + abi.type_spec_from_annotation(NamedTComp1), + True, + ), + ( + abi.type_spec_from_annotation(NamedTComp1), + abi.type_spec_from_annotation(NamedTComp0), + True, + ), + ( + abi.type_spec_from_annotation(NamedTDecl), + abi.type_spec_from_annotation(NamedTComp0), + False, + ), + ( + abi.type_spec_from_annotation(NamedTComp2), + abi.type_spec_from_annotation(NamedTComp0), + False, + ), ]