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
2 changes: 1 addition & 1 deletion docarray/document/mixins/multimodal.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MultiModalMixin:
@classmethod
def from_dataclass(cls, obj):
if not is_dataclass(obj):
raise ValueError(f'Object {obj.__name__} is not a dataclass instance')
raise ValueError(f'Object {type(obj).__name__} is not a dataclass instance')

from docarray import Document

Expand Down
4 changes: 2 additions & 2 deletions docarray/types/deserializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def image_deserializer(attribute_name, doc: 'Document'):
if doc._metadata['image_type'] == 'uri':
return doc._metadata['image_uri']
elif doc._metadata['image_type'] == 'PIL':
from PIL.Image import Image
from PIL import Image

return Image.fromarray(doc.tensor)
elif doc._metadata['image_type'] == 'ndarray':
Expand All @@ -24,7 +24,7 @@ def text_deserializer(attribute_name, doc: 'Document'):


def audio_deserializer(attribute_name, doc: 'Document'):
from PIL.Image import Image
from PIL import Image

return Image.fromarray(doc.tensor)

Expand Down
65 changes: 65 additions & 0 deletions tests/unit/document/test_deserializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import json
import os

import numpy as np
from PIL import Image

from docarray import Document
from docarray.types.deserializers import (
audio_deserializer,
image_deserializer,
json_deserializer,
text_deserializer,
)

cur_dir = os.path.dirname(os.path.abspath(__file__))


IMAGE_URI = os.path.join(cur_dir, 'toydata/test.png')


def test_image_deserializer():
doc = Document()
doc._metadata['image_type'] = 'uri'
doc._metadata['image_uri'] = 'image_uri'

assert image_deserializer('attribute_name', doc) == 'image_uri'

doc._metadata['image_type'] = 'ndarray'
im = Image.open(IMAGE_URI)
doc.tensor = np.asarray(im)

assert np.all(image_deserializer('attribute_name', doc) == np.asarray(im))

doc._metadata['image_type'] = 'PIL'

assert np.all(
image_deserializer('attribute_name', doc) == Image.fromarray(np.asarray(im))
)


def test_text_deserializer():
doc = Document(text='text')

assert text_deserializer('attriute_name', doc) == 'text'


def test_json_deserializer():
doc = Document()
doc._metadata['json_type'] = ''
doc.tags['attribute_name'] = 'attribute'
assert json_deserializer('attribute_name', doc) == 'attribute'

doc._metadata['json_type'] = 'str'

assert json_deserializer('attribute_name', doc) == json.dumps('attribute')


def test_audio_deserializer():
doc = Document()
ad = Image.open(IMAGE_URI)
doc.tensor = np.asarray(ad)

assert np.all(
audio_deserializer('attribute_name', doc) == Image.fromarray(np.asarray(ad))
)
25 changes: 25 additions & 0 deletions tests/unit/document/test_multi_modal.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,28 @@ class MMDocument:
np.array(obj.pickled_image).shape
== np.array(translated_obj.pickled_image).shape
)


def test_invalid_type_annotations():
@dataclass
class MMDocument:
attr: List

inp = ['something']
obj = MMDocument(attr=inp)
with pytest.raises(Exception) as exc_info:
doc = Document.from_dataclass(obj)
assert exc_info.value.args[0] == 'Unsupported type annotation'
assert str(exc_info.value) == 'Unsupported type annotation'


def test_not_data_class():
class MMDocument:
pass

obj = MMDocument()

with pytest.raises(Exception) as exc_info:
doc = Document.from_dataclass(obj)
assert exc_info.value.args[0] == 'Object MMDocument is not a dataclass instance'
assert str(exc_info.value) == 'Object MMDocument is not a dataclass instance'