Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4a1bc26

Browse filesBrowse files
authored
fix: allow nested model dump via docvec (#1808)
Signed-off-by: samsja <sami.jaghouar@hotmail.fr>
1 parent 26d776d commit 4a1bc26
Copy full SHA for 4a1bc26

File tree

Expand file treeCollapse file tree

2 files changed

+17
-5
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+17
-5
lines changed

‎docarray/base_doc/doc.py

Copy file name to clipboardExpand all lines: docarray/base_doc/doc.py
+17-4Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,6 @@ def dict(
419419
which fields to include or exclude.
420420
421421
"""
422-
423422
exclude, original_exclude, doclist_exclude_fields = self._exclude_doclist(
424423
exclude=exclude
425424
)
@@ -447,6 +446,20 @@ def dict(
447446

448447
else:
449448

449+
def _copy_view_pydantic_v2(self: T) -> T:
450+
"""
451+
perform a deep copy, the new doc has its own data
452+
"""
453+
data = {}
454+
for key, value in self.__dict__.to_dict().items():
455+
if isinstance(value, BaseDocWithoutId):
456+
data[key] = value._copy_view_pydantic_v2()
457+
else:
458+
data[key] = value
459+
460+
doc = self.__class__.model_construct(**data)
461+
return doc
462+
450463
def model_dump( # type: ignore
451464
self,
452465
*,
@@ -460,15 +473,15 @@ def model_dump( # type: ignore
460473
round_trip: bool = False,
461474
warnings: bool = True,
462475
) -> Dict[str, Any]:
463-
def _model_dump(cls):
476+
def _model_dump(doc):
464477

465478
(
466479
exclude_,
467480
original_exclude,
468481
doclist_exclude_fields,
469482
) = self._exclude_doclist(exclude=exclude)
470483

471-
data = cls.model_dump(
484+
data = doc.model_dump(
472485
mode=mode,
473486
include=include,
474487
exclude=exclude_,
@@ -495,7 +508,7 @@ def _model_dump(cls):
495508
## for some reason use ColumnViewStorage to dump the data is not working with
496509
## pydantic v2, so we need to create a new doc and dump it
497510

498-
new_doc = self.__class__.model_construct(**self.__dict__.to_dict())
511+
new_doc = self._copy_view_pydantic_v2()
499512
return _model_dump(new_doc)
500513
else:
501514
return _model_dump(super())

‎tests/units/array/test_array_from_to_pandas.py

Copy file name to clipboardExpand all lines: tests/units/array/test_array_from_to_pandas.py
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class MyDocNested(MyDoc):
2222
return MyDocNested
2323

2424

25-
@pytest.mark.skipif(is_pydantic_v2, reason="Not working with pydantic v2")
2625
@pytest.mark.parametrize('doc_vec', [False, True])
2726
def test_to_from_pandas_df(nested_doc_cls, doc_vec):
2827
da = DocList[nested_doc_cls](

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.