From 8247440de5ca528647ccbcf6cee0108783b5bc14 Mon Sep 17 00:00:00 2001 From: samsja Date: Thu, 28 Sep 2023 13:57:42 +0200 Subject: [PATCH 1/3] fix: allow nested model dump via docvec Signed-off-by: samsja --- docarray/base_doc/doc.py | 21 ++++++++++++++++---- docarray/typing/tensor/image/image_tensor.py | 1 + 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/docarray/base_doc/doc.py b/docarray/base_doc/doc.py index c53cd7431e..e5c630622c 100644 --- a/docarray/base_doc/doc.py +++ b/docarray/base_doc/doc.py @@ -419,7 +419,6 @@ def dict( which fields to include or exclude. """ - exclude, original_exclude, doclist_exclude_fields = self._exclude_doclist( exclude=exclude ) @@ -447,6 +446,20 @@ def dict( else: + def _copy_view_pydantic_v2(self: T) -> T: + """ + perform a deep copy, the new doc has its own data + """ + data = {} + for key, value in self.__dict__.to_dict().items(): + if isinstance(value, BaseDocWithoutId): + data[key] = value._copy_view_pydantic_v2() + else: + data[key] = value + + doc = self.__class__.model_construct(**data) + return doc + def model_dump( # type: ignore self, *, @@ -460,7 +473,7 @@ def model_dump( # type: ignore round_trip: bool = False, warnings: bool = True, ) -> Dict[str, Any]: - def _model_dump(cls): + def _model_dump(doc): ( exclude_, @@ -468,7 +481,7 @@ def _model_dump(cls): doclist_exclude_fields, ) = self._exclude_doclist(exclude=exclude) - data = cls.model_dump( + data = doc.model_dump( mode=mode, include=include, exclude=exclude_, @@ -495,7 +508,7 @@ def _model_dump(cls): ## for some reason use ColumnViewStorage to dump the data is not working with ## pydantic v2, so we need to create a new doc and dump it - new_doc = self.__class__.model_construct(**self.__dict__.to_dict()) + new_doc = self._copy_view_pydantic_v2() return _model_dump(new_doc) else: return _model_dump(super()) diff --git a/docarray/typing/tensor/image/image_tensor.py b/docarray/typing/tensor/image/image_tensor.py index b8920f7dba..d0079755ad 100644 --- a/docarray/typing/tensor/image/image_tensor.py +++ b/docarray/typing/tensor/image/image_tensor.py @@ -103,6 +103,7 @@ def _docarray_validate( return ImageNdArray._docarray_validate(value) except Exception: # noqa pass + breakpoint() raise TypeError( f"Expected one of [torch.Tensor, tensorflow.Tensor, numpy.ndarray] " f"compatible type, got {type(value)}" From a49d91acd0d1d816a8075cc80e3ba886b4784f85 Mon Sep 17 00:00:00 2001 From: samsja Date: Thu, 28 Sep 2023 15:16:25 +0200 Subject: [PATCH 2/3] fix: remove breakpont Signed-off-by: samsja --- docarray/typing/tensor/image/image_tensor.py | 1 - 1 file changed, 1 deletion(-) diff --git a/docarray/typing/tensor/image/image_tensor.py b/docarray/typing/tensor/image/image_tensor.py index d0079755ad..b8920f7dba 100644 --- a/docarray/typing/tensor/image/image_tensor.py +++ b/docarray/typing/tensor/image/image_tensor.py @@ -103,7 +103,6 @@ def _docarray_validate( return ImageNdArray._docarray_validate(value) except Exception: # noqa pass - breakpoint() raise TypeError( f"Expected one of [torch.Tensor, tensorflow.Tensor, numpy.ndarray] " f"compatible type, got {type(value)}" From f18ee7685f0a926160f65e9c0df3f5dccbc8f843 Mon Sep 17 00:00:00 2001 From: samsja Date: Thu, 28 Sep 2023 15:32:24 +0200 Subject: [PATCH 3/3] fix: remove breakpont Signed-off-by: samsja --- tests/units/array/test_array_from_to_pandas.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/units/array/test_array_from_to_pandas.py b/tests/units/array/test_array_from_to_pandas.py index 0d14151062..68917f48a7 100644 --- a/tests/units/array/test_array_from_to_pandas.py +++ b/tests/units/array/test_array_from_to_pandas.py @@ -22,7 +22,6 @@ class MyDocNested(MyDoc): return MyDocNested -@pytest.mark.skipif(is_pydantic_v2, reason="Not working with pydantic v2") @pytest.mark.parametrize('doc_vec', [False, True]) def test_to_from_pandas_df(nested_doc_cls, doc_vec): da = DocList[nested_doc_cls](