Skip to content

fix: better error message when docvec is unusable #1675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docarray/array/any_array.py
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,12 @@
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
Member Author

Choose a reason for hiding this comment

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

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.'
)


class AnyDocArray(Sequence[T_doc], Generic[T_doc], AbstractType):
doc_type: Type[BaseDoc]
Expand Down Expand Up @@ -64,9 +71,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
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 added docarray/exceptions/__init__.py
Empty file.
2 changes: 2 additions & 0 deletions docarray/exceptions/exceptions.py
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
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'
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy