- 
                Notifications
    You must be signed in to change notification settings 
- Fork 28
          Add get_model_state to get validated doc.
          #309
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|  | @@ -2,7 +2,19 @@ | |||||
|  | ||||||
| from functools import partial | ||||||
| from inspect import iscoroutinefunction | ||||||
| from typing import Any, Awaitable, Callable, Generic, Iterable, Literal, Type, TypeVar, Union, cast, overload | ||||||
| from typing import ( | ||||||
| Any, | ||||||
| Awaitable, | ||||||
| Callable, | ||||||
| Generic, | ||||||
| Iterable, | ||||||
| Literal, | ||||||
| Type, | ||||||
| TypeVar, | ||||||
| Union, | ||||||
| cast, | ||||||
| overload, | ||||||
| ) | ||||||
|  | ||||||
| from anyio import BrokenResourceError, create_memory_object_stream | ||||||
| from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream | ||||||
|  | @@ -15,7 +27,9 @@ | |||||
| from ._transaction import NewTransaction, ReadTransaction, Transaction | ||||||
|  | ||||||
| T = TypeVar("T", bound=BaseType) | ||||||
| TransactionOrSubdocsEvent = TypeVar("TransactionOrSubdocsEvent", bound=TransactionEvent | SubdocsEvent) | ||||||
| TransactionOrSubdocsEvent = TypeVar( | ||||||
| "TransactionOrSubdocsEvent", bound=TransactionEvent | SubdocsEvent | ||||||
| ) | ||||||
|  | ||||||
|  | ||||||
| class Doc(BaseDoc, Generic[T]): | ||||||
|  | @@ -35,7 +49,7 @@ def __init__( | |||||
| client_id: int | None = None, | ||||||
| skip_gc: bool | None = None, | ||||||
| doc: _Doc | None = None, | ||||||
| Model=None, | ||||||
| Model: Any | None = None, | ||||||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
        Suggested change
       
 | ||||||
| allow_multithreading: bool = False, | ||||||
| ) -> None: | ||||||
| """ | ||||||
|  | @@ -47,8 +61,9 @@ def __init__( | |||||
| allow_multithreading: Whether to allow the document to be used in different threads. | ||||||
| """ | ||||||
| super().__init__( | ||||||
| client_id=client_id, skip_gc=skip_gc, doc=doc, Model=Model, allow_multithreading=allow_multithreading | ||||||
| client_id=client_id, skip_gc=skip_gc, doc=doc, allow_multithreading=allow_multithreading | ||||||
| ) | ||||||
| self._Model = Model | ||||||
| for k, v in init.items(): | ||||||
| self[k] = v | ||||||
| if Model is not None: | ||||||
|  | @@ -150,6 +165,14 @@ def get_state(self) -> bytes: | |||||
| assert txn._txn is not None | ||||||
| return self._doc.get_state(txn._txn) | ||||||
|  | ||||||
| def get_model_state(self) -> Any: | ||||||
| if self._Model is None: | ||||||
| raise RuntimeError( | ||||||
| "no Model defined for doc. Instantiate Doc with Doc(Model=PydanticModel)" | ||||||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
        Suggested change
       
 | ||||||
| ) | ||||||
| d = {k: self[k].to_py() for k in self._Model.model_fields} | ||||||
| return self._Model.model_validate(d) | ||||||
|  | ||||||
| def get_update(self, state: bytes | None = None) -> bytes: | ||||||
| """ | ||||||
| Args: | ||||||
|  | @@ -174,7 +197,7 @@ def apply_update(self, update: bytes) -> None: | |||||
| twin_doc.apply_update(update) | ||||||
| d = {k: twin_doc[k].to_py() for k in self._Model.model_fields} | ||||||
| try: | ||||||
| self._Model(**d) | ||||||
| self._Model.model_validate(d) | ||||||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the style change i refer to in the PR description. | ||||||
| except Exception as e: | ||||||
| self._twin_doc = Doc(dict(self)) | ||||||
| raise e | ||||||
|  | @@ -292,7 +315,8 @@ def _roots(self) -> dict[str, T]: | |||||
|  | ||||||
| def observe( | ||||||
| self, | ||||||
| callback: Callable[[TransactionEvent], None] | Callable[[TransactionEvent], Awaitable[None]], | ||||||
| callback: Callable[[TransactionEvent], None] | ||||||
| | Callable[[TransactionEvent], Awaitable[None]], | ||||||
| ) -> Subscription: | ||||||
| """ | ||||||
| Subscribes a callback to be called with the document change event. | ||||||
|  | @@ -405,7 +429,9 @@ async def main(): | |||||
| observe = self.observe_subdocs if subdocs else self.observe | ||||||
| if not self._send_streams[subdocs]: | ||||||
| if async_transactions: | ||||||
| self._event_subscription[subdocs] = observe(partial(self._async_send_event, subdocs)) | ||||||
| self._event_subscription[subdocs] = observe( | ||||||
| partial(self._async_send_event, subdocs) | ||||||
| ) | ||||||
| else: | ||||||
| self._event_subscription[subdocs] = observe(partial(self._send_event, subdocs)) | ||||||
| send_stream, receive_stream = create_memory_object_stream[ | ||||||
|  | ||||||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| import gc | ||
| import platform | ||
| import sys | ||
| import time | ||
| from functools import partial | ||
|  | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you're introducing
ruff, maybe we should usepre-committo check and lint?