docarray.dataclasses.types module#

docarray.dataclasses.types.field(*, _source_field: Optional[Field] = None, setter: Optional[Callable] = None, getter: Optional[Callable] = None, default=MISSING, default_factory=MISSING, init=True, repr=True, hash=None, compare=True, metadata=None) Field[source]#

Creates new multimodal type for a DocArray dataclass.

field() is used to define the get and set behaviour of custom types when used in a DocArray dataclass.

from docarray import Document, dataclass, field
from typing import TypeVar

MyImage = TypeVar('MyImage', bound=str)


def my_setter(value) -> 'Document':
    return Document(uri=value).load_uri_to_blob()


def my_getter(doc: 'Document'):
    return doc.uri


@dataclass
class MMDoc:
    banner: MyImage = field(setter=my_setter, getter=my_getter, default='test-1.jpeg')
Return type:

Field

docarray.dataclasses.types.dataclass(cls: Optional[T] = None, *, init: bool = True, repr: bool = True, eq: bool = True, order: bool = False, unsafe_hash: bool = False, frozen: bool = False, type_var_map: Optional[Dict[TypeVar, Callable[[_Field], Field]]] = None) T[source]#

Annotates a class as a DocArray dataclass type.

Example usage:

>>> from docarray.typing import Image, Text
>>> from docarray import dataclass
>>> @dataclass:
>>> class X:
>>>     banner: Image = 'apple.png'
>>>     description: Text = 'This is a big red apple.'
Parameters:

type_var_map (Optional[Dict[TypeVar(TypeVar, bound= <member ‘__bound__’ of ‘TypeVar’ objects>, covariant=<member ‘__covariant__’ of ‘TypeVar’ objects>, contravariant=<member ‘__contravariant__’ of ‘TypeVar’ objects>), Callable[[Field], Field]]]) –

a mapping from TypeVar to a callable that gives Field.

_TYPES_REGISTRY = {
    Image: lambda x: field(setter=image_setter, getter=image_getter, _source_field=x),
    Text: lambda x: field(setter=text_setter, getter=text_getter, _source_field=x),
}

The default mapping will be overrided by this new mapping if they collide on the keys.

Return type:

T

docarray.dataclasses.types.is_multimodal(obj)[source]#

Returns True if obj is an instance of dataclass().

Return type:

bool