Skip to content

Commit

Permalink
Update EIP-7495: Allow setting properties in StableContainer impl
Browse files Browse the repository at this point in the history
Merged by EIP-Bot.
  • Loading branch information
etan-status authored Nov 29, 2023
1 parent d6be759 commit 9f20f20
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions assets/eip-7495/stable_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,35 @@ def __getattr__(self, item):
fnode = data.getter(2**get_depth(self.__class__.N) + findex)
return ftyp.view_from_backing(fnode)

def __setattr__(self, key, value):
if key[0] == '_':
super().__setattr__(key, value)
else:
try:
(findex, ftyp, fopt) = self.__class__._field_indices[key]
except KeyError:
raise AttributeError(f"unknown attribute {key}")

next_backing = self.get_backing()

assert value is not None or fopt
active_fields = self.active_fields()
active_fields.set(findex, value is not None)
next_backing = next_backing.rebind_right(active_fields.get_backing())

if value is not None:
if isinstance(value, ftyp):
fnode = value.get_backing()
else:
fnode = ftyp.coerce_view(value).get_backing()
else:
fnode = zero_node(0)
data = next_backing.get_left()
next_data = data.setter(2**get_depth(self.__class__.N) + findex)(fnode)
next_backing = next_backing.rebind_left(next_data)

self.set_backing(next_backing)

def _get_field_val_repr(self, fkey: str, ftyp: Type[View], fopt: bool) -> str:
field_start = ' ' + fkey + ': ' + (
('Optional[' if fopt else '') + ftyp.__name__ + (']' if fopt else '')
Expand Down

0 comments on commit 9f20f20

Please sign in to comment.