mutablenesting

This commit is contained in:
John Lancaster
2026-02-21 19:14:30 -06:00
parent 8d598ff135
commit 32ee15348b
2 changed files with 9 additions and 7 deletions

View File

@@ -1,10 +1,12 @@
from collections.abc import Callable, MutableSequence from collections.abc import Callable, MutableMapping, MutableSequence
from typing import Generic, TypeVar from typing import Generic, TypeVar
from . import events as e from . import events as e
T = TypeVar("T") T = TypeVar("T")
type MutableNesting[T] = T | MutableSequence[T] | MutableMapping[T, MutableNesting[T]]
class HookedContainer(Generic[T]): class HookedContainer(Generic[T]):
_path: MutableSequence[int] _path: MutableSequence[int]

View File

@@ -2,26 +2,26 @@ from collections.abc import MutableMapping, MutableSequence, Sequence
from copy import copy from copy import copy
from typing import TypeVar from typing import TypeVar
from .common import HookedContainer from .common import HookedContainer, MutableNesting
from .sequence import HookedList from .sequence import HookedList
T = TypeVar("T") T = TypeVar("T")
class HookedMapping(HookedContainer[T], MutableMapping[T, T | MutableSequence[T] | MutableMapping]): class HookedMapping(HookedContainer[T], MutableMapping[T, MutableNesting[T]]):
_data: MutableMapping[T, T | MutableSequence[T] | MutableMapping] _data: MutableMapping[T, MutableNesting[T]]
def __init__( def __init__(
self, self,
hook, hook,
existing: MutableMapping[T, T | MutableSequence[T] | MutableMapping], existing: MutableMapping[T, MutableNesting[T]],
path: Sequence[int] | None = None, path: Sequence[int] | None = None,
): ):
self.hook = hook self.hook = hook
self._data = existing self._data = existing
self._path = list(path) if path is not None else [] self._path = list(path) if path is not None else []
def __getitem__(self, key: T) -> T | MutableSequence[T] | MutableMapping: def __getitem__(self, key: T) -> MutableNesting[T]:
value = self._data[key] value = self._data[key]
match value: match value:
case MutableMapping() as mapping: case MutableMapping() as mapping:
@@ -35,7 +35,7 @@ class HookedMapping(HookedContainer[T], MutableMapping[T, T | MutableSequence[T]
case _ as item: case _ as item:
return item return item
def __setitem__(self, key: T, value: T | MutableSequence[T] | MutableMapping) -> None: def __setitem__(self, key: T, value: MutableNesting[T]) -> None:
self._data[key] = value self._data[key] = value
if self.hook: if self.hook:
from . import events as e from . import events as e