From 1d2fcd13f9b531e96cf77f030b1fc5ad5014571f Mon Sep 17 00:00:00 2001 From: John Lancaster <32917998+jsl12@users.noreply.github.com> Date: Sat, 21 Feb 2026 22:30:50 -0600 Subject: [PATCH] abstractmethods --- src/hooked_containers/common.py | 27 ++++++++++++++++++++------- src/hooked_containers/events.py | 1 + src/hooked_containers/sequence.py | 9 ++++++--- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/hooked_containers/common.py b/src/hooked_containers/common.py index 4e79c1f..c576311 100644 --- a/src/hooked_containers/common.py +++ b/src/hooked_containers/common.py @@ -1,5 +1,6 @@ -from collections.abc import Callable, MutableMapping, MutableSequence -from typing import Generic, TypeVar +from abc import ABC, abstractmethod +from collections.abc import MutableMapping, MutableSequence +from typing import Generic, Protocol, TypeVar from . import events as e @@ -8,22 +9,31 @@ T = TypeVar("T") type MutableNesting[T] = T | MutableSequence[T] | MutableMapping[T, MutableNesting[T]] -class HookedContainer(Generic[T]): +class HookFunction(Protocol): + def __call__(self, event: e.ChangeEvent[T]) -> None: ... + + +class HookedContainer(Generic[T], ABC): _path: MutableSequence[int] - hook: Callable[[e.ChangeEvent[T]], None] | None + hook: HookFunction def __repr__(self): return f"{self.__class__.__name__}({self._data!r})" - def __len__(self): - return len(self._data) - def __iter__(self): return iter(self._data) def __contains__(self, value): return value in self._data + # MutableSequence Methods + + def __len__(self): + return len(self._data) + + @abstractmethod + def __getitem__(self, key): ... + def __setitem__(self, s, value): self._data[s] = value if self.hook: @@ -33,3 +43,6 @@ class HookedContainer(Generic[T]): del self._data[s] if self.hook: self.hook(e.RemoveItemEvent(index=s, item=self._data[s])) + + @abstractmethod + def insert(self, index, value): ... diff --git a/src/hooked_containers/events.py b/src/hooked_containers/events.py index 5f2136d..3568f5c 100644 --- a/src/hooked_containers/events.py +++ b/src/hooked_containers/events.py @@ -7,6 +7,7 @@ T = TypeVar("T") @dataclass(frozen=True) class ChangeEvent(Generic[T]): index: int + item: T @dataclass(frozen=True) diff --git a/src/hooked_containers/sequence.py b/src/hooked_containers/sequence.py index dd190e5..8af5160 100644 --- a/src/hooked_containers/sequence.py +++ b/src/hooked_containers/sequence.py @@ -1,19 +1,22 @@ -from collections.abc import Iterable, MutableSequence, Sequence +from collections.abc import MutableSequence from copy import copy from typing import TypeVar from . import events as e -from .common import HookedContainer +from .common import HookedContainer, HookFunction T = TypeVar("T") class HookedList(HookedContainer[T], MutableSequence[T]): _data: MutableSequence[T] + path: MutableSequence[int] - def __init__(self, hook, existing: Iterable[T], path: Sequence[int] | None = None): + def __init__(self, hook: HookFunction, existing: MutableSequence[T], path: MutableSequence[int] | None = None): self.hook = hook match existing: + case HookedContainer(_data=seq): + self._data = seq case MutableSequence() as seq: self._data = seq case _ as it: