diff --git a/docarray/array/doc_list/doc_list.py b/docarray/array/doc_list/doc_list.py index 07d3d37fedd..c88f68903b2 100644 --- a/docarray/array/doc_list/doc_list.py +++ b/docarray/array/doc_list/doc_list.py @@ -171,7 +171,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]): """ @@ -180,7 +180,14 @@ def extend(self, docs: Iterable[T_doc]): fail. :param docs: Iterable of Documents """ - super().extend(self._validate_docs(docs)) + it: Iterable[T_doc] = list() + if self is docs: + # see https://github.com/docarray/docarray/issues/1489 + it = list(docs) + else: + it = self._validate_docs(docs) + + 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 33344bd2aaa..b72a42292a4 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):