Skip to content

Commit

Permalink
feat: no return in ChunkArray/MatchArray append (#3974)
Browse files Browse the repository at this point in the history
  • Loading branch information
alaeddine-13 committed Nov 23, 2021
1 parent b8f1d5d commit fec0d42
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 28 deletions.
12 changes: 4 additions & 8 deletions jina/types/arrays/chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,19 @@ def __init__(self, doc_views, reference_doc: 'Document'):
self._ref_doc = reference_doc
super().__init__(doc_views)

def append(self, document: 'Document') -> 'Document':
def append(self, document: 'Document'):
"""Add a sub-document (i.e chunk) to the current Document.
:param document: Sub-document to be appended
:return: the newly added sub-document in :class:`Document` view
:rtype: :class:`Document`
.. note::
Comparing to :attr:`DocumentArray.append()`, this method adds more safeguard to
make sure the added chunk is legit.
"""
super().append(document)
chunk = self[-1]
chunk.parent_id = self._ref_doc.id
chunk.granularity = self.granularity

return chunk
proto = self._pb_body[-1]
proto.parent_id = self._ref_doc._pb_body.id
proto.granularity = self._ref_doc._pb_body.granularity + 1

def extend(self, iterable: Iterable['Document']) -> None:
"""
Expand Down
12 changes: 4 additions & 8 deletions jina/types/arrays/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,15 @@ def __init__(self, doc_views, reference_doc: 'Document'):
self._ref_doc = reference_doc
super().__init__(doc_views)

def append(self, document: 'Document') -> 'Document':
def append(self, document: 'Document'):
"""Add a matched document to the current Document.
:param document: Sub-document to be added
:return: the newly added sub-document in :class:`Document` view
:rtype: :class:`Document` view
"""
super().append(document)
match = self[-1]
match.granularity = self.granularity
match.adjacency = self.adjacency

return match
proto = self._pb_body[-1]
proto.granularity = self._ref_doc._pb_body.granularity
proto.adjacency = self._ref_doc._pb_body.adjacency + 1

@property
def reference_doc(self) -> 'Document':
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/types/arrays/documentarray/test_chunkarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def chunkarray(chunks, reference_doc):

def test_append_from_documents(chunkarray, document_factory, reference_doc):
chunk = document_factory.create(4, 'test 4')
rv = chunkarray.append(chunk)
chunkarray.append(chunk)
rv = chunkarray[-1]
assert len(chunkarray) == 4
assert chunkarray[-1].text == 'test 4'
assert rv.text == chunk.text
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/types/arrays/documentarray/test_matcharray.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def matcharray(matches, reference_doc):
def test_append_from_documents(matcharray, document_factory, reference_doc):
match = document_factory.create(4, 'test 4')
match.scores['score'] = 10
rv = matcharray.append(match)
matcharray.append(match)
rv = matcharray[-1]
assert len(matcharray) == 4
assert matcharray[-1].text == 'test 4'
assert rv.text == match.text
Expand All @@ -58,8 +59,8 @@ def test_mime_type_not_reassigned():
m = Document()
assert m.mime_type == ''
d.mime_type = 'text/plain'
r = d.matches.append(m)
assert r.mime_type == ''
d.matches.append(m)
assert d.matches[0].mime_type == ''


def test_matches_sort_by_document_interface_in_proto():
Expand Down
10 changes: 2 additions & 8 deletions tests/unit/types/document/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,25 +234,19 @@ def test_doc_setattr():
assert root.adjacency == 0

match = Document(text='def')
m = root.matches.append(match)
root.matches.append(match)

chunk = Document(text='def')
c = root.chunks.append(chunk)
root.chunks.append(chunk)

assert len(root.matches) == 1
assert root.matches[0].granularity == 0
assert root.matches[0].adjacency == 1

assert m.granularity == 0
assert m.adjacency == 1

assert len(root.chunks) == 1
assert root.chunks[0].granularity == 1
assert root.chunks[0].adjacency == 0

assert c.granularity == 1
assert c.adjacency == 0


def test_doc_score():
from jina.types.score import NamedScore
Expand Down

0 comments on commit fec0d42

Please sign in to comment.