Skip to content

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.

Functions
__rich_repr__
__rich_repr__() -> Result

Requirement for pretty printing using the rich package.

base_class
base_class() -> Class

Get the deepest base class in the same module as this class.

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.

Functions
__rich_repr__
__rich_repr__() -> Result

Requirement for pretty printing using the rich package.

Bindings

Bases: SerialModel

Functions
__rich_repr__
__rich_repr__() -> Result

Requirement for pretty printing using the rich package.

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: BoundArguments

skip_self

If True, skip the first argument if it is named "self".

TYPE: bool DEFAULT: True

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: bool DEFAULT: False

FunctionOrMethod

Bases: SerialModel

Functions
__rich_repr__
__rich_repr__() -> Result

Requirement for pretty printing using the rich package.

of_callable staticmethod
of_callable(
    c: Callable, loadable: bool = False
) -> "FunctionOrMethod"

Serialize the given callable. If loadable is set, tries to add enough info for the callable to be deserialized.

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
__rich_repr__
__rich_repr__() -> Result

Requirement for pretty printing using the rich package.

of_callable staticmethod
of_callable(
    c: Callable, loadable: bool = False
) -> "FunctionOrMethod"

Serialize the given callable. If loadable is set, tries to add enough info for the callable to be deserialized.

Function

Bases: FunctionOrMethod

A python function. Could be a static method inside a class (not instance of the class).

Functions
__rich_repr__
__rich_repr__() -> Result

Requirement for pretty printing using the rich package.

of_callable staticmethod
of_callable(
    c: Callable, loadable: bool = False
) -> "FunctionOrMethod"

Serialize the given callable. If loadable is set, tries to add enough info for the callable to be deserialized.

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

is_noserio(obj: Any) -> bool

Determines whether the given json object represents some non-serializable object. See noserio.

noserio

noserio(obj: Any, **extra: Dict) -> Dict

Create a json structure to represent a non-serializable object. Any additional keyword arguments are included.

safe_getattr

safe_getattr(
    obj: Any, k: str, get_prop: bool = True
) -> Any

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

clean_attributes(
    obj, include_props: bool = False
) -> Dict[str, Any]

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.