22#
33# SPDX-License-Identifier: Apache-2.0
44
5- from typing import Any , Optional
5+ from typing import Any
66
77from haystack import default_from_dict , default_to_dict , logging
88from haystack .dataclasses import Document
99from haystack .document_stores .errors import DuplicateDocumentError
1010from haystack .document_stores .types import DocumentStore , DuplicatePolicy
1111from haystack .utils .auth import Secret , deserialize_secrets_inplace
1212
13- from supabase import create_client , Client
13+ from supabase import Client , create_client
1414
1515logger = logging .getLogger (__name__ )
1616
@@ -70,7 +70,7 @@ def __init__(
7070 self .recreate_table = recreate_table
7171
7272 # Client is initialized lazily in warm_up()
73- self ._client : Optional [ Client ] = None
73+ self ._client : Client | None = None
7474
7575 def warm_up (self ) -> None :
7676 """
@@ -88,7 +88,9 @@ def _setup_table(self) -> None:
8888
8989 If recreate_table is True, drops and recreates the table.
9090 """
91- assert self ._client is not None , "Call warm_up() before using the document store."
91+ if self ._client is None :
92+ msg = "Call warm_up() before using the document store."
93+ raise RuntimeError (msg )
9294
9395 if self .recreate_table :
9496 self ._client .rpc ("exec_sql" , {"query" : f"DROP TABLE IF EXISTS { self .table_name } ;" }).execute ()
@@ -118,7 +120,9 @@ def count_documents(self) -> int:
118120
119121 :returns: Number of documents.
120122 """
121- assert self ._client is not None , "Call warm_up() before using the document store."
123+ if self ._client is None :
124+ msg = "Call warm_up() before using the document store."
125+ raise RuntimeError (msg )
122126 result = self ._client .table (self .table_name ).select ("*" , count = "exact" ).execute ()
123127 return int (result .count ) if result .count is not None else 0
124128
@@ -128,10 +132,13 @@ def filter_documents(self, filters: dict[str, Any] | None = None) -> list[Docume
128132
129133 Supported filters: equality filters on `id`, `content`, and `meta` fields.
130134
131- :param filters: Optional dictionary of filters. Example: {"field": "meta.language", "operator": "==", "value": "en"}
135+ :param filters: Optional dictionary of filters.
136+ Example: ``{"field": "meta.language", "operator": "==", "value": "en"}``
132137 :returns: List of matching Document objects.
133138 """
134- assert self ._client is not None , "Call warm_up() before using the document store."
139+ if self ._client is None :
140+ msg = "Call warm_up() before using the document store."
141+ raise RuntimeError (msg )
135142
136143 query = self ._client .table (self .table_name ).select ("*" )
137144
@@ -149,7 +156,6 @@ def _apply_filters(self, query: Any, filters: dict[str, Any]) -> Any:
149156 :param filters: Dictionary of filters to apply.
150157 :returns: The query with filters applied.
151158 """
152- operator = filters .get ("operator" , "AND" )
153159 conditions = filters .get ("conditions" , [])
154160
155161 for condition in conditions :
@@ -159,13 +165,12 @@ def _apply_filters(self, query: Any, filters: dict[str, Any]) -> Any:
159165
160166 # Handle nested meta fields e.g. "meta.language"
161167 if field .startswith ("meta." ):
162- meta_key = field [len ("meta." ):]
168+ meta_key = field [len ("meta." ) :]
163169 if op == "==" :
164170 query = query .eq (f"meta->>'{ meta_key } '" , value )
165171 elif op == "!=" :
166172 query = query .neq (f"meta->>'{ meta_key } '" , value )
167- else :
168- if op == "==" :
173+ elif op == "==" :
169174 query = query .eq (field , value )
170175 elif op == "!=" :
171176 query = query .neq (field , value )
@@ -186,7 +191,9 @@ def write_documents(
186191 :param policy: How to handle duplicate documents. Defaults to DuplicatePolicy.FAIL.
187192 :returns: Number of documents written.
188193 """
189- assert self ._client is not None , "Call warm_up() before using the document store."
194+ if self ._client is None :
195+ msg = "Call warm_up() before using the document store."
196+ raise RuntimeError (msg )
190197
191198 if not documents :
192199 return 0
@@ -226,7 +233,9 @@ def delete_documents(self, document_ids: list[str]) -> None:
226233
227234 :param document_ids: List of document IDs to delete.
228235 """
229- assert self ._client is not None , "Call warm_up() before using the document store."
236+ if self ._client is None :
237+ msg = "Call warm_up() before using the document store."
238+ raise RuntimeError (msg )
230239
231240 if not document_ids :
232241 return
@@ -246,10 +255,13 @@ def _groonga_retrieval(
246255 :param filters: Optional filters to apply after retrieval.
247256 :returns: List of matching Document objects ranked by relevance.
248257 """
249- assert self ._client is not None , "Call warm_up() before using the document store."
258+ if self ._client is None :
259+ msg = "Call warm_up() before using the document store."
260+ raise RuntimeError (msg )
250261
251262 result = self ._client .rpc (
252- "groonga_search" , {"query_text" : query , "table" : self .table_name , "top_k" : top_k }
263+ "groonga_search" ,
264+ {"query_text" : query , "table" : self .table_name , "top_k" : top_k },
253265 ).execute ()
254266
255267 documents = [self ._to_haystack_document (row ) for row in (result .data or []) if isinstance (row , dict )]
@@ -279,7 +291,7 @@ def _filter_documents_in_memory(self, documents: list[Document], filters: dict[s
279291 value = condition .get ("value" )
280292
281293 if field .startswith ("meta." ):
282- meta_key = field [len ("meta." ):]
294+ meta_key = field [len ("meta." ) :]
283295 doc_value = doc .meta .get (meta_key )
284296 else :
285297 doc_value = getattr (doc , field , None )
@@ -336,4 +348,4 @@ def from_dict(cls, data: dict[str, Any]) -> "SupabaseGroongaDocumentStore":
336348 :returns: Deserialized component.
337349 """
338350 deserialize_secrets_inplace (data ["init_parameters" ], ["supabase_key" ])
339- return default_from_dict (cls , data )
351+ return default_from_dict (cls , data )
0 commit comments