default-ish behavior

This commit is contained in:
John Lancaster
2026-02-22 18:02:32 -06:00
parent dcc8a37627
commit 62204abdd0

View File

@@ -40,7 +40,9 @@ class HookedMapping(HookedContainer[T], MutableMapping[T, MutableNesting[T]]):
# HookedContainer methods # HookedContainer methods
def __getitem__(self, key: T) -> MutableNesting[T]: def __getitem__(self, key: T) -> MutableNesting[T]:
value = self._data.get(key, {}) if key not in self._data:
self.insert(key, {})
value = self._data[key]
match value: match value:
case HookedMapping(_data=seq): case HookedMapping(_data=seq):
return type(self)(seq, hook=self.hook, path=self.new_path(key)) return type(self)(seq, hook=self.hook, path=self.new_path(key))
@@ -50,6 +52,9 @@ class HookedMapping(HookedContainer[T], MutableMapping[T, MutableNesting[T]]):
return item return item
def __setitem__(self, key: T, value: MutableNesting[T]) -> None: def __setitem__(self, key: T, value: MutableNesting[T]) -> None:
if key not in self._data:
self.insert(key, value)
return
self._data[key] = value self._data[key] = value
if self.hook: if self.hook:
self.hook(e.SetItemEvent(index=key, item=value, path=self.new_path(key))) self.hook(e.SetItemEvent(index=key, item=value, path=self.new_path(key)))