Skip to content

Commit

Permalink
fix: single doc set in docarray (#42)
Browse files Browse the repository at this point in the history
Co-authored-by: Han Xiao <han.xiao@jina.ai>
  • Loading branch information
davidbp and hanxiao committed Jan 13, 2022
1 parent edf836d commit 97725ed
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
8 changes: 6 additions & 2 deletions docarray/array/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def __setitem__(
index: 'DocumentArrayIndexType',
value: Union['Document', Sequence['Document']],
):

if isinstance(index, (int, np.generic)) and not isinstance(index, bool):
index = int(index)
self._data[index] = value
Expand Down Expand Up @@ -274,8 +275,11 @@ def __setitem__(
elif _a == 'embedding':
_docs.embeddings = _v
else:
for _d, _vv in zip(_docs, _v):
setattr(_d, _a, _vv)
if len(_docs) == 1:
setattr(_docs[0], _a, _v)
else:
for _d, _vv in zip(_docs, _v):
setattr(_d, _a, _vv)
elif isinstance(index[0], bool):
if len(index) != len(self._data):
raise IndexError(
Expand Down
9 changes: 7 additions & 2 deletions docarray/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,13 @@
DocumentArrayMultipleIndexType = Union[
slice, Sequence[int], Sequence[str], Sequence[bool], Ellipsis
]
DocumentArraySingleAttributeType = Tuple[slice, str]
DocumentArrayMultipleAttributeType = Tuple[slice, Sequence[str]]
DocumentArraySingleAttributeType = Tuple[
Union[DocumentArraySingletonIndexType, DocumentArrayMultipleIndexType], str
]
DocumentArrayMultipleAttributeType = Tuple[
Union[DocumentArraySingletonIndexType, DocumentArrayMultipleIndexType],
Sequence[str],
]
DocumentArrayIndexType = Union[
DocumentArraySingletonIndexType,
DocumentArrayMultipleIndexType,
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/array/mixins/test_getset.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ def test_texts_getter_da(da):
assert not da.texts


@pytest.mark.parametrize('da', da_and_dam())
def test_setter_by_sequences_in_selected_docs_da(da):

da[[0], 'text'] = 'jina'
assert ['jina'] == da[[0], 'text']

da[[0, 1], 'text'] = ['jina', 'jana']
assert ['jina', 'jana'] == da[[0, 1], 'text']

da[[0], 'id'] = '12'
assert ['12'] == da[[0], 'id']

da[[0, 1], 'id'] = ['12', '34']
assert ['12', '34'] == da[[0, 1], 'id']


@pytest.mark.parametrize('da', da_and_dam())
def test_texts_wrong_len(da):
texts = ['hello']
Expand Down

0 comments on commit 97725ed

Please sign in to comment.