Skip to content

Commit 29583dd

Browse files
anakin87davidsbatista
authored andcommitted
chore!: mongodb_atlas - drop Python 3.9 and use X|Y typing (#2718)
1 parent 149af5f commit 29583dd

6 files changed

Lines changed: 51 additions & 59 deletions

File tree

integrations/mongodb_atlas/pyproject.toml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ name = "mongodb-atlas-haystack"
77
dynamic = ["version"]
88
description = "An integration of MongoDB Atlas with Haystack"
99
readme = "README.md"
10-
requires-python = ">=3.9"
10+
requires-python = ">=3.10"
1111
license = "Apache-2.0"
1212
keywords = []
1313
authors = [{ name = "deepset GmbH", email = "info@deepset.ai" }]
1414
classifiers = [
1515
"License :: OSI Approved :: Apache Software License",
1616
"Development Status :: 4 - Beta",
1717
"Programming Language :: Python",
18-
"Programming Language :: Python :: 3.9",
1918
"Programming Language :: Python :: 3.10",
2019
"Programming Language :: Python :: 3.11",
2120
"Programming Language :: Python :: 3.12",
@@ -24,7 +23,7 @@ classifiers = [
2423
"Programming Language :: Python :: Implementation :: PyPy",
2524
]
2625
dependencies = [
27-
"haystack-ai>=2.11.0",
26+
"haystack-ai>=2.22.0",
2827
"pymongo[srv]>=4.13.0"
2928
]
3029

@@ -80,7 +79,6 @@ disallow_incomplete_defs = true
8079

8180

8281
[tool.ruff]
83-
target-version = "py39"
8482
line-length = 120
8583

8684
[tool.ruff.lint]
@@ -129,10 +127,6 @@ ignore = [
129127
# Allow assert statements
130128
"S101",
131129
]
132-
unfixable = [
133-
# Don't touch unused imports
134-
"F401",
135-
]
136130

137131
[tool.ruff.lint.isort]
138132
known-first-party = ["haystack_integrations"]

integrations/mongodb_atlas/src/haystack_integrations/components/retrievers/mongodb_atlas/embedding_retriever.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SPDX-FileCopyrightText: 2023-present deepset GmbH <info@deepset.ai>
22
#
33
# SPDX-License-Identifier: Apache-2.0
4-
from typing import Any, Optional, Union
4+
from typing import Any
55

66
from haystack import component, default_from_dict, default_to_dict
77
from haystack.dataclasses import Document
@@ -45,9 +45,9 @@ def __init__(
4545
self,
4646
*,
4747
document_store: MongoDBAtlasDocumentStore,
48-
filters: Optional[dict[str, Any]] = None,
48+
filters: dict[str, Any] | None = None,
4949
top_k: int = 10,
50-
filter_policy: Union[str, FilterPolicy] = FilterPolicy.REPLACE,
50+
filter_policy: str | FilterPolicy = FilterPolicy.REPLACE,
5151
):
5252
"""
5353
Create the MongoDBAtlasDocumentStore component.
@@ -110,8 +110,8 @@ def from_dict(cls, data: dict[str, Any]) -> "MongoDBAtlasEmbeddingRetriever":
110110
def run(
111111
self,
112112
query_embedding: list[float],
113-
filters: Optional[dict[str, Any]] = None,
114-
top_k: Optional[int] = None,
113+
filters: dict[str, Any] | None = None,
114+
top_k: int | None = None,
115115
) -> dict[str, list[Document]]:
116116
"""
117117
Retrieve documents from the MongoDBAtlasDocumentStore, based on the provided embedding similarity.
@@ -138,8 +138,8 @@ def run(
138138
async def run_async(
139139
self,
140140
query_embedding: list[float],
141-
filters: Optional[dict[str, Any]] = None,
142-
top_k: Optional[int] = None,
141+
filters: dict[str, Any] | None = None,
142+
top_k: int | None = None,
143143
) -> dict[str, list[Document]]:
144144
"""
145145
Asynchronously retrieve documents from the MongoDBAtlasDocumentStore, based on the provided embedding

integrations/mongodb_atlas/src/haystack_integrations/components/retrievers/mongodb_atlas/full_text_retriever.py

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

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

77
from haystack import component, default_from_dict, default_to_dict
88
from haystack.dataclasses import Document
@@ -43,9 +43,9 @@ def __init__(
4343
self,
4444
*,
4545
document_store: MongoDBAtlasDocumentStore,
46-
filters: Optional[dict[str, Any]] = None,
46+
filters: dict[str, Any] | None = None,
4747
top_k: int = 10,
48-
filter_policy: Union[str, FilterPolicy] = FilterPolicy.REPLACE,
48+
filter_policy: str | FilterPolicy = FilterPolicy.REPLACE,
4949
):
5050
"""
5151
:param document_store: An instance of MongoDBAtlasDocumentStore.
@@ -103,12 +103,12 @@ def from_dict(cls, data: dict[str, Any]) -> "MongoDBAtlasFullTextRetriever":
103103
@component.output_types(documents=list[Document])
104104
def run(
105105
self,
106-
query: Union[str, list[str]],
107-
fuzzy: Optional[dict[str, int]] = None,
108-
match_criteria: Optional[Literal["any", "all"]] = None,
109-
score: Optional[dict[str, dict]] = None,
110-
synonyms: Optional[str] = None,
111-
filters: Optional[dict[str, Any]] = None,
106+
query: str | list[str],
107+
fuzzy: dict[str, int] | None = None,
108+
match_criteria: Literal["any", "all"] | None = None,
109+
score: dict[str, dict] | None = None,
110+
synonyms: str | None = None,
111+
filters: dict[str, Any] | None = None,
112112
top_k: int = 10,
113113
) -> dict[str, list[Document]]:
114114
"""
@@ -153,12 +153,12 @@ def run(
153153
@component.output_types(documents=list[Document])
154154
async def run_async(
155155
self,
156-
query: Union[str, list[str]],
157-
fuzzy: Optional[dict[str, int]] = None,
158-
match_criteria: Optional[Literal["any", "all"]] = None,
159-
score: Optional[dict[str, dict]] = None,
160-
synonyms: Optional[str] = None,
161-
filters: Optional[dict[str, Any]] = None,
156+
query: str | list[str],
157+
fuzzy: dict[str, int] | None = None,
158+
match_criteria: Literal["any", "all"] | None = None,
159+
score: dict[str, dict] | None = None,
160+
synonyms: str | None = None,
161+
filters: dict[str, Any] | None = None,
162162
top_k: int = 10,
163163
) -> dict[str, list[Document]]:
164164
"""

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

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44
import re
5-
from typing import Any, Literal, Optional, Union
5+
from typing import Any, Literal
66

77
from haystack import default_from_dict, default_to_dict, logging
88
from haystack.dataclasses.document import Document
@@ -105,10 +105,10 @@ def __init__(
105105
self.full_text_search_index = full_text_search_index
106106
self.embedding_field = embedding_field
107107
self.content_field = content_field
108-
self._connection: Optional[MongoClient] = None
109-
self._connection_async: Optional[AsyncMongoClient] = None
110-
self._collection: Optional[Collection] = None
111-
self._collection_async: Optional[AsyncCollection] = None
108+
self._connection: MongoClient | None = None
109+
self._connection_async: AsyncMongoClient | None = None
110+
self._collection: Collection | None = None
111+
self._collection_async: AsyncCollection | None = None
112112

113113
def __del__(self) -> None:
114114
"""
@@ -118,7 +118,7 @@ def __del__(self) -> None:
118118
self._connection.close()
119119

120120
@property
121-
def connection(self) -> Union[AsyncMongoClient, MongoClient]:
121+
def connection(self) -> AsyncMongoClient | MongoClient:
122122
if self._connection:
123123
return self._connection
124124
if self._connection_async:
@@ -127,7 +127,7 @@ def connection(self) -> Union[AsyncMongoClient, MongoClient]:
127127
raise DocumentStoreError(msg)
128128

129129
@property
130-
def collection(self) -> Union[AsyncCollection, Collection]:
130+
def collection(self) -> AsyncCollection | Collection:
131131
if self._collection:
132132
return self._collection
133133
if self._collection_async:
@@ -278,7 +278,7 @@ async def count_documents_async(self) -> int:
278278
assert self._collection_async is not None
279279
return await self._collection_async.count_documents({})
280280

281-
def filter_documents(self, filters: Optional[dict[str, Any]] = None) -> list[Document]:
281+
def filter_documents(self, filters: dict[str, Any] | None = None) -> list[Document]:
282282
"""
283283
Returns the documents that match the filters provided.
284284
@@ -294,7 +294,7 @@ def filter_documents(self, filters: Optional[dict[str, Any]] = None) -> list[Doc
294294
documents = list(self._collection.find(filters))
295295
return [self._mongo_doc_to_haystack_doc(doc) for doc in documents]
296296

297-
async def filter_documents_async(self, filters: Optional[dict[str, Any]] = None) -> list[Document]:
297+
async def filter_documents_async(self, filters: dict[str, Any] | None = None) -> list[Document]:
298298
"""
299299
Asynchronously returns the documents that match the filters provided.
300300
@@ -332,7 +332,7 @@ def write_documents(self, documents: list[Document], policy: DuplicatePolicy = D
332332
policy = DuplicatePolicy.FAIL
333333

334334
mongo_documents = [self._haystack_doc_to_mongo_doc(doc) for doc in documents]
335-
operations: list[Union[UpdateOne, InsertOne, ReplaceOne]]
335+
operations: list[UpdateOne | InsertOne | ReplaceOne]
336336
written_docs = len(documents)
337337

338338
if policy == DuplicatePolicy.SKIP:
@@ -377,7 +377,7 @@ async def write_documents_async(
377377

378378
mongo_documents = [self._haystack_doc_to_mongo_doc(doc) for doc in documents]
379379

380-
operations: list[Union[UpdateOne, InsertOne, ReplaceOne]]
380+
operations: list[UpdateOne | InsertOne | ReplaceOne]
381381
written_docs = len(documents)
382382

383383
if policy == DuplicatePolicy.SKIP:
@@ -636,7 +636,7 @@ async def delete_all_documents_async(self, *, recreate_collection: bool = False)
636636
def _embedding_retrieval(
637637
self,
638638
query_embedding: list[float],
639-
filters: Optional[dict[str, Any]] = None,
639+
filters: dict[str, Any] | None = None,
640640
top_k: int = 10,
641641
) -> list[Document]:
642642
"""
@@ -686,7 +686,7 @@ def _embedding_retrieval(
686686
return documents
687687

688688
async def _embedding_retrieval_async(
689-
self, query_embedding: list[float], filters: Optional[dict[str, Any]] = None, top_k: int = 10
689+
self, query_embedding: list[float], filters: dict[str, Any] | None = None, top_k: int = 10
690690
) -> list[Document]:
691691
"""
692692
Asynchronously find the documents that are most similar to the provided `query_embedding` by using a vector
@@ -738,12 +738,12 @@ async def _embedding_retrieval_async(
738738

739739
def _fulltext_retrieval(
740740
self,
741-
query: Union[str, list[str]],
742-
fuzzy: Optional[dict[str, int]] = None,
743-
match_criteria: Optional[Literal["any", "all"]] = None,
744-
score: Optional[dict[str, dict]] = None,
745-
synonyms: Optional[str] = None,
746-
filters: Optional[dict[str, Any]] = None,
741+
query: str | list[str],
742+
fuzzy: dict[str, int] | None = None,
743+
match_criteria: Literal["any", "all"] | None = None,
744+
score: dict[str, dict] | None = None,
745+
synonyms: str | None = None,
746+
filters: dict[str, Any] | None = None,
747747
top_k: int = 10,
748748
) -> list[Document]:
749749
"""
@@ -831,12 +831,12 @@ def _fulltext_retrieval(
831831

832832
async def _fulltext_retrieval_async(
833833
self,
834-
query: Union[str, list[str]],
835-
fuzzy: Optional[dict[str, int]] = None,
836-
match_criteria: Optional[Literal["any", "all"]] = None,
837-
score: Optional[dict[str, dict]] = None,
838-
synonyms: Optional[str] = None,
839-
filters: Optional[dict[str, Any]] = None,
834+
query: str | list[str],
835+
fuzzy: dict[str, int] | None = None,
836+
match_criteria: Literal["any", "all"] | None = None,
837+
score: dict[str, dict] | None = None,
838+
synonyms: str | None = None,
839+
filters: dict[str, Any] | None = None,
840840
top_k: int = 10,
841841
) -> list[Document]:
842842
"""

integrations/mongodb_atlas/tests/test_fulltext_retrieval.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import os
66
from time import sleep
7-
from typing import Union
87
from unittest.mock import MagicMock
98

109
import pytest
@@ -157,7 +156,7 @@ def test_synonyms_retrieval(self, document_store: MongoDBAtlasDocumentStore):
157156
assert results[0].score >= results[1].score
158157

159158
@pytest.mark.parametrize("query", ["", []])
160-
def test_empty_query_raises_value_error(self, query: Union[str, list], document_store: MongoDBAtlasDocumentStore):
159+
def test_empty_query_raises_value_error(self, query: str | list, document_store: MongoDBAtlasDocumentStore):
161160
with pytest.raises(ValueError):
162161
document_store._fulltext_retrieval(query=query)
163162

integrations/mongodb_atlas/tests/test_fulltext_retrieval_async.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import os
66
from time import sleep
7-
from typing import Union
87
from unittest.mock import AsyncMock, MagicMock, patch
98

109
import pytest
@@ -116,7 +115,7 @@ async def test_synonyms_retrieval_async(self, document_store: MongoDBAtlasDocume
116115

117116
@pytest.mark.parametrize("query", ["", []])
118117
async def test_empty_query_raises_value_error_async(
119-
self, query: Union[str, list], document_store: MongoDBAtlasDocumentStore
118+
self, query: str | list, document_store: MongoDBAtlasDocumentStore
120119
):
121120
with pytest.raises(ValueError):
122121
await document_store._fulltext_retrieval_async(query=query)

0 commit comments

Comments
 (0)