Skip to content

Commit 8266238

Browse files
committed
fix: raise exception when type of DocList is object
Signed-off-by: punndcoder28 <puneethk.2899@gmail.com>
1 parent d0b9990 commit 8266238

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

docarray/array/doc_list/doc_list.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from pydantic import parse_obj_as
1818
from typing_extensions import SupportsIndex
19-
from typing_inspect import is_union_type
19+
from typing_inspect import is_union_type, is_typevar
2020

2121
from docarray.array.any_array import AnyDocArray
2222
from docarray.array.doc_list.io import IOMixinDocList
@@ -337,8 +337,15 @@ def __class_getitem__(cls, item: Union[Type[BaseDoc], TypeVar, str]):
337337

338338
if isinstance(item, type) and safe_issubclass(item, BaseDoc):
339339
return AnyDocArray.__class_getitem__.__func__(cls, item) # type: ignore
340-
else:
341-
return super().__class_getitem__(item)
340+
if (
341+
isinstance(item, object)
342+
and not is_typevar(item)
343+
and not isinstance(item, str)
344+
and item is not Any
345+
):
346+
raise TypeError('Expecting a type, got object instead')
347+
348+
return super().__class_getitem__(item)
342349

343350
def __repr__(self):
344351
return AnyDocArray.__repr__(self) # type: ignore

tests/units/array/test_array.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,3 +487,13 @@ def test_legacy_doc():
487487
newDoc = LegacyDocument()
488488
da = DocList[LegacyDocument]([newDoc])
489489
da.summary()
490+
491+
492+
def test_parameterize_list():
493+
from docarray import DocList, BaseDoc
494+
495+
with pytest.raises(TypeError) as excinfo:
496+
doc = DocList[BaseDoc()]
497+
assert doc is None
498+
499+
assert str(excinfo.value) == 'Expecting a type, got object instead'

0 commit comments

Comments
 (0)