caikit.core.data_model.dataobject ================================= .. py:module:: caikit.core.data_model.dataobject .. autoapi-nested-parse:: This module defines the @schema decorator which can be used to declare data model objects inline without manually defining the protobufs representation Attributes ---------- .. autoapisummary:: caikit.core.data_model.dataobject.log 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.core.data_model.dataobject._AUTO_GEN_PROTO_CLASSES caikit.core.data_model.dataobject._DataObjectBaseT Classes ------- .. autoapisummary:: caikit.core.data_model.dataobject._DataObjectBaseMetaClass caikit.core.data_model.dataobject.DataObjectBase caikit.core.data_model.dataobject._DataobjectConverter Functions --------- .. autoapisummary:: caikit.core.data_model.dataobject.dataobject caikit.core.data_model.dataobject.get_generated_proto_classes caikit.core.data_model.dataobject.render_dataobject_protos caikit.core.data_model.dataobject.make_dataobject caikit.core.data_model.dataobject._dataobject_to_proto caikit.core.data_model.dataobject._get_all_enums caikit.core.data_model.dataobject._make_data_model_class caikit.core.data_model.dataobject._make_oneof_init caikit.core.data_model.dataobject._has_dataclass_init Module Contents --------------- .. py:data:: log .. py:data:: error .. py:data:: DATAOBJECT_PY_TO_PROTO_TYPES .. py:data:: CAIKIT_DATA_MODEL :value: 'caikit_data_model' .. py:data:: _AUTO_GEN_PROTO_CLASSES :value: [] .. py:class:: _DataObjectBaseMetaClass Bases: :py:obj:`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. .. py:class:: DataObjectBase Bases: :py:obj:`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. .. py:data:: _DataObjectBaseT .. py:function:: dataobject(*args, **kwargs) -> Callable[[_DataObjectBaseT], _DataObjectBaseT] 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 .. py:function:: get_generated_proto_classes() Provide get access to the auto-gen classes .. py:function:: render_dataobject_protos(interfaces_dir: str) 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) .. py:function:: make_dataobject(*, name: str, annotations: Dict[str, type], bases: Optional[Iterable[type]] = None, attrs: Optional[Dict[str, Any]] = None, proto_name: Optional[str] = None, **kwargs) -> _DataObjectBaseMetaClass 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 .. py:function:: _dataobject_to_proto(*args, **kwargs) .. py:class:: _DataobjectConverter(dataclass_: type, package: str, *, name: Optional[str] = None, type_mapping: Optional[Dict[str, Union[int, google.protobuf.descriptor.Descriptor]]] = None, validate: bool = False, descriptor_pool: Optional[google.protobuf.descriptor_pool.DescriptorPool] = None) Bases: :py:obj:`py_to_proto.dataclass_to_proto.DataclassConverter` Augment the dataclass converter to be able to pull descriptors from existing data objects .. py:method:: get_concrete_type(entry: Any) -> Any Also include data model classes and enums as concrete types .. py:method:: get_descriptor(entry: Any) -> Any Unpack data model classes and enums to their descriptors .. py:method:: get_optional_field_names(entry: Any) -> List[str] 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[] .. py:method:: _is_python_optional(entry: Any) -> Any :staticmethod: Detect if this type is a python optional .. py:function:: _get_all_enums(proto_class: Union[google.protobuf.message.Message, google.protobuf.internal.enum_type_wrapper.EnumTypeWrapper]) -> List[google.protobuf.internal.enum_type_wrapper.EnumTypeWrapper] Given a generated proto class, recursively extract all enums .. py:function:: _make_data_model_class(proto_class: Type[google.protobuf.message.Message], cls) .. py:function:: _make_oneof_init(cls) Helper to augment a defaulted dataclass __init__ to support kwargs for oneof fields .. py:function:: _has_dataclass_init(cls) -> bool 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