preserving internal mutable seqs

This commit is contained in:
John Lancaster
2026-02-21 18:43:33 -06:00
parent cd3679cbd7
commit fcc8ad68ae

View File

@@ -1,4 +1,4 @@
from collections.abc import Callable, Iterable, MutableSequence from collections.abc import Callable, Iterable, MutableSequence, Sequence
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
@@ -33,11 +33,17 @@ class RemoveItemEvent(ListChangeEvent[T]):
class HookedList(Generic[T], MutableSequence[T]): class HookedList(Generic[T], MutableSequence[T]):
_data: list[T] _data: MutableSequence[T]
_path: MutableSequence[int]
hook: Callable[[ListChangeEvent[T]], None] | None hook: Callable[[ListChangeEvent[T]], None] | None
def __init__(self, iterable: Iterable[T], hook=None): def __init__(self, iterable: Iterable[T], path: Sequence[int] | None = None, *, hook=None):
self._data = list(iterable) match iterable:
case MutableSequence() as seq:
self._data = seq
case Iterable() as it:
self._data = list(it)
self._path = list(path) if path is not None else []
self.hook = hook self.hook = hook
def __repr__(self): def __repr__(self):