Skip to content

Commit 5be352e

Browse files
fix(supabase): fix lint errors - imports, assert, formatting
1 parent 3ae853c commit 5be352e

3 files changed

Lines changed: 33 additions & 22 deletions

File tree

integrations/supabase/src/haystack_integrations/components/retrievers/supabase/groonga_retriever.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ async def run_async(
108108
top_k: int | None = None,
109109
) -> dict[str, list[Document]]:
110110
"""
111-
Async version of run().
111+
Async version of run().
112112
113113
Note: supabase-py's sync client does not support native async queries.
114114
This method runs the synchronous retrieval and returns the result.
@@ -162,4 +162,4 @@ def from_dict(cls, data: dict[str, Any]) -> "SupabaseGroongaRetriever":
162162
data["init_parameters"]["document_store"] = SupabaseGroongaDocumentStore.from_dict(doc_store_params)
163163
if filter_policy := data["init_parameters"].get("filter_policy"):
164164
data["init_parameters"]["filter_policy"] = FilterPolicy.from_str(filter_policy)
165-
return default_from_dict(cls, data)
165+
return default_from_dict(cls, data)

integrations/supabase/src/haystack_integrations/document_stores/supabase/groonga_document_store.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5-
from typing import Any, Optional
5+
from typing import Any
66

77
from haystack import default_from_dict, default_to_dict, logging
88
from haystack.dataclasses import Document
99
from haystack.document_stores.errors import DuplicateDocumentError
1010
from haystack.document_stores.types import DocumentStore, DuplicatePolicy
1111
from haystack.utils.auth import Secret, deserialize_secrets_inplace
1212

13-
from supabase import create_client, Client
13+
from supabase import Client, create_client
1414

1515
logger = 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)

integrations/supabase/tests/test_groonga_document_store.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import pytest
88
from haystack.dataclasses import Document
9+
from haystack.document_stores.errors import DuplicateDocumentError
910
from haystack.document_stores.types import DuplicatePolicy
1011

1112
from haystack_integrations.components.retrievers.supabase import SupabaseGroongaRetriever
@@ -162,8 +163,6 @@ def test_write_documents_skip(groonga_store, mock_supabase_client):
162163

163164
def test_write_documents_fail_on_duplicate(groonga_store, mock_supabase_client):
164165
"""Test that FAIL policy raises error on duplicate."""
165-
from haystack.document_stores.errors import DuplicateDocumentError
166-
167166
mock_table = mock_supabase_client.table.return_value
168167
# simulate document already exists
169168
mock_table.select.return_value.eq.return_value.execute.return_value = MagicMock(data=[{"id": "existing"}])
@@ -350,4 +349,4 @@ def test_retriever_from_dict(mock_supabase_client, monkeypatch): # noqa: ARG001
350349
},
351350
}
352351
retriever = SupabaseGroongaRetriever.from_dict(data)
353-
assert retriever.top_k == 7
352+
assert retriever.top_k == 7

0 commit comments

Comments
 (0)