trulens.core.utils.pyschema¶
trulens.core.utils.pyschema
¶
Serialization of Python objects¶
In order to serialize (and optionally deserialize) python entities while still being able to inspect them in their serialized form, we employ several storage classes that mimic basic python entities:
Serializable representation | Python entity |
---|---|
Class | (python) class |
Module | (python) module |
Obj | (python) object |
Function | (python) function |
Method | (python) method |
Classes¶
Class
¶
Bases: SerialModel
A python class. Should be enough to deserialize the constructor. Also includes bases so that we can query subtyping relationships without deserializing the class first.
Obj
¶
Bases: SerialModel
An object that may or may not be loadable from its serialized form. Do not
use for base types that don't have a class. Loadable if init_bindings
is
not None.
Bindings
¶
Bases: SerialModel
Functions¶
of_bound_arguments
staticmethod
¶
of_bound_arguments(
b: BoundArguments,
skip_self: bool = True,
arguments_only: bool = False,
) -> Bindings
Populate Bindings from inspect.BoundArguments.
PARAMETER | DESCRIPTION |
---|---|
b
|
BoundArguments to populate from.
TYPE:
|
skip_self
|
If True, skip the first argument if it is named "self".
TYPE:
|
arguments_only
|
If True, only populate kwargs from arguments. This includes the same arguments as otherwise except it provides all of them by name even if they were bound by position.
TYPE:
|
FunctionOrMethod
¶
Bases: SerialModel
Functions¶
Method
¶
Bases: FunctionOrMethod
A python method. A method belongs to some class in some module and must have
a pre-bound self object. The location of the method is encoded in obj
alongside self. If obj is Obj with init_bindings, this method should be
deserializable.
Functions¶
Function
¶
Bases: FunctionOrMethod
A python function. Could be a static method inside a class (not instance of the class).
Functions¶
WithClassInfo
¶
Bases: BaseModel
Mixin to track class information to aid in querying serialized components without having to load them.
Attributes¶
tru_class_info
instance-attribute
¶
tru_class_info: Class
Class information of this pydantic object for use in deserialization.
Using this odd key to not pollute attribute names in whatever class we mix this into. Should be the same as CLASS_INFO.
Functions¶
load
staticmethod
¶
load(obj, *args, **kwargs)
Deserialize/load this object using the class information in tru_class_info to lookup the actual class that will do the deserialization.
model_validate
classmethod
¶
model_validate(*args, **kwargs) -> Any
Deserialized a jsonized version of the app into the instance of the class it was serialized from.
Note
This process uses extra information stored in the jsonized object and handled by WithClassInfo.
Functions¶
is_noserio
¶
Determines whether the given json object represents some non-serializable
object. See noserio
.
noserio
¶
Create a json structure to represent a non-serializable object. Any additional keyword arguments are included.
safe_getattr
¶
Try to get the attribute k
of the given object. This may evaluate some
code if the attribute is a property and may fail. In that case, an dict
indicating so is returned.
If get_prop
is False, will not return contents of properties (will raise
ValueException
).
clean_attributes
¶
Determine which attributes of the given object should be enumerated for storage and/or display in UI. Returns a dict of those attributes and their values.
For enumerating contents of objects that do not support utility classes like pydantic, we use this method to guess what should be enumerated when serializing/displaying.
If include_props
is True, will produce attributes which are properties;
otherwise those will be excluded.