This commit is contained in:
John Lancaster
2026-02-21 23:19:32 -06:00
parent 941e689c19
commit 0980145b10

View File

@@ -2,6 +2,7 @@ from collections.abc import MutableMapping, MutableSequence, Sequence
from copy import copy from copy import copy
from typing import TypeVar from typing import TypeVar
from . import events as e
from .common import HookedContainer, MutableNesting from .common import HookedContainer, MutableNesting
from .sequence import HookedList from .sequence import HookedList
@@ -23,29 +24,23 @@ class HookedMapping(HookedContainer[T], MutableMapping[T, MutableNesting[T]]):
def __getitem__(self, key: T) -> MutableNesting[T]: def __getitem__(self, key: T) -> MutableNesting[T]:
value = self._data[key] value = self._data[key]
new_path = copy(self._path)
new_path.append(key)
match value: match value:
case MutableMapping() as mapping: case MutableMapping() as mapping:
new_path = copy(self._path) return HookedMapping(hook=self.hook, existing=mapping, path=new_path)
new_path.append(key)
return type(self)(self.hook, existing=mapping, path=new_path)
case MutableSequence() as seq: case MutableSequence() as seq:
new_path = copy(self._path) return HookedList(hook=self.hook, existing=seq, path=new_path)
new_path.append(key)
return HookedList(self.hook, existing=seq, path=new_path)
case _ as item: case _ as item:
return item return item
def __setitem__(self, key: T, value: MutableNesting[T]) -> None: def __setitem__(self, key: T, value: MutableNesting[T]) -> None:
self._data[key] = value self._data[key] = value
if self.hook: if self.hook:
from . import events as e
self.hook(e.SetItemEvent(index=key, item=value)) self.hook(e.SetItemEvent(index=key, item=value))
def __delitem__(self, key: T) -> None: def __delitem__(self, key: T) -> None:
item = self._data[key] item = self._data[key]
del self._data[key] del self._data[key]
if self.hook: if self.hook:
from . import events as e self.hook(e.RemoveItemEvent(index=key, item=item, path=self._path))
self.hook(e.RemoveItemEvent(index=key, item=item))