Skip to content

Commit

Permalink
fix: raise exception when type of DocList is object (#1794)
Browse files Browse the repository at this point in the history
Signed-off-by: punndcoder28 <puneethk.2899@gmail.com>
  • Loading branch information
punndcoder28 committed Sep 19, 2023
1 parent 8f32866 commit 2f3b85e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
13 changes: 10 additions & 3 deletions docarray/array/doc_list/doc_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from pydantic import parse_obj_as
from typing_extensions import SupportsIndex
from typing_inspect import is_union_type
from typing_inspect import is_union_type, is_typevar

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

if isinstance(item, type) and safe_issubclass(item, BaseDoc):
return AnyDocArray.__class_getitem__.__func__(cls, item) # type: ignore
else:
return super().__class_getitem__(item)
if (
isinstance(item, object)
and not is_typevar(item)
and not isinstance(item, str)
and item is not Any
):
raise TypeError('Expecting a type, got object instead')

return super().__class_getitem__(item)

def __repr__(self):
return AnyDocArray.__repr__(self) # type: ignore
Expand Down
10 changes: 10 additions & 0 deletions tests/units/array/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,3 +487,13 @@ def test_legacy_doc():
newDoc = LegacyDocument()
da = DocList[LegacyDocument]([newDoc])
da.summary()


def test_parameterize_list():
from docarray import DocList, BaseDoc

with pytest.raises(TypeError) as excinfo:
doc = DocList[BaseDoc()]
assert doc is None

assert str(excinfo.value) == 'Expecting a type, got object instead'

0 comments on commit 2f3b85e

Please sign in to comment.