From 20fa10e92c2eba193979b5f657e5c425e18ef502 Mon Sep 17 00:00:00 2001 From: Joan Fontanals Martinez Date: Fri, 5 May 2023 09:50:21 +0200 Subject: [PATCH 1/3] fix: fix extend with itself infinite recursion Signed-off-by: Joan Fontanals Martinez --- docarray/array/doc_list/doc_list.py | 8 ++++++-- tests/units/array/test_array.py | 13 ++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/docarray/array/doc_list/doc_list.py b/docarray/array/doc_list/doc_list.py index 132cdcb9f66..e26063ab13f 100644 --- a/docarray/array/doc_list/doc_list.py +++ b/docarray/array/doc_list/doc_list.py @@ -164,7 +164,7 @@ def append(self, doc: T_doc): as the `.doc_type` of this `DocList` otherwise it will fail. :param doc: A Document """ - super().append(self._validate_one_doc(doc)) + return super().append(self._validate_one_doc(doc)) def extend(self, docs: Iterable[T_doc]): """ @@ -173,7 +173,11 @@ def extend(self, docs: Iterable[T_doc]): fail. :param docs: Iterable of Documents """ - super().extend(self._validate_docs(docs)) + it = self._validate_docs(docs) + if self is docs: + # see https://github.com/docarray/docarray/issues/1489 + it = list(it) + return super().extend(it) def insert(self, i: SupportsIndex, doc: T_doc): """ diff --git a/tests/units/array/test_array.py b/tests/units/array/test_array.py index 316baa26d38..2c554493264 100644 --- a/tests/units/array/test_array.py +++ b/tests/units/array/test_array.py @@ -53,6 +53,17 @@ class Text(BaseDoc): assert da.id == str(i) +def test_extend_itself(): + class Text(BaseDoc): + text: str + + da = DocList[Text]([Text(text='hello', id=str(i)) for i in range(10)]) + + da.extend(da) + + assert len(da) == 20 + + def test_slice(da): da2 = da[0:5] assert type(da2) == da.__class__ @@ -331,7 +342,7 @@ class Doc(BaseDoc): texts = da_sliced.text assert len(texts) == 5 for i, text in enumerate(texts): - assert text == f'hello{i*2}' + assert text == f'hello{i * 2}' def test_del_item(da): From ab4629e6df987925a504598930e8f636c3e2e58b Mon Sep 17 00:00:00 2001 From: Joan Fontanals Date: Mon, 8 May 2023 09:41:25 +0200 Subject: [PATCH 2/3] refactor: update docarray/array/doc_list/doc_list.py Co-authored-by: samsja <55492238+samsja@users.noreply.github.com> Signed-off-by: Joan Fontanals --- docarray/array/doc_list/doc_list.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docarray/array/doc_list/doc_list.py b/docarray/array/doc_list/doc_list.py index c30ed728663..d81b029620e 100644 --- a/docarray/array/doc_list/doc_list.py +++ b/docarray/array/doc_list/doc_list.py @@ -180,10 +180,12 @@ def extend(self, docs: Iterable[T_doc]): fail. :param docs: Iterable of Documents """ - it = self._validate_docs(docs) if self is docs: # see https://github.com/docarray/docarray/issues/1489 it = list(it) + else: + it = self._validate_docs(docs) + return super().extend(it) def insert(self, i: SupportsIndex, doc: T_doc): From 431c77cd70b920befd5366b7ee46ca93e7875875 Mon Sep 17 00:00:00 2001 From: Joan Fontanals Martinez Date: Mon, 8 May 2023 10:06:40 +0200 Subject: [PATCH 3/3] fix: fix ruff and mypy Signed-off-by: Joan Fontanals Martinez --- docarray/array/doc_list/doc_list.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docarray/array/doc_list/doc_list.py b/docarray/array/doc_list/doc_list.py index d81b029620e..c88f68903b2 100644 --- a/docarray/array/doc_list/doc_list.py +++ b/docarray/array/doc_list/doc_list.py @@ -180,12 +180,13 @@ def extend(self, docs: Iterable[T_doc]): fail. :param docs: Iterable of Documents """ + it: Iterable[T_doc] = list() if self is docs: # see https://github.com/docarray/docarray/issues/1489 - it = list(it) + it = list(docs) else: it = self._validate_docs(docs) - + return super().extend(it) def insert(self, i: SupportsIndex, doc: T_doc):