caikit.core.data_model.dataobject

This module defines the @schema decorator which can be used to declare data model objects inline without manually defining the protobufs representation

Attributes

log

error

DATAOBJECT_PY_TO_PROTO_TYPES

CAIKIT_DATA_MODEL

_AUTO_GEN_PROTO_CLASSES

_DataObjectBaseT

Classes

_DataObjectBaseMetaClass

This metaclass is used for the DataObject base class so that all data

DataObjectBase

A DataObject is a data model class that is backed by a @dataclass.

_DataobjectConverter

Augment the dataclass converter to be able to pull descriptors from

Functions

dataobject(→ Callable[[_DataObjectBaseT], ...)

The @dataobject decorator can be used to define a Data Model object's

get_generated_proto_classes()

Provide get access to the auto-gen classes

render_dataobject_protos(interfaces_dir)

Write out protobufs files for all proto classes generated from dataobjects

make_dataobject(→ _DataObjectBaseMetaClass)

Factory function for creating net-new dataobject classes

_dataobject_to_proto(*args, **kwargs)

_get_all_enums(...)

Given a generated proto class, recursively extract all enums

_make_data_model_class(proto_class, cls)

_make_oneof_init(cls)

Helper to augment a defaulted dataclass __init__ to support kwargs for

_has_dataclass_init(→ bool)

When the dataclass decorator adds an __init__ to a class, it adds

Module Contents

caikit.core.data_model.dataobject.log[source]
caikit.core.data_model.dataobject.error
caikit.core.data_model.dataobject.DATAOBJECT_PY_TO_PROTO_TYPES
caikit.core.data_model.dataobject.CAIKIT_DATA_MODEL = 'caikit_data_model'
caikit.core.data_model.dataobject._AUTO_GEN_PROTO_CLASSES = []
class caikit.core.data_model.dataobject._DataObjectBaseMetaClass[source]

Bases: caikit.core.data_model.base._DataBaseMetaClass

This metaclass is used for the DataObject base class so that all data objects can delay the creation of their proto class until after the metaclass has been instantiated.

class caikit.core.data_model.dataobject.DataObjectBase[source]

Bases: caikit.core.data_model.base.DataBase

A DataObject is a data model class that is backed by a @dataclass.

Data model classes that use the @dataobject decorator must derive from this base class.

caikit.core.data_model.dataobject._DataObjectBaseT
caikit.core.data_model.dataobject.dataobject(*args, **kwargs) Callable[[_DataObjectBaseT], _DataObjectBaseT][source]

The @dataobject decorator can be used to define a Data Model object’s schema inline with the definition of the python class rather than needing to bind to a pre-compiled protobufs class. For example:

@dataobject(“foo.bar”) class MyDataObject(DataObjectBase):

‘’’My Custom Data Object’’’ foo: str bar: int

NOTE: The wrapped class must NOT inherit directly from DataBase. That

inheritance will be added by this decorator, but if it is written directly, the metaclass that links protobufs to the class will be called before this decorator can auto-gen the protobufs class.

The dataobject decorator will not provide tools with enough information to perform type completion for constructions in an IDE, or static typechecking. In order to have that, the dataclass decorator may optionally be added, with the slight overhead of wasted effort in creating the “standard” __init__ function which then gets re-done by @dataobject. The dataclass must follow the dataobject decorator. For example:

@dataobject(“foo.bar”) @dataclass class MyDataObject(DataObjectBase):

‘’’My Custom Data Object’’’ foo: str bar: int

Kwargs:
package: str

The package name to use for the generated protobufs class

Returns:
decorator: Callable[[Type], Type[DataBase]]

The decorator function that will wrap the given class

caikit.core.data_model.dataobject.get_generated_proto_classes()[source]

Provide get access to the auto-gen classes

caikit.core.data_model.dataobject.render_dataobject_protos(interfaces_dir: str)[source]

Write out protobufs files for all proto classes generated from dataobjects to the target interfaces directory

Args:

interfaces_dir (str): The target directory (must already exist)

caikit.core.data_model.dataobject.make_dataobject(*, name: str, annotations: Dict[str, type], bases: Iterable[type] | None = None, attrs: Dict[str, Any] | None = None, proto_name: str | None = None, **kwargs) _DataObjectBaseMetaClass[source]

Factory function for creating net-new dataobject classes

WARNING: This is a power-user feature that should be used with caution since

dynamically generated dataobject classes have portability issues due to the use of global registries.

Kwargs:

name (str): The name of the class to create annotations (Dict[str, type]): The type annotations for the class bases (Optional[Iterable[type]]): Additional base classes beyond

DataObjectBase

attrs (Optional[Dict[str, Any]]): Additional class attributes beyond

__annotations__

proto_name (Optional[str]): Alternate name to use for the name of

protobuf message

Returns:
dataobject_class (_DataObjectBaseMetaClass): Programmatically created

class derived from DataObjectBase with the given name and annotations

caikit.core.data_model.dataobject._dataobject_to_proto(*args, **kwargs)[source]
class caikit.core.data_model.dataobject._DataobjectConverter(dataclass_: type, package: str, *, name: str | None = None, type_mapping: Dict[str, int | google.protobuf.descriptor.Descriptor] | None = None, validate: bool = False, descriptor_pool: google.protobuf.descriptor_pool.DescriptorPool | None = None)[source]

Bases: py_to_proto.dataclass_to_proto.DataclassConverter

Augment the dataclass converter to be able to pull descriptors from existing data objects

get_concrete_type(entry: Any) Any[source]

Also include data model classes and enums as concrete types

get_descriptor(entry: Any) Any[source]

Unpack data model classes and enums to their descriptors

get_optional_field_names(entry: Any) List[str][source]

Get the names of any fields which are optional. This will be any field that has a user-defined default or is marked as Optional[]

static _is_python_optional(entry: Any) Any[source]

Detect if this type is a python optional

caikit.core.data_model.dataobject._get_all_enums(proto_class: google.protobuf.message.Message | google.protobuf.internal.enum_type_wrapper.EnumTypeWrapper) List[google.protobuf.internal.enum_type_wrapper.EnumTypeWrapper][source]

Given a generated proto class, recursively extract all enums

caikit.core.data_model.dataobject._make_data_model_class(proto_class: Type[google.protobuf.message.Message], cls)[source]
caikit.core.data_model.dataobject._make_oneof_init(cls)[source]

Helper to augment a defaulted dataclass __init__ to support kwargs for oneof fields

caikit.core.data_model.dataobject._has_dataclass_init(cls) bool[source]

When the dataclass decorator adds an __init__ to a class, it adds __annotations__ to the init function itself. This function uses that fact to detect if the class’s __init__ function was generated by @dataclass