diff --git a/src/hooked_containers/common.py b/src/hooked_containers/common.py index a7d8d5f..4e79c1f 100644 --- a/src/hooked_containers/common.py +++ b/src/hooked_containers/common.py @@ -1,10 +1,12 @@ -from collections.abc import Callable, MutableSequence +from collections.abc import Callable, MutableMapping, MutableSequence from typing import Generic, TypeVar from . import events as e T = TypeVar("T") +type MutableNesting[T] = T | MutableSequence[T] | MutableMapping[T, MutableNesting[T]] + class HookedContainer(Generic[T]): _path: MutableSequence[int] diff --git a/src/hooked_containers/mapping.py b/src/hooked_containers/mapping.py index ebd3507..c1162c8 100644 --- a/src/hooked_containers/mapping.py +++ b/src/hooked_containers/mapping.py @@ -2,26 +2,26 @@ from collections.abc import MutableMapping, MutableSequence, Sequence from copy import copy from typing import TypeVar -from .common import HookedContainer +from .common import HookedContainer, MutableNesting from .sequence import HookedList T = TypeVar("T") -class HookedMapping(HookedContainer[T], MutableMapping[T, T | MutableSequence[T] | MutableMapping]): - _data: MutableMapping[T, T | MutableSequence[T] | MutableMapping] +class HookedMapping(HookedContainer[T], MutableMapping[T, MutableNesting[T]]): + _data: MutableMapping[T, MutableNesting[T]] def __init__( self, hook, - existing: MutableMapping[T, T | MutableSequence[T] | MutableMapping], + existing: MutableMapping[T, MutableNesting[T]], path: Sequence[int] | None = None, ): self.hook = hook self._data = existing 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] match value: case MutableMapping() as mapping: @@ -35,7 +35,7 @@ class HookedMapping(HookedContainer[T], MutableMapping[T, T | MutableSequence[T] case _ as 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 if self.hook: from . import events as e