Skip to content

Commit e52b768

Browse files
committed
Add more type hints
1 parent 3c8bf8b commit e52b768

1 file changed

Lines changed: 25 additions & 25 deletions

File tree

  • integrations/qdrant/src/haystack_integrations/document_stores/qdrant

integrations/qdrant/src/haystack_integrations/document_stores/qdrant/document_store.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import inspect
22
from itertools import islice
3-
from typing import Any, AsyncGenerator, ClassVar, Dict, Generator, List, Optional, Set, Union
3+
from typing import Any, AsyncGenerator, ClassVar, Dict, Generator, List, Optional, Set, Tuple, Union
44

55
import numpy as np
66
import qdrant_client
@@ -21,6 +21,7 @@
2121
convert_haystack_documents_to_qdrant_points,
2222
convert_id,
2323
convert_qdrant_point_to_haystack_document,
24+
QdrantPoint
2425
)
2526
from .filters import convert_filters_to_qdrant
2627

@@ -34,7 +35,7 @@ class QdrantStoreError(DocumentStoreError):
3435
FilterType = Dict[str, Union[Dict[str, Any], List[Any], str, int, float, bool]]
3536

3637

37-
def get_batches_from_generator(iterable, n):
38+
def get_batches_from_generator(iterable: List, n: int) -> Generator:
3839
"""
3940
Batch elements of an iterable into fixed-length chunks or blocks.
4041
"""
@@ -127,7 +128,7 @@ def __init__(
127128
write_batch_size: int = 100,
128129
scroll_size: int = 10_000,
129130
payload_fields_to_index: Optional[List[dict]] = None,
130-
):
131+
) -> None:
131132
"""
132133
:param location:
133134
If `memory` - use in-memory Qdrant instance.
@@ -164,7 +165,7 @@ def __init__(
164165
Dimension of the embeddings.
165166
:param on_disk:
166167
Whether to store the collection on disk.
167-
:param use_sparse_embedding:
168+
:param use_sparse_embeddings:
168169
If set to `True`, enables support for sparse embeddings.
169170
:param sparse_idf:
170171
If set to `True`, computes the Inverse Document Frequency (IDF) when using sparse embeddings.
@@ -257,7 +258,7 @@ def __init__(
257258
self.write_batch_size = write_batch_size
258259
self.scroll_size = scroll_size
259260

260-
def _initialize_client(self):
261+
def _initialize_client(self) -> None:
261262
if self._client is None:
262263
client_params = self._prepare_client_params()
263264
self._client = qdrant_client.QdrantClient(**client_params)
@@ -273,7 +274,7 @@ def _initialize_client(self):
273274
self.payload_fields_to_index,
274275
)
275276

276-
async def _initialize_async_client(self):
277+
async def _initialize_async_client(self) -> None:
277278
"""
278279
Returns the asynchronous Qdrant client, initializing it if necessary.
279280
"""
@@ -627,8 +628,6 @@ def get_documents_by_id(
627628
628629
:param ids:
629630
A list of document IDs to retrieve.
630-
:param index:
631-
The name of the index to retrieve documents from.
632631
:returns:
633632
A list of documents.
634633
"""
@@ -660,8 +659,6 @@ async def get_documents_by_id_async(
660659
661660
:param ids:
662661
A list of document IDs to retrieve.
663-
:param index:
664-
The name of the index to retrieve documents from.
665662
:returns:
666663
A list of documents.
667664
"""
@@ -1209,7 +1206,7 @@ def get_distance(self, similarity: str) -> rest.Distance:
12091206
)
12101207
raise QdrantStoreError(msg) from ke
12111208

1212-
def _create_payload_index(self, collection_name: str, payload_fields_to_index: Optional[List[dict]] = None):
1209+
def _create_payload_index(self, collection_name: str, payload_fields_to_index: Optional[List[dict]] = None) -> None:
12131210
"""
12141211
Create payload index for the collection if payload_fields_to_index is provided
12151212
See: https://qdrant.tech/documentation/concepts/indexing/#payload-index
@@ -1228,7 +1225,7 @@ def _create_payload_index(self, collection_name: str, payload_fields_to_index: O
12281225

12291226
async def _create_payload_index_async(
12301227
self, collection_name: str, payload_fields_to_index: Optional[List[dict]] = None
1231-
):
1228+
) -> None:
12321229
"""
12331230
Asynchronously create payload index for the collection if payload_fields_to_index is provided
12341231
See: https://qdrant.tech/documentation/concepts/indexing/#payload-index
@@ -1256,7 +1253,7 @@ def _set_up_collection(
12561253
sparse_idf: bool,
12571254
on_disk: bool = False,
12581255
payload_fields_to_index: Optional[List[dict]] = None,
1259-
):
1256+
) -> None:
12601257
"""
12611258
Sets up the Qdrant collection with the specified parameters.
12621259
:param collection_name:
@@ -1312,7 +1309,7 @@ async def _set_up_collection_async(
13121309
sparse_idf: bool,
13131310
on_disk: bool = False,
13141311
payload_fields_to_index: Optional[List[dict]] = None,
1315-
):
1312+
) -> None:
13161313
"""
13171314
Asynchronously sets up the Qdrant collection with the specified parameters.
13181315
:param collection_name:
@@ -1366,7 +1363,7 @@ def recreate_collection(
13661363
on_disk: Optional[bool] = None,
13671364
use_sparse_embeddings: Optional[bool] = None,
13681365
sparse_idf: bool = False,
1369-
):
1366+
) -> None:
13701367
"""
13711368
Recreates the Qdrant collection with the specified parameters.
13721369
@@ -1409,7 +1406,7 @@ async def recreate_collection_async(
14091406
on_disk: Optional[bool] = None,
14101407
use_sparse_embeddings: Optional[bool] = None,
14111408
sparse_idf: bool = False,
1412-
):
1409+
) -> None:
14131410
"""
14141411
Asynchronously recreates the Qdrant collection with the specified parameters.
14151412
@@ -1448,7 +1445,7 @@ def _handle_duplicate_documents(
14481445
self,
14491446
documents: List[Document],
14501447
policy: DuplicatePolicy = None,
1451-
):
1448+
) -> List[Document]:
14521449
"""
14531450
Checks whether any of the passed documents is already existing in the chosen index and returns a list of
14541451
documents that are not in the index yet.
@@ -1475,7 +1472,7 @@ async def _handle_duplicate_documents_async(
14751472
self,
14761473
documents: List[Document],
14771474
policy: DuplicatePolicy = None,
1478-
):
1475+
) -> List[Document]:
14791476
"""
14801477
Asynchronously checks whether any of the passed documents is already existing
14811478
in the chosen index and returns a list of
@@ -1520,7 +1517,7 @@ def _drop_duplicate_documents(self, documents: List[Document]) -> List[Document]
15201517

15211518
return _documents
15221519

1523-
def _prepare_collection_params(self):
1520+
def _prepare_collection_params(self) -> Dict[str, Any]:
15241521
"""
15251522
Prepares the common parameters for collection creation.
15261523
"""
@@ -1536,7 +1533,7 @@ def _prepare_collection_params(self):
15361533
"init_from": self.init_from,
15371534
}
15381535

1539-
def _prepare_client_params(self):
1536+
def _prepare_client_params(self) -> Dict[str, Any]:
15401537
"""
15411538
Prepares the common parameters for client initialization.
15421539
@@ -1564,7 +1561,7 @@ def _prepare_collection_config(
15641561
on_disk: Optional[bool] = None,
15651562
use_sparse_embeddings: Optional[bool] = None,
15661563
sparse_idf: bool = False,
1567-
):
1564+
) -> Tuple[Dict[str, rest.VectorParams], Optional[Dict[str, rest.SparseVectorParams]]]:
15681565
"""
15691566
Prepares the configuration for creating or recreating a Qdrant collection.
15701567
@@ -1594,9 +1591,12 @@ def _prepare_collection_config(
15941591

15951592
return vectors_config, sparse_vectors_config
15961593

1597-
def _validate_filters(self, filters: Optional[Union[Dict[str, Any], rest.Filter]] = None):
1594+
def _validate_filters(self, filters: Optional[Union[Dict[str, Any], rest.Filter]] = None) -> None:
15981595
"""
15991596
Validates the filters provided for querying.
1597+
1598+
:param filters: Filters to validate. Can be a dictionary or an instance of `qdrant_client.http.models.Filter`.
1599+
:raises ValueError: If the filters are not in the correct format or syntax.
16001600
"""
16011601
if filters and not isinstance(filters, dict) and not isinstance(filters, rest.Filter):
16021602
msg = "Filter must be a dictionary or an instance of `qdrant_client.http.models.Filter`"
@@ -1606,7 +1606,7 @@ def _validate_filters(self, filters: Optional[Union[Dict[str, Any], rest.Filter]
16061606
msg = "Invalid filter syntax. See https://docs.haystack.deepset.ai/docs/metadata-filtering for details."
16071607
raise ValueError(msg)
16081608

1609-
def _process_query_point_results(self, results, scale_score: bool = False):
1609+
def _process_query_point_results(self, results: List[QdrantPoint], scale_score: bool = False) -> List[Document]:
16101610
"""
16111611
Processes query results from Qdrant.
16121612
"""
@@ -1626,7 +1626,7 @@ def _process_query_point_results(self, results, scale_score: bool = False):
16261626

16271627
return documents
16281628

1629-
def _process_group_results(self, groups):
1629+
def _process_group_results(self, groups: List[rest.PointGroup]) -> List[Document]:
16301630
"""
16311631
Processes grouped query results from Qdrant.
16321632
@@ -1646,7 +1646,7 @@ def _validate_collection_compatibility(
16461646
collection_info,
16471647
distance,
16481648
embedding_dim: int,
1649-
):
1649+
) -> None:
16501650
"""
16511651
Validates that an existing collection is compatible with the current configuration.
16521652
"""

0 commit comments

Comments
 (0)