-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
34 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,33 +1,14 @@ | ||
from typing import List, Optional, Type | ||
from typing import List, Type | ||
|
||
|
||
from haystack.preview.document_stores.protocols import Store | ||
|
||
|
||
class StoreAwareMixin: | ||
""" | ||
Adds the capability of a component to use a single document store from the `self.store` property. | ||
Signals to the pipeline that this component will need a store added to their input data. | ||
To use this mixin you must specify which document stores to support by setting a value to `supported_stores`. | ||
To support any document store, set it to `[Store]`. | ||
""" | ||
|
||
_store: Optional[Store] = None | ||
supported_stores: List[Type[Store]] # type: ignore # (see https://github.com/python/mypy/issues/4717) | ||
|
||
@property | ||
def store(self) -> Optional[Store]: | ||
return self._store | ||
|
||
@store.setter | ||
def store(self, store: Store): | ||
if not store: | ||
raise ValueError("Can't set the value of the store to None.") | ||
if not isinstance(store, Store): | ||
raise ValueError("'store' does not respect the Store Protocol.") | ||
if not any(isinstance(store, type_) for type_ in type(self).supported_stores): | ||
raise ValueError( | ||
f"Store type '{type(store).__name__}' is not compatible with this component. " | ||
f"Compatible store types: {[type_.__name__ for type_ in type(self).supported_stores]}" | ||
) | ||
self._store = store |
This file contains 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