types module

This commit is contained in:
John Lancaster
2026-02-22 21:00:12 -06:00
parent 8eea62f403
commit 0c3dfde336
7 changed files with 55 additions and 47 deletions

View File

@@ -1,12 +0,0 @@
from .container import HookedContainer, HookFunction
from .mapping import HookedMapping
from .sequence import HookedList
from .state import NameSpaceState
__all__ = [
"HookedContainer",
"HookFunction",
"HookedMapping",
"HookedList",
"NameSpaceState",
]

View File

@@ -1,17 +1,13 @@
from abc import ABC, abstractmethod
from collections.abc import Container, Iterable, MutableMapping, MutableSequence, Sized
from typing import Protocol, TypeVar
from typing import TypeVar
from . import events as e
from .events import HookFunction
from .types import MutableNesting
T = TypeVar("T")
type MutableNesting[T] = T | MutableSequence[T] | MutableMapping[T, MutableNesting[T]]
class HookFunction(Protocol):
def __call__(self, event: e.ChangeEvent[T]) -> None: ...
class HookedContainer(ABC, Sized, Iterable[T], Container[T]):
_data: MutableNesting[T]

View File

@@ -1,26 +1,30 @@
from collections.abc import MutableSequence
from dataclasses import dataclass
from typing import Generic, TypeVar
from dataclasses import dataclass, field
from typing import Generic, Protocol, TypeVar
from .types import MutableNesting
T = TypeVar("T")
@dataclass(frozen=True)
class ChangeEvent(Generic[T]):
root: MutableNesting[T] = field(repr=False)
path: MutableSequence[int]
item: T
@dataclass(frozen=True)
class AddItemEvent(ChangeEvent[T]):
item: T
class AddItemEvent(ChangeEvent[T]): ...
@dataclass(frozen=True)
class SetItemEvent(ChangeEvent[T]):
item: T
class SetItemEvent(ChangeEvent[T]): ...
@dataclass(frozen=True)
class RemoveItemEvent(ChangeEvent[T]):
item: T
class RemoveItemEvent(ChangeEvent[T]): ...
class HookFunction(Protocol):
def __call__(self, event: ChangeEvent[T]) -> None: ...

View File

@@ -2,7 +2,8 @@ from collections.abc import MutableMapping, MutableSequence
from typing import TypeVar
from . import events as e
from .container import HookedContainer, HookFunction, MutableNesting
from .container import HookedContainer, MutableNesting
from .events import HookFunction
T = TypeVar("T")
@@ -46,7 +47,7 @@ class HookedMapping(HookedContainer[T], MutableMapping[T, MutableNesting[T]]):
suppress_hook: bool = False,
) -> MutableNesting[T] | None:
new_path = self.new_path(key)
event = e.SetItemEvent(new_path, value) if key in self._data else e.AddItemEvent(new_path, value)
event = e.SetItemEvent(self, new_path, value) if key in self._data else e.AddItemEvent(self, new_path, value)
match value:
case HookedMapping(_data=value):
self._data[key] = value
@@ -59,4 +60,4 @@ class HookedMapping(HookedContainer[T], MutableMapping[T, MutableNesting[T]]):
def __delitem__(self, key: T) -> None:
item = self._data.pop(key)
if self.hook:
self.hook(e.RemoveItemEvent(self.new_path(key), item))
self.hook(e.RemoveItemEvent(self, self.new_path(key), item))

View File

@@ -3,7 +3,8 @@ from copy import copy
from typing import TypeVar
from . import events as e
from .container import HookedContainer, HookFunction
from .container import HookedContainer
from .types import HookFunction
T = TypeVar("T")

View File

@@ -1,31 +1,43 @@
from collections.abc import MutableMapping
from datetime import datetime
from .mapping import HookedMapping
class EntityState(HookedMapping[str]):
pass
def __setitem__(self, key, value):
super().__setitem__(key, value)
super().__setitem__("last_changed", datetime.now(), suppress_hook=True)
class DomainState(HookedMapping[str]):
_data: EntityState
_data: MutableMapping[str, EntityState]
def __setitem__(self, key, value):
super().__setitem__(key, value)
super().__setitem__("last_changed", datetime.now())
def __getitem__(self, key):
match super().__getitem__(key):
case HookedMapping(_data=val):
return EntityState(val, hook=self.hook, path=self.new_path(key))
case _ as val:
raise TypeError(f"Expected a mapping for domain state, got {type(val)}")
class NameSpaceState(HookedMapping[str]):
_data: DomainState
def __iter__(self):
return super().__iter__()
def __setitem__(self, key, value):
super().__setitem__(key, value)
# print("ns SetItem")
_data: MutableMapping[str, DomainState]
def __getitem__(self, key):
val = super().__getitem__(key)
# print("ns GetItem")
match super().__getitem__(key):
case HookedMapping(_data=val):
return DomainState(val, hook=self.hook, path=self.new_path(key))
case _ as val:
raise TypeError(f"Expected a mapping for domain state, got {type(val)}")
class FullState(HookedMapping[str]):
_data: MutableMapping[str, NameSpaceState]
def __getitem__(self, key):
match super().__getitem__(key):
case HookedMapping(_data=val):
return NameSpaceState(val, hook=self.hook, path=self.new_path(key))
case _ as val:
raise TypeError(f"Expected a mapping for namespace state, got {type(val)}")

View File

@@ -0,0 +1,6 @@
from collections.abc import MutableMapping, MutableSequence
from typing import TypeVar
T = TypeVar("T")
type MutableNesting[T] = T | MutableSequence[T] | MutableMapping[T, MutableNesting[T]]