Skip to content

Commit f6657fb

Browse files
committed
Support dict-style value access
1 parent 34e87a6 commit f6657fb

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

eip712_structs/struct.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,17 @@ def from_message(cls, message_dict: dict) -> 'StructTuple':
251251

252252
return result
253253

254+
def __getitem__(self, key):
255+
"""Provide access directly to the underlying value dictionary"""
256+
return self.values.__getitem__(key)
257+
258+
def __setitem__(self, key, value):
259+
"""Provide access directly to the underlying value dictionary"""
260+
return self.values.__setitem__(key, value)
261+
262+
def __delete__(self, instance):
263+
raise TypeError('Deleting entries from an EIP712Struct is not allowed.')
264+
254265

255266
class StructTuple(NamedTuple):
256267
message: EIP712Struct

tests/test_encode_data.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,21 @@ def test_validation_errors():
187187
bool_type.encode_value(0)
188188
with pytest.raises(ValueError, match='Must be True or False.'):
189189
bool_type.encode_value(1)
190+
191+
192+
def test_value_access():
193+
class Foo(EIP712Struct):
194+
s = String()
195+
b = Bytes(32)
196+
197+
test_str = 'hello world'
198+
test_bytes = os.urandom(32)
199+
foo = Foo(s=test_str, b=test_bytes)
200+
201+
assert foo['s'] == test_str
202+
assert foo['b'] == test_bytes
203+
204+
test_bytes_2 = os.urandom(32)
205+
foo['b'] = test_bytes_2
206+
207+
assert foo['b'] == test_bytes_2

0 commit comments

Comments
 (0)