From 9226633c412835b1e8b445287fd2a9ef99343676 Mon Sep 17 00:00:00 2001 From: Johannes Messner Date: Fri, 26 Aug 2022 13:54:53 +0200 Subject: [PATCH 1/3] fix(plot): be robust against non-existing subindices --- docarray/array/mixins/plot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docarray/array/mixins/plot.py b/docarray/array/mixins/plot.py index b9fe8909d15..86c62d50498 100644 --- a/docarray/array/mixins/plot.py +++ b/docarray/array/mixins/plot.py @@ -79,7 +79,7 @@ def summary(self): is_multimodal = all(d.is_multimodal for d in self) table.add_row('Multimodal dataclass', str(is_multimodal)) - if getattr(self, '_subindices'): + if getattr(self, '_subindices', None): table.add_row( 'Subindices', rich.markup.escape(str(tuple(self._subindices.keys()))) ) From 62c4fd10b3db7d55ac0f529d9775db9667757c7a Mon Sep 17 00:00:00 2001 From: Johannes Messner Date: Wed, 9 Nov 2022 10:57:18 +0100 Subject: [PATCH 2/3] feat: propagate context manager enter and exit to subindices --- docarray/array/document.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/docarray/array/document.py b/docarray/array/document.py index 688ea90228b..7c535b25330 100644 --- a/docarray/array/document.py +++ b/docarray/array/document.py @@ -1,3 +1,4 @@ +from contextlib import ExitStack from typing import Optional, overload, TYPE_CHECKING, Dict, Union from docarray.array.base import BaseDocumentArray @@ -140,13 +141,18 @@ def __new__( ... def __enter__(self): + self._exit_stack = ExitStack() + # Ensure that we sync the data to the storage backend when exiting the context manager + self._exit_stack.callback(self.sync) + # Enter (and then exit) context of all subindices + if getattr(self, '_subindices', None): + for selector, da in self._subindices.items(): + self._exit_stack.enter_context(da) return self def __exit__(self, *args, **kwargs): - """ - Ensures that we sync the data to the storage backend when exiting the context manager - """ - self.sync() + # Trigger all __exit__()s and callbacks added in self.__enter__() + self._exit_stack.close() def __new__(cls, *args, storage: str = 'memory', **kwargs): if cls is DocumentArray: From 0320860d8b07badcd27f19cfd2f109a8ca848406 Mon Sep 17 00:00:00 2001 From: Johannes Messner Date: Wed, 9 Nov 2022 11:00:16 +0100 Subject: [PATCH 3/3] fix: remove sync propagation logic --- docarray/array/storage/base/getsetdel.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/docarray/array/storage/base/getsetdel.py b/docarray/array/storage/base/getsetdel.py index ed9db6558a6..2b29e81d7ab 100644 --- a/docarray/array/storage/base/getsetdel.py +++ b/docarray/array/storage/base/getsetdel.py @@ -326,7 +326,3 @@ def _save_offset2ids(self): def sync(self): if hasattr(self, '_offset2ids'): self._save_offset2ids() - - if getattr(self, '_subindices', None): - for selector, da in self._subindices.items(): - da.sync()