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

fix: better error message when docvec is unusable by JohannesMessner · Pull Request #1675 · docarray/docarray · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (5) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
16 changes: 16 additions & 0 deletions docarray/array/any_array.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from docarray.base_doc import BaseDoc
from docarray.display.document_array_summary import DocArraySummary
from docarray.exceptions.exceptions import UnusableObjectError
from docarray.typing.abstract_type import AbstractType
from docarray.utils._internal._typing import change_cls_name

Expand All @@ -32,6 +33,13 @@
T_doc = TypeVar('T_doc', bound=BaseDoc)
IndexIterType = Union[slice, Iterable[int], Iterable[bool], None]

UNUSABLE_ERROR_MSG = (
'This {cls} instance is in an unusable state. \n'
'The most common cause of this is converting a DocVec to a DocList. '

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Right now this is the only cause, actually, but I wanted to keep our options open here.

'After you call `doc_vec.to_doc_list()`, `doc_vec` cannot be used anymore. '
'Instead, you should do `doc_list = doc_vec.to_doc_list()` and only use `doc_list`.'
)


class AnyDocArray(Sequence[T_doc], Generic[T_doc], AbstractType):
doc_type: Type[BaseDoc]
Expand Down Expand Up @@ -64,9 +72,17 @@ class _DocArrayTyped(cls): # type: ignore

def _property_generator(val: str):
def _getter(self):
if getattr(self, '_is_unusable', False):
raise UnusableObjectError(
UNUSABLE_ERROR_MSG.format(cls=cls.__name__)
)
return self._get_data_column(val)

def _setter(self, value):
if getattr(self, '_is_unusable', False):
raise UnusableObjectError(
UNUSABLE_ERROR_MSG.format(cls=cls.__name__)
)
self._set_data_column(val, value)

# need docstring for the property
Expand Down
10 changes: 9 additions & 1 deletion docarray/array/doc_vec/doc_vec.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def __init__(
f'docs = DocVec[MyDoc](docs) instead of DocVec(docs)'
)
self.tensor_type = tensor_type
self._is_unusable = False

tensor_columns: Dict[str, Optional[AbstractTensor]] = dict()
doc_columns: Dict[str, Optional['DocVec']] = dict()
Expand Down Expand Up @@ -769,7 +770,14 @@ def to_doc_list(self: T) -> DocList[T_doc]:

del self._storage

return DocList.__class_getitem__(self.doc_type).construct(docs)
doc_type = self.doc_type

# Setting _is_unusable will raise an Exception if someone interacts with this instance from hereon out.
# I don't like relying on this state, but we can't override the getattr/setattr directly:
# https://stackoverflow.com/questions/10376604/overriding-special-methods-on-an-instance
self._is_unusable = True

return DocList.__class_getitem__(doc_type).construct(docs)

def traverse_flat(
self,
Expand Down
Empty file.
2 changes: 2 additions & 0 deletions docarray/exceptions/exceptions.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class UnusableObjectError(NotImplementedError):
...
16 changes: 16 additions & 0 deletions tests/units/array/stack/test_array_stacked.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from docarray import BaseDoc, DocList
from docarray.array import DocVec
from docarray.documents import ImageDoc
from docarray.exceptions.exceptions import UnusableObjectError
from docarray.typing import AnyEmbedding, AnyTensor, NdArray, TorchTensor


Expand Down Expand Up @@ -657,3 +658,18 @@ class ImageDoc(BaseDoc):
)

assert da != da2


def teste_unusable_state_raises_exception():
from docarray import DocVec
from docarray.documents import ImageDoc

docs = DocVec[ImageDoc]([ImageDoc(url='http://url.com/foo.png') for _ in range(10)])

docs.to_doc_list()

with pytest.raises(UnusableObjectError):
docs.url

with pytest.raises(UnusableObjectError):
docs.url = 'hi'

Back | FazBrowse Home | New Git URL