abstractmethods

This commit is contained in:
John Lancaster
2026-02-21 22:30:50 -06:00
parent 428db7366e
commit 1d2fcd13f9
3 changed files with 27 additions and 10 deletions

View File

@@ -1,5 +1,6 @@
from collections.abc import Callable, MutableMapping, MutableSequence from abc import ABC, abstractmethod
from typing import Generic, TypeVar from collections.abc import MutableMapping, MutableSequence
from typing import Generic, Protocol, TypeVar
from . import events as e from . import events as e
@@ -8,22 +9,31 @@ T = TypeVar("T")
type MutableNesting[T] = T | MutableSequence[T] | MutableMapping[T, MutableNesting[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] _path: MutableSequence[int]
hook: Callable[[e.ChangeEvent[T]], None] | None hook: HookFunction
def __repr__(self): def __repr__(self):
return f"{self.__class__.__name__}({self._data!r})" return f"{self.__class__.__name__}({self._data!r})"
def __len__(self):
return len(self._data)
def __iter__(self): def __iter__(self):
return iter(self._data) return iter(self._data)
def __contains__(self, value): def __contains__(self, value):
return value in self._data return value in self._data
# MutableSequence Methods
def __len__(self):
return len(self._data)
@abstractmethod
def __getitem__(self, key): ...
def __setitem__(self, s, value): def __setitem__(self, s, value):
self._data[s] = value self._data[s] = value
if self.hook: if self.hook:
@@ -33,3 +43,6 @@ class HookedContainer(Generic[T]):
del self._data[s] del self._data[s]
if self.hook: if self.hook:
self.hook(e.RemoveItemEvent(index=s, item=self._data[s])) self.hook(e.RemoveItemEvent(index=s, item=self._data[s]))
@abstractmethod
def insert(self, index, value): ...

View File

@@ -7,6 +7,7 @@ T = TypeVar("T")
@dataclass(frozen=True) @dataclass(frozen=True)
class ChangeEvent(Generic[T]): class ChangeEvent(Generic[T]):
index: int index: int
item: T
@dataclass(frozen=True) @dataclass(frozen=True)

View File

@@ -1,19 +1,22 @@
from collections.abc import Iterable, MutableSequence, Sequence from collections.abc import MutableSequence
from copy import copy from copy import copy
from typing import TypeVar from typing import TypeVar
from . import events as e from . import events as e
from .common import HookedContainer from .common import HookedContainer, HookFunction
T = TypeVar("T") T = TypeVar("T")
class HookedList(HookedContainer[T], MutableSequence[T]): class HookedList(HookedContainer[T], MutableSequence[T]):
_data: 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 self.hook = hook
match existing: match existing:
case HookedContainer(_data=seq):
self._data = seq
case MutableSequence() as seq: case MutableSequence() as seq:
self._data = seq self._data = seq
case _ as it: case _ as it: