This commit is contained in:
John Lancaster
2026-02-21 18:35:37 -06:00
parent a58f043eaa
commit cd3679cbd7

View File

@@ -1,4 +1,4 @@
from collections.abc import Callable, MutableSequence from collections.abc import Callable, Iterable, MutableSequence
from dataclasses import dataclass from dataclasses import dataclass
from enum import Enum, auto from enum import Enum, auto
from typing import Generic, TypeVar from typing import Generic, TypeVar
@@ -36,8 +36,8 @@ class HookedList(Generic[T], MutableSequence[T]):
_data: list[T] _data: list[T]
hook: Callable[[ListChangeEvent[T]], None] | None hook: Callable[[ListChangeEvent[T]], None] | None
def __init__(self, *args, hook=None, **kwargs): def __init__(self, iterable: Iterable[T], hook=None):
self._data = list(*args, **kwargs) self._data = list(iterable)
self.hook = hook self.hook = hook
def __repr__(self): def __repr__(self):
@@ -53,8 +53,15 @@ class HookedList(Generic[T], MutableSequence[T]):
return len(self._data) return len(self._data)
def __getitem__(self, s): def __getitem__(self, s):
print("Getting item:", s) # print("Getting item:", s)
return self._data[s] match self._data[s]:
case HookedList() as hs:
hs.hook = self.hook
return hs
case MutableSequence() as seq:
return HookedList(seq, hook=self.hook)
case _ as item:
return item
def __setitem__(self, s, value): def __setitem__(self, s, value):
self._data[s] = value self._data[s] = value