From 03642a09b3829f6346aa2abc71746ff494f9466f Mon Sep 17 00:00:00 2001 From: Edward Amor Date: Tue, 26 Oct 2021 15:17:57 +0000 Subject: [PATCH] fix: static list of structs as arg (#2515) * fix: allow list of struct types as fn argument * test: list of struct args * test: dynamic subtype in struct, list of structs arg --- tests/parser/types/test_lists.py | 33 ++++++++++++++++++++++++++++++++ vyper/old_codegen/types/types.py | 2 +- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/tests/parser/types/test_lists.py b/tests/parser/types/test_lists.py index 6c4be0c092..f791b1c293 100644 --- a/tests/parser/types/test_lists.py +++ b/tests/parser/types/test_lists.py @@ -362,3 +362,36 @@ def foo() -> (uint256, uint256[3], uint256[2]): """ c = get_contract(code) assert c.foo() == [666, [1, 2, 3], [88, 12]] + + +def test_list_of_structs_arg(get_contract): + code = """ +struct Foo: + x: uint256 + y: uint256 + +@external +def bar(_baz: Foo[3]) -> uint256: + sum: uint256 = 0 + for i in range(3): + sum += _baz[i].x * _baz[i].y + return sum + """ + c = get_contract(code) + c_input = [[x, y] for x, y in zip(range(3), range(3))] + assert c.bar(c_input) == 5 # 0 * 0 + 1 * 1 + 2 * 2 + + +def test_list_of_structs_arg_with_dynamic_type(get_contract): + code = """ +struct Foo: + x: uint256 + _msg: String[32] + +@external +def bar(_baz: Foo[3]) -> String[96]: + return concat(_baz[0]._msg, _baz[1]._msg, _baz[2]._msg) + """ + c = get_contract(code) + c_input = [[i, msg] for i, msg in enumerate(("Hello ", "world", "!!!!"))] + assert c.bar(c_input) == "Hello world!!!!" diff --git a/vyper/old_codegen/types/types.py b/vyper/old_codegen/types/types.py index 8d9c021730..bbd0547e56 100644 --- a/vyper/old_codegen/types/types.py +++ b/vyper/old_codegen/types/types.py @@ -162,7 +162,7 @@ def canonicalize_type(t, is_indexed=False): return byte_type if isinstance(t, ListType): - if not isinstance(t.subtype, (ListType, BaseType)): + if not isinstance(t.subtype, (ListType, BaseType, StructType)): raise InvalidType(f"List of {t.subtype} not allowed") return canonicalize_type(t.subtype) + f"[{t.count}]"