FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: DocList and DocVec are now coerced to each other correctly (#1568) · Ditto190/docarray@693f877 · GitHub

Commit 693f877

Browse files
authored
fix: DocList and DocVec are now coerced to each other correctly (docarray#1568)
Signed-off-by: agaraman0 <agaraman0@gmail.com>
1 parent 65afa9a commit 693f877

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

‎docarray/array/doc_list/doc_list.py‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Type,
1111
TypeVar,
1212
Union,
13+
cast,
1314
overload,
1415
)
1516

@@ -267,8 +268,18 @@ def validate(
267268
):
268269
from docarray.array.doc_vec.doc_vec import DocVec
269270

270-
if isinstance(value, (cls, DocVec)):
271+
if isinstance(value, cls):
271272
return value
273+
elif isinstance(value, DocVec):
274+
if (
275+
issubclass(value.doc_type, cls.doc_type)
276+
or value.doc_type == cls.doc_type
277+
):
278+
return cast(T, value.to_doc_list())
279+
else:
280+
raise ValueError(
281+
f'DocList[value.doc_type] is not compatible with {cls}'
282+
)
272283
elif isinstance(value, cls):
273284
return cls(value)
274285
elif isinstance(value, Iterable):

‎docarray/array/doc_vec/doc_vec.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,14 @@ def validate(
293293
) -> T:
294294
if isinstance(value, cls):
295295
return value
296+
elif isinstance(value, DocList):
297+
if (
298+
issubclass(value.doc_type, cls.doc_type)
299+
or value.doc_type == cls.doc_type
300+
):
301+
return cast(T, value.to_doc_vec())
302+
else:
303+
raise ValueError(f'DocVec[value.doc_type] is not compatible with {cls}')
296304
elif isinstance(value, DocList.__class_getitem__(cls.doc_type)):
297305
return cast(T, value.to_doc_vec())
298306
elif isinstance(value, Sequence):

‎tests/units/array/stack/test_array_stacked.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ def test_to_device():
359359

360360
def test_to_device_with_nested_da():
361361
class Video(BaseDoc):
362-
images: DocList[ImageDoc]
362+
images: DocVec[ImageDoc]
363363

364364
da_image = DocVec[ImageDoc](
365365
[ImageDoc(tensor=torch.zeros(3, 5))], tensor_type=TorchTensor

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL