event fixes

This commit is contained in:
John Lancaster
2026-02-22 18:25:26 -06:00
parent 07bdbe0b46
commit 0e58dc7a86
2 changed files with 13 additions and 4 deletions

View File

@@ -35,7 +35,7 @@ class HookedMapping(HookedContainer[T], MutableMapping[T, MutableNesting[T]]):
def insert(self, key: T, value: MutableNesting[T]) -> None:
self._data[key] = value
if self.hook:
self.hook(e.AddItemEvent(value, path=self.new_path(key)))
self.hook(e.AddItemEvent(self.new_path(key), value))
# HookedContainer methods
@@ -58,10 +58,10 @@ class HookedMapping(HookedContainer[T], MutableMapping[T, MutableNesting[T]]):
else:
self._data[key] = value
if self.hook:
self.hook(e.SetItemEvent(value, path=self.new_path(key)))
self.hook(e.SetItemEvent(self.new_path(key), value))
def __delitem__(self, key: T) -> None:
item = self._data[key]
del self._data[key]
if self.hook:
self.hook(e.RemoveItemEvent(item, path=self.new_path(key)))
self.hook(e.RemoveItemEvent(self.new_path(key), item))

View File

@@ -24,9 +24,18 @@ class TestHookedMapping:
class TestMappingOps:
def test_setitem(self):
m = HookedMapping({})
added = []
m = HookedMapping({}, lambda e: added.append(e.item))
m["a"] = 1
assert m._data == {"a": 1}
assert added == [1]
def test_nested_setitem(self):
added = []
m = HookedMapping({"a": {}}, lambda e: added.append(e.item))
m["a"]["x"] = 1
assert m._data == {"a": {"x": 1}}
assert added == [1]
def test_getitem(self):
m = HookedMapping({"a": 1})