typing updates

This commit is contained in:
John Lancaster
2026-10-08 20:50:03 -05:00
parent fe696e7780
commit 1159347cca
4 changed files with 46 additions and 90 deletions
+16 -23
View File
@@ -1,14 +1,14 @@
from collections.abc import MutableMapping, MutableSequence
from typing import TypeVar
from collections.abc import MutableMapping
from collections.abc import MutableSequence
from . import events as e
from .container import HookedContainer, MutableNesting
from .container import HookedContainer
from .container import MutableNesting
from .events import HookFunction
T = TypeVar("T")
from .mixins import MutableMappingMixin
class HookedMapping(HookedContainer[T], MutableMapping[T, MutableNesting[T]]):
class HookedMapping[T](MutableMappingMixin[T, MutableNesting[T]], HookedContainer[T]):
_data: MutableMapping[T, MutableNesting[T]]
def __init__(
@@ -26,7 +26,6 @@ class HookedMapping(HookedContainer[T], MutableMapping[T, MutableNesting[T]]):
case _:
raise TypeError(f"Expected a mapping, got {type(existing)}")
# self._data = dict(it)
self._data = existing
self.hook = hook
self._root = root if root is not None else self
self._path = list(path) if path is not None else []
@@ -51,22 +50,16 @@ class HookedMapping(HookedContainer[T], MutableMapping[T, MutableNesting[T]]):
*,
suppress_hook: bool = False,
) -> MutableNesting[T] | None:
new_path = self.new_path(key)
event = (
e.SetItemEvent(self._root, new_path, value)
if key in self._data
else e.AddItemEvent(self._root, new_path, value)
)
match value:
case HookedMapping(_data=value):
self._data[key] = value
case HookedMapping(_data=data):
event_type = e.SetItemEvent if key in self._data else e.AddItemEvent
self._data[key] = data
if not suppress_hook:
self._notify(event_type, key, value)
return data
case _:
self._data[key] = value
if self.hook and not suppress_hook:
self.hook(event)
if suppress_hook:
self._data[key] = value
else:
super().__setitem__(key, value)
return value
def __delitem__(self, key: T) -> None:
item = self._data.pop(key)
if self.hook:
self.hook(e.RemoveItemEvent(self._root, self.new_path(key), item))