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
Classes
This metaclass is used for the DataObject base class so that all data |
|
A DataObject is a data model class that is backed by a @dataclass. |
|
Augment the dataclass converter to be able to pull descriptors from |
Functions
|
The @dataobject decorator can be used to define a Data Model object's |
Provide get access to the auto-gen classes |
|
|
Write out protobufs files for all proto classes generated from dataobjects |
|
Factory function for creating net-new dataobject classes |
|
|
|
Given a generated proto class, recursively extract all enums |
|
|
|
Helper to augment a defaulted dataclass __init__ to support kwargs for |
|
When the dataclass decorator adds an __init__ to a class, it adds |
Module Contents
- 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._DataBaseMetaClassThis 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.DataBaseA 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
- 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.DataclassConverterAugment 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
- 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]