Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions docarray/document/mixins/multimodal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import typing

from docarray.dataclasses.types import (
Field,
is_multimodal,
_is_field,
AttributeTypeError,
Expand Down Expand Up @@ -104,7 +103,6 @@ def _from_dataclass(cls, obj) -> 'Document':
# TODO: may have to modify this?
root.tags = tags
root._metadata[DocumentMetadata.MULTI_MODAL_SCHEMA] = multi_modal_schema

return root

def _get_mm_attr_postion(self, attr):
Expand Down Expand Up @@ -208,7 +206,17 @@ def _has_multimodal_attr(self, attr):
def __getattr__(self, attr):
if self._has_multimodal_attr(attr):
mm_attr_da = self.get_multi_modal_attribute(attr)
return mm_attr_da if len(mm_attr_da) > 1 else mm_attr_da[0]
attr_type = self._metadata[DocumentMetadata.MULTI_MODAL_SCHEMA][attr][
'attribute_type'
]
if attr_type in [
AttributeType.ITERABLE_DOCUMENT,
AttributeType.ITERABLE_NESTED,
AttributeType.ITERABLE_PRIMITIVE,
]:
return mm_attr_da
else:
return mm_attr_da[0]
else:
raise AttributeError(f'{self.__class__.__name__} has no attribute `{attr}`')

Expand Down
22 changes: 22 additions & 0 deletions tests/unit/document/test_multi_modal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest

from docarray import Document, DocumentArray
from docarray.array.chunk import ChunkArray
from docarray.dataclasses import dataclass, field
from docarray.typing import Image, Text, Audio, Video, Mesh, Tabular, Blob, JSON
from docarray.dataclasses.getter import image_getter
Expand Down Expand Up @@ -911,3 +912,24 @@ class A:
text: List[Text]

doc = Document(A(text=[]))


def test_doc_with_dataclass_with_list_of_length_one():
@dataclass
class MyDoc:
title: Text
images: List[Image]

doc = Document(MyDoc(title='doc 1', images=[IMAGE_URI]))
assert type(doc.images) == ChunkArray
assert len(doc.images) == 1


def test_doc_with_dataclass_without_list():
@dataclass
class MyDoc:
title: Text
image: Image

doc = Document(MyDoc(title='doc 1', image=IMAGE_URI))
assert type(doc.image) == Document