-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #243 from ndif-team/dev
Dev
- Loading branch information
Showing
3 changed files
with
75 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,50 @@ | ||
from __future__ import annotations | ||
|
||
import json | ||
from datetime import datetime | ||
from typing import TYPE_CHECKING, Dict, List, Union | ||
|
||
from pydantic import BaseModel, ConfigDict | ||
from pydantic import BaseModel, ConfigDict, TypeAdapter, field_serializer | ||
|
||
from .. import NNsight | ||
from .format.types import * | ||
|
||
if TYPE_CHECKING: | ||
from ..contexts.backends.RemoteBackend import RemoteMixin | ||
|
||
OBJECT_TYPES = Union[SessionType, TracerType, SessionModel, TracerModel] | ||
|
||
|
||
class RequestModel(BaseModel): | ||
|
||
model_config = ConfigDict(arbitrary_types_allowed=True, protected_namespaces=()) | ||
|
||
object: Union[SessionType, TracerType, SessionModel, TracerModel] | ||
model_config = ConfigDict( | ||
arbitrary_types_allowed=True, protected_namespaces=() | ||
) | ||
|
||
object: str | OBJECT_TYPES | ||
model_key: str | ||
|
||
id: str = None | ||
received: datetime = None | ||
|
||
session_id: Optional[str] = None | ||
|
||
@field_serializer("object") | ||
def serialize_object( | ||
self, object: Union[SessionType, TracerType, SessionModel, TracerModel] | ||
) -> str: | ||
|
||
if isinstance(object, str): | ||
return object | ||
|
||
return object.model_dump_json() | ||
|
||
def deserialize(self, model: NNsight) -> "RemoteMixin": | ||
|
||
handler = DeserializeHandler(model=model) | ||
|
||
return self.object.deserialize(handler) | ||
object = TypeAdapter( | ||
OBJECT_TYPES, config=RequestModel.model_config | ||
).validate_python(json.loads(self.object)) | ||
|
||
return object.deserialize(handler) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters