Skip to content

Commit

Permalink
fix: static list of structs as arg (#2515)
Browse files Browse the repository at this point in the history
* fix: allow list of struct types as fn argument

* test: list of struct args

* test: dynamic subtype in struct, list of structs arg
  • Loading branch information
skellet0r authored Oct 26, 2021
1 parent 317625f commit 03642a0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 33 additions & 0 deletions tests/parser/types/test_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!!!!"
2 changes: 1 addition & 1 deletion vyper/old_codegen/types/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}]"

Expand Down

0 comments on commit 03642a0

Please sign in to comment.