diff --git a/src/hooked_containers/mapping.py b/src/hooked_containers/mapping.py index f993329..2eb67f4 100644 --- a/src/hooked_containers/mapping.py +++ b/src/hooked_containers/mapping.py @@ -30,7 +30,7 @@ class HookedMapping(HookedContainer[T], MutableMapping[T, MutableNesting[T]]): def __getitem__(self, key: T) -> MutableNesting[T]: if key not in self._data: - return self.__setitem__(key, {}) + return HookedMapping(self.__setitem__(key, {}), hook=self.hook, path=self.new_path(key)) value = self._data[key] match value: case MutableMapping() as mapping: @@ -38,7 +38,13 @@ class HookedMapping(HookedContainer[T], MutableMapping[T, MutableNesting[T]]): case _ as item: return item - def __setitem__(self, key: T, value: MutableNesting[T]) -> MutableNesting[T] | None: + def __setitem__( + self, + key: T, + value: MutableNesting[T], + *, + suppress_hook: bool = False, + ) -> MutableNesting[T] | None: new_path = self.new_path(key) event = e.SetItemEvent(new_path, value) if key in self._data else e.AddItemEvent(new_path, value) match value: @@ -46,7 +52,7 @@ class HookedMapping(HookedContainer[T], MutableMapping[T, MutableNesting[T]]): self._data[key] = value case _: self._data[key] = value - if self.hook: + if self.hook and not suppress_hook: self.hook(event) return value