Skip to content

Commit

Permalink
feat[lang]: use keyword arguments for struct instantiation (vyperlang…
Browse files Browse the repository at this point in the history
…#3777)

This commit changes struct instantiation from taking a single dict
argument to taking keyword arguments.

For backwards compatibility (and to ease users into v0.4.0), the old
syntax is still supported, but a warning will be emitted.

---------

Co-authored-by: Charles Cooper <cooper.charles.m@gmail.com>
  • Loading branch information
tserg and charles-cooper authored Feb 20, 2024
1 parent 4177314 commit 0752760
Show file tree
Hide file tree
Showing 41 changed files with 357 additions and 328 deletions.
8 changes: 4 additions & 4 deletions examples/auctions/blind_auction.vy
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ def bid(_blindedBid: bytes32):
assert numBids < MAX_BIDS

# Add bid to mapping of all bids
self.bids[msg.sender][numBids] = Bid({
blindedBid: _blindedBid,
deposit: msg.value
})
self.bids[msg.sender][numBids] = Bid(
blindedBid=_blindedBid,
deposit=msg.value
)
self.bidCounts[msg.sender] += 1


Expand Down
8 changes: 4 additions & 4 deletions examples/voting/ballot.vy
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ def __init__(_proposalNames: bytes32[2]):
self.chairperson = msg.sender
self.voterCount = 0
for i: int128 in range(2):
self.proposals[i] = Proposal({
name: _proposalNames[i],
voteCount: 0
})
self.proposals[i] = Proposal(
name=_proposalNames[i],
voteCount=0
)
self.int128Proposals += 1

# Give a `voter` the right to vote on this ballot.
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/builtins/codegen/test_abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ def test_nested_struct(type, abi_type):
@external
def getStructList() -> {type}:
return [
NestedStruct({{t: MyStruct({{a: msg.sender, b: block.prevhash}}), foo: 1}}),
NestedStruct({{t: MyStruct({{a: msg.sender, b: block.prevhash}}), foo: 2}})
NestedStruct(t=MyStruct(a=msg.sender, b=block.prevhash), foo=1),
NestedStruct(t=MyStruct(a=msg.sender, b=block.prevhash), foo=2)
]
"""

Expand Down
24 changes: 12 additions & 12 deletions tests/functional/builtins/codegen/test_abi_decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ def abi_decode(x: Bytes[160]) -> (address, int128, bool, decimal, bytes32):
@external
def abi_decode_struct(x: Bytes[544]) -> Human:
human: Human = Human({
name: "",
pet: Animal({
name: "",
address_: empty(address),
id_: 0,
is_furry: False,
price: 0.0,
data: [0, 0, 0],
metadata: 0x0000000000000000000000000000000000000000000000000000000000000000
})
})
human: Human = Human(
name = "",
pet = Animal(
name = "",
address_ = empty(address),
id_ = 0,
is_furry = False,
price = 0.0,
data = [0, 0, 0],
metadata = 0x0000000000000000000000000000000000000000000000000000000000000000
)
)
human = _abi_decode(x, Human)
return human
"""
Expand Down
26 changes: 13 additions & 13 deletions tests/functional/builtins/codegen/test_abi_encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ def abi_encode(
ensure_tuple: bool,
include_method_id: bool
) -> Bytes[548]:
human: Human = Human({
name: name,
pet: Animal({
name: pet_name,
address_: pet_address,
id_: pet_id,
is_furry: pet_is_furry,
price: pet_price,
data: pet_data,
metadata: pet_metadata
}),
})
human: Human = Human(
name = name,
pet = Animal(
name = pet_name,
address_ = pet_address,
id_ = pet_id,
is_furry = pet_is_furry,
price = pet_price,
data = pet_data,
metadata = pet_metadata
),
)
if ensure_tuple:
if not include_method_id:
return _abi_encode(human) # default ensure_tuple=True
Expand Down Expand Up @@ -128,7 +128,7 @@ def test_abi_encode_length_failing(get_contract, assert_compile_failed, type, va
@internal
def foo():
x: WrappedBytes = WrappedBytes({{bs: {value}}})
x: WrappedBytes = WrappedBytes(bs={value})
y: {type}[96] = _abi_encode(x, ensure_tuple=True) # should be Bytes[128]
"""

Expand Down
37 changes: 17 additions & 20 deletions tests/functional/builtins/codegen/test_empty.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,22 +351,22 @@ def test_empty_struct(get_contract_with_gas_estimation):
@external
def foo():
self.foobar = FOOBAR({
a: 1,
b: 2,
c: True,
d: 3.0,
e: 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,
f: msg.sender
})
bar: FOOBAR = FOOBAR({
a: 1,
b: 2,
c: True,
d: 3.0,
e: 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,
f: msg.sender
})
self.foobar = FOOBAR(
a=1,
b=2,
c=True,
d=3.0,
e=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,
f=msg.sender
)
bar: FOOBAR = FOOBAR(
a=1,
b=2,
c=True,
d=3.0,
e=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,
f=msg.sender
)
self.foobar = empty(FOOBAR)
bar = empty(FOOBAR)
Expand Down Expand Up @@ -575,10 +575,7 @@ def test_map_clear_struct(get_contract_with_gas_estimation):
@external
def set():
self.structmap[123] = X({
a: 333,
b: 444
})
self.structmap[123] = X(a=333, b=444)
@external
def get() -> (int128, int128):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def foo(a: uint256 = 2**8): pass
b: uint256
@external
def foo(bar: Bar = Bar({a: msg.sender, b: 1})): pass
def foo(bar: Bar = Bar(a=msg.sender, b=1)): pass
""",
"""
struct Baz:
Expand All @@ -321,7 +321,7 @@ def foo(bar: Bar = Bar({a: msg.sender, b: 1})): pass
b: Baz
@external
def foo(bar: Bar = Bar({a: msg.sender, b: Baz({c: block.coinbase, d: -10})})): pass
def foo(bar: Bar = Bar(a=msg.sender, b=Baz(c=block.coinbase, d=-10))): pass
""",
"""
A: public(address)
Expand All @@ -341,7 +341,7 @@ def foo(a: int112 = min_value(int112)):
struct X:
x: int128
y: address
BAR: constant(X) = X({x: 1, y: 0x0000000000000000000000000000000000012345})
BAR: constant(X) = X(x=1, y=0x0000000000000000000000000000000000012345)
@external
def out_literals(a: int128 = BAR.x + 1) -> X:
return BAR
Expand All @@ -353,8 +353,8 @@ def out_literals(a: int128 = BAR.x + 1) -> X:
struct Y:
x: X
y: uint256
BAR: constant(X) = X({x: 1, y: 0x0000000000000000000000000000000000012345})
FOO: constant(Y) = Y({x: BAR, y: 256})
BAR: constant(X) = X(x=1, y=0x0000000000000000000000000000000000012345)
FOO: constant(Y) = Y(x=BAR, y=256)
@external
def out_literals(a: int128 = FOO.x.x + 1) -> Y:
return FOO
Expand All @@ -363,7 +363,7 @@ def out_literals(a: int128 = FOO.x.x + 1) -> Y:
struct Bar:
a: bool
BAR: constant(Bar) = Bar({a: True})
BAR: constant(Bar) = Bar(a=True)
@external
def foo(x: bool = True and not BAR.a):
Expand All @@ -373,7 +373,7 @@ def foo(x: bool = True and not BAR.a):
struct Bar:
a: uint256
BAR: constant(Bar) = Bar({ a: 123 })
BAR: constant(Bar) = Bar(a=123)
@external
def foo(x: bool = BAR.a + 1 > 456):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1586,7 +1586,7 @@ def test_struct_return_external_contract_call_1(get_contract_with_gas_estimation
y: address
@external
def out_literals() -> X:
return X({x: 1, y: 0x0000000000000000000000000000000000012345})
return X(x=1, y=0x0000000000000000000000000000000000012345)
"""

contract_2 = """
Expand Down Expand Up @@ -1618,7 +1618,7 @@ def test_struct_return_external_contract_call_2(get_contract_with_gas_estimation
z: Bytes[{ln}]
@external
def get_struct_x() -> X:
return X({{x: {i}, y: "{s}", z: b"{s}"}})
return X(x={i}, y="{s}", z=b"{s}")
"""

contract_2 = f"""
Expand Down Expand Up @@ -1648,7 +1648,7 @@ def test_struct_return_external_contract_call_3(get_contract_with_gas_estimation
x: int128
@external
def out_literals() -> X:
return X({x: 1})
return X(x=1)
"""

contract_2 = """
Expand Down Expand Up @@ -1676,7 +1676,7 @@ def test_constant_struct_return_external_contract_call_1(get_contract_with_gas_e
x: int128
y: address
BAR: constant(X) = X({x: 1, y: 0x0000000000000000000000000000000000012345})
BAR: constant(X) = X(x=1, y=0x0000000000000000000000000000000000012345)
@external
def out_literals() -> X:
Expand Down Expand Up @@ -1713,7 +1713,7 @@ def test_constant_struct_return_external_contract_call_2(
y: String[{ln}]
z: Bytes[{ln}]
BAR: constant(X) = X({{x: {i}, y: "{s}", z: b"{s}"}})
BAR: constant(X) = X(x={i}, y="{s}", z=b"{s}")
@external
def get_struct_x() -> X:
Expand Down Expand Up @@ -1746,7 +1746,7 @@ def test_constant_struct_return_external_contract_call_3(get_contract_with_gas_e
struct X:
x: int128
BAR: constant(X) = X({x: 1})
BAR: constant(X) = X(x=1)
@external
def out_literals() -> X:
Expand Down Expand Up @@ -1778,7 +1778,7 @@ def test_constant_struct_member_return_external_contract_call_1(get_contract_wit
x: int128
y: address
BAR: constant(X) = X({x: 1, y: 0x0000000000000000000000000000000000012345})
BAR: constant(X) = X(x=1, y=0x0000000000000000000000000000000000012345)
@external
def get_y() -> address:
Expand Down Expand Up @@ -1811,7 +1811,7 @@ def test_constant_struct_member_return_external_contract_call_2(
y: String[{ln}]
z: Bytes[{ln}]
BAR: constant(X) = X({{x: {i}, y: "{s}", z: b"{s}"}})
BAR: constant(X) = X(x={i}, y="{s}", z=b"{s}")
@external
def get_y() -> String[{ln}]:
Expand Down Expand Up @@ -1840,7 +1840,7 @@ def test_constant_struct_member_return_external_contract_call_3(get_contract_wit
struct X:
x: int128
BAR: constant(X) = X({x: 1})
BAR: constant(X) = X(x=1)
@external
def get_x() -> int128:
Expand Down Expand Up @@ -1874,7 +1874,7 @@ def test_constant_nested_struct_return_external_contract_call_1(get_contract_wit
a: X
b: uint256
BAR: constant(A) = A({a: X({x: 1, y: 0x0000000000000000000000000000000000012345}), b: 777})
BAR: constant(A) = A(a=X(x=1, y=0x0000000000000000000000000000000000012345), b=777)
@external
def out_literals() -> A:
Expand Down Expand Up @@ -1919,7 +1919,7 @@ def test_constant_nested_struct_return_external_contract_call_2(
a: X
b: uint256
BAR: constant(A) = A({{a: X({{x: {i}, y: "{s}", z: b"{s}"}}), b: 777}})
BAR: constant(A) = A(a=X(x={i}, y="{s}", z=b"{s}"), b=777)
@external
def get_struct_a() -> A:
Expand Down Expand Up @@ -1966,7 +1966,7 @@ def test_constant_nested_struct_return_external_contract_call_3(get_contract_wit
c: A
d: bool
BAR: constant(C) = C({c: A({a: X({x: 1, y: -1}), b: 777}), d: True})
BAR: constant(C) = C(c=A(a=X(x=1, y=-1), b=777), d=True)
@external
def out_literals() -> C:
Expand Down Expand Up @@ -2013,7 +2013,7 @@ def test_constant_nested_struct_member_return_external_contract_call_1(
a: X
b: uint256
BAR: constant(A) = A({a: X({x: 1, y: 0x0000000000000000000000000000000000012345}), b: 777})
BAR: constant(A) = A(a=X(x=1, y=0x0000000000000000000000000000000000012345), b=777)
@external
def get_y() -> address:
Expand Down Expand Up @@ -2051,7 +2051,7 @@ def test_constant_nested_struct_member_return_external_contract_call_2(
b: uint256
c: bool
BAR: constant(A) = A({{a: X({{x: {i}, y: "{s}", z: b"{s}"}}), b: 777, c: True}})
BAR: constant(A) = A(a=X(x={i}, y="{s}", z=b"{s}"), b=777, c=True)
@external
def get_y() -> String[{ln}]:
Expand Down Expand Up @@ -2091,7 +2091,7 @@ def test_constant_nested_struct_member_return_external_contract_call_3(
c: A
d: bool
BAR: constant(C) = C({c: A({a: X({x: 1, y: -1}), b: 777}), d: True})
BAR: constant(C) = C(c=A(a=X(x=1, y=-1), b=777), d=True)
@external
def get_y() -> int128:
Expand Down Expand Up @@ -2148,7 +2148,7 @@ def foo(x: X) -> Bytes[6]: nonpayable
@external
def bar(addr: address) -> Bytes[6]:
_X: X = X({x: 1, y: b"hello"})
_X: X = X(x=1, y=b"hello")
return Foo(addr).foo(_X)
"""

Expand Down Expand Up @@ -2180,7 +2180,7 @@ def foo(x: X) -> String[6]: nonpayable
@external
def bar(addr: address) -> String[6]:
_X: X = X({x: 1, y: "hello"})
_X: X = X(x=1, y="hello")
return Foo(addr).foo(_X)
"""

Expand Down Expand Up @@ -2208,7 +2208,7 @@ def foo(b: Bytes[6]) -> Bytes[6]: nonpayable
@external
def bar(addr: address) -> Bytes[6]:
_X: X = X({x: 1, y: b"hello"})
_X: X = X(x=1, y=b"hello")
return Foo(addr).foo(_X.y)
"""

Expand Down Expand Up @@ -2236,7 +2236,7 @@ def foo(b: String[6]) -> String[6]: nonpayable
@external
def bar(addr: address) -> String[6]:
_X: X = X({x: 1, y: "hello"})
_X: X = X(x=1, y="hello")
return Foo(addr).foo(_X.y)
"""

Expand Down Expand Up @@ -2433,7 +2433,7 @@ def return_64_bytes():
def return_64_bytes() -> BoolPair: nonpayable
@external
def bar(foo: Foo):
t: BoolPair = foo.return_64_bytes(default_return_value=BoolPair({x: True, y:True}))
t: BoolPair = foo.return_64_bytes(default_return_value=BoolPair(x=True, y=True))
assert t.x and t.y
"""
bad_1 = get_contract(bad_code_1)
Expand Down
Loading

0 comments on commit 0752760

Please sign in to comment.