From f3ab971bbf4c094985049131b283bc8d81d25cc5 Mon Sep 17 00:00:00 2001 From: samsja Date: Mon, 8 May 2023 17:07:46 +0200 Subject: [PATCH 1/3] fix: json and dict when calling empty doclist Signed-off-by: samsja --- docarray/base_doc/doc.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docarray/base_doc/doc.py b/docarray/base_doc/doc.py index 04a74c8c312..9b24b384c1e 100644 --- a/docarray/base_doc/doc.py +++ b/docarray/base_doc/doc.py @@ -249,7 +249,8 @@ def json( # we need to do this because pydantic will not recognize DocList correctly original_exclude = original_exclude or {} if field not in original_exclude: - data[field] = [doc.dict() for doc in getattr(self, field)] + val = getattr(self, field) + data[field] = [doc.dict() for doc in val] if val is not None else None # this is copy from pydantic code if self.__custom_root_type__: @@ -319,7 +320,8 @@ def dict( # we need to do this because pydantic will not recognize DocList correctly original_exclude = original_exclude or {} if field not in original_exclude: - data[field] = [doc.dict() for doc in getattr(self, field)] + val = getattr(self, field) + data[field] = [doc.dict() for doc in val] if val is not None else None return data From e85cd8cbbf7737d2a6b98ddf3ddcc688307782f0 Mon Sep 17 00:00:00 2001 From: samsja Date: Mon, 8 May 2023 17:26:40 +0200 Subject: [PATCH 2/3] tests: add more tests Signed-off-by: samsja --- docarray/base_doc/doc.py | 3 +++ tests/units/document/test_base_document.py | 25 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/docarray/base_doc/doc.py b/docarray/base_doc/doc.py index 9b24b384c1e..72b94353a4c 100644 --- a/docarray/base_doc/doc.py +++ b/docarray/base_doc/doc.py @@ -151,6 +151,9 @@ def __setattr__(self, field, value) -> None: object.__setattr__(self, '__dict__', dict_ref) def __eq__(self, other) -> bool: + if not isinstance(other, BaseDoc): + return False + if self.__fields__.keys() != other.__fields__.keys(): return False diff --git a/tests/units/document/test_base_document.py b/tests/units/document/test_base_document.py index 1f68d751eaf..475c03b07df 100644 --- a/tests/units/document/test_base_document.py +++ b/tests/units/document/test_base_document.py @@ -89,3 +89,28 @@ def test_nested_to_dict_exclude_dict(nested_docs): def test_nested_to_json(nested_docs): d = nested_docs.json() nested_docs.__class__.parse_raw(d) + + +@pytest.fixture +def nested_none_docs(): + class SimpleDoc(BaseDoc): + simple_tens: NdArray[10] + + class NestedDoc(BaseDoc): + docs: Optional[DocList[SimpleDoc]] + hello: str = 'world' + + nested_docs = NestedDoc() + + return nested_docs + + +def test_nested_none_to_dict(nested_none_docs): + d = nested_none_docs.dict() + assert d == {'docs': None, 'hello': 'world', 'id': nested_none_docs.id} + + +def test_nested_none_to_json(nested_none_docs): + d = nested_none_docs.json() + d = nested_none_docs.__class__.parse_raw(d) + assert d.dict() == {'docs': None, 'hello': 'world', 'id': nested_none_docs.id} From eda1aed3a8ed6c0f12b2f40b500897fed8dd0e41 Mon Sep 17 00:00:00 2001 From: samsja Date: Mon, 8 May 2023 17:55:01 +0200 Subject: [PATCH 3/3] chore: add comments Signed-off-by: samsja --- docarray/base_doc/doc.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docarray/base_doc/doc.py b/docarray/base_doc/doc.py index 72b94353a4c..f69187d35e0 100644 --- a/docarray/base_doc/doc.py +++ b/docarray/base_doc/doc.py @@ -252,8 +252,9 @@ def json( # we need to do this because pydantic will not recognize DocList correctly original_exclude = original_exclude or {} if field not in original_exclude: - val = getattr(self, field) - data[field] = [doc.dict() for doc in val] if val is not None else None + data[field] = getattr( + self, field + ) # here we need to keep doclist as doclist otherwise if a user want to have a special json config it will not work # this is copy from pydantic code if self.__custom_root_type__: