-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd97147
commit 82b41ba
Showing
9 changed files
with
24 additions
and
60 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
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
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,46 +1,14 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Any, TypeVar | ||
from typing import TYPE_CHECKING, Any, Protocol, TypeVar, runtime_checkable | ||
|
||
if TYPE_CHECKING: | ||
from weave.trace.vals import WeaveObject | ||
|
||
_registry: dict[str, type] = {} | ||
|
||
|
||
T = TypeVar("T") | ||
|
||
|
||
def register(cls: type[T]) -> type[T]: | ||
"""Decorator to register a class with the objectify function. | ||
Registered classes will be able to be deserialized directly into their base objects | ||
instead of into a WeaveObject.""" | ||
_registry[cls.__name__] = cls | ||
|
||
@runtime_checkable | ||
class WeaveConvertible(Protocol): | ||
@classmethod | ||
def from_uri(cls: type[T], uri: str) -> T: | ||
import weave | ||
|
||
obj = weave.ref(uri).get() | ||
if isinstance(obj, cls): | ||
return obj | ||
return cls(**obj.model_dump()) | ||
|
||
cls.from_uri = from_uri | ||
|
||
return cls | ||
|
||
|
||
def objectify(obj: WeaveObject) -> Any: | ||
if not (cls_name := getattr(obj, "_class_name", None)): | ||
return obj | ||
|
||
if cls_name not in _registry: | ||
return obj | ||
|
||
cls = _registry[cls_name] | ||
if hasattr(cls, "from_uri"): | ||
return cls.from_uri(obj.ref.uri()) | ||
|
||
return obj | ||
def from_obj(cls, obj: WeaveObject) -> Any: ... |
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
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