Skip to content

Commit 6e2d3ef

Browse files
authored
chore!: opensearch - drop Python 3.9 and use X|Y typing (#2744)
1 parent 5f17fca commit 6e2d3ef

6 files changed

Lines changed: 103 additions & 109 deletions

File tree

integrations/opensearch/pyproject.toml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ name = "opensearch-haystack"
77
dynamic = ["version"]
88
description = 'Haystack 2.x Document Store for OpenSearch'
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", 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.14.0",
26+
"haystack-ai>=2.22.0",
2827
"opensearch-py[async]>=2.4.0,<3"]
2928

3029
[project.urls]
@@ -85,7 +84,6 @@ allow-direct-references = true
8584

8685

8786
[tool.ruff]
88-
target-version = "py39"
8987
line-length = 120
9088

9189
[tool.ruff.lint]
@@ -134,10 +132,6 @@ ignore = [
134132
# Ignore asserts
135133
"S101",
136134
]
137-
unfixable = [
138-
# Don't touch unused imports
139-
"F401",
140-
]
141135

142136
[tool.ruff.lint.isort]
143137
known-first-party = ["haystack_integrations"]

integrations/opensearch/src/haystack_integrations/components/retrievers/opensearch/bm25_retriever.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# ruff: noqa: FBT001 Boolean-typed positional argument in function definition
66

7-
from typing import Any, Optional, Union
7+
from typing import Any
88

99
from haystack import component, default_from_dict, default_to_dict, logging
1010
from haystack.dataclasses import Document
@@ -28,13 +28,13 @@ def __init__(
2828
self,
2929
*,
3030
document_store: OpenSearchDocumentStore,
31-
filters: Optional[dict[str, Any]] = None,
32-
fuzziness: Union[int, str] = "AUTO",
31+
filters: dict[str, Any] | None = None,
32+
fuzziness: int | str = "AUTO",
3333
top_k: int = 10,
3434
scale_score: bool = False,
3535
all_terms_must_match: bool = False,
36-
filter_policy: Union[str, FilterPolicy] = FilterPolicy.REPLACE,
37-
custom_query: Optional[dict[str, Any]] = None,
36+
filter_policy: str | FilterPolicy = FilterPolicy.REPLACE,
37+
custom_query: dict[str, Any] | None = None,
3838
raise_on_failure: bool = True,
3939
):
4040
"""
@@ -158,12 +158,12 @@ def _prepare_bm25_args(
158158
self,
159159
*,
160160
query: str,
161-
filters: Optional[dict[str, Any]],
162-
all_terms_must_match: Optional[bool],
163-
top_k: Optional[int],
164-
fuzziness: Optional[Union[str, int]],
165-
scale_score: Optional[bool],
166-
custom_query: Optional[dict[str, Any]],
161+
filters: dict[str, Any] | None,
162+
all_terms_must_match: bool | None,
163+
top_k: int | None,
164+
fuzziness: str | int | None,
165+
scale_score: bool | None,
166+
custom_query: dict[str, Any] | None,
167167
) -> dict[str, Any]:
168168
filters = apply_filter_policy(self._filter_policy, self._filters, filters)
169169

@@ -194,13 +194,13 @@ def _prepare_bm25_args(
194194
def run(
195195
self,
196196
query: str,
197-
filters: Optional[dict[str, Any]] = None,
198-
all_terms_must_match: Optional[bool] = None,
199-
top_k: Optional[int] = None,
200-
fuzziness: Optional[Union[int, str]] = None,
201-
scale_score: Optional[bool] = None,
202-
custom_query: Optional[dict[str, Any]] = None,
203-
document_store: Optional[OpenSearchDocumentStore] = None,
197+
filters: dict[str, Any] | None = None,
198+
all_terms_must_match: bool | None = None,
199+
top_k: int | None = None,
200+
fuzziness: int | str | None = None,
201+
scale_score: bool | None = None,
202+
custom_query: dict[str, Any] | None = None,
203+
document_store: OpenSearchDocumentStore | None = None,
204204
) -> dict[str, list[Document]]:
205205
"""
206206
Retrieve documents using BM25 retrieval.
@@ -294,13 +294,13 @@ def run(
294294
async def run_async( # pylint: disable=too-many-positional-arguments
295295
self,
296296
query: str,
297-
filters: Optional[dict[str, Any]] = None,
298-
all_terms_must_match: Optional[bool] = None,
299-
top_k: Optional[int] = None,
300-
fuzziness: Optional[Union[int, str]] = None,
301-
scale_score: Optional[bool] = None,
302-
custom_query: Optional[dict[str, Any]] = None,
303-
document_store: Optional[OpenSearchDocumentStore] = None,
297+
filters: dict[str, Any] | None = None,
298+
all_terms_must_match: bool | None = None,
299+
top_k: int | None = None,
300+
fuzziness: int | str | None = None,
301+
scale_score: bool | None = None,
302+
custom_query: dict[str, Any] | None = None,
303+
document_store: OpenSearchDocumentStore | None = None,
304304
) -> dict[str, list[Document]]:
305305
"""
306306
Asynchronously retrieve documents using BM25 retrieval.

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# ruff: noqa: FBT001 Boolean-typed positional argument in function definition
66

7-
from typing import Any, Optional, Union
7+
from typing import Any
88

99
from haystack import component, default_from_dict, default_to_dict, logging
1010
from haystack.dataclasses import Document
@@ -28,10 +28,10 @@ def __init__(
2828
self,
2929
*,
3030
document_store: OpenSearchDocumentStore,
31-
filters: Optional[dict[str, Any]] = None,
31+
filters: dict[str, Any] | None = None,
3232
top_k: int = 10,
33-
filter_policy: Union[str, FilterPolicy] = FilterPolicy.REPLACE,
34-
custom_query: Optional[dict[str, Any]] = None,
33+
filter_policy: str | FilterPolicy = FilterPolicy.REPLACE,
34+
custom_query: dict[str, Any] | None = None,
3535
raise_on_failure: bool = True,
3636
efficient_filtering: bool = False,
3737
):
@@ -150,11 +150,11 @@ def from_dict(cls, data: dict[str, Any]) -> "OpenSearchEmbeddingRetriever":
150150
def run(
151151
self,
152152
query_embedding: list[float],
153-
filters: Optional[dict[str, Any]] = None,
154-
top_k: Optional[int] = None,
155-
custom_query: Optional[dict[str, Any]] = None,
156-
efficient_filtering: Optional[bool] = None,
157-
document_store: Optional[OpenSearchDocumentStore] = None,
153+
filters: dict[str, Any] | None = None,
154+
top_k: int | None = None,
155+
custom_query: dict[str, Any] | None = None,
156+
efficient_filtering: bool | None = None,
157+
document_store: OpenSearchDocumentStore | None = None,
158158
) -> dict[str, list[Document]]:
159159
"""
160160
Retrieve documents using a vector similarity metric.
@@ -259,11 +259,11 @@ def run(
259259
async def run_async(
260260
self,
261261
query_embedding: list[float],
262-
filters: Optional[dict[str, Any]] = None,
263-
top_k: Optional[int] = None,
264-
custom_query: Optional[dict[str, Any]] = None,
265-
efficient_filtering: Optional[bool] = None,
266-
document_store: Optional[OpenSearchDocumentStore] = None,
262+
filters: dict[str, Any] | None = None,
263+
top_k: int | None = None,
264+
custom_query: dict[str, Any] | None = None,
265+
efficient_filtering: bool | None = None,
266+
document_store: OpenSearchDocumentStore | None = None,
267267
) -> dict[str, list[Document]]:
268268
"""
269269
Asynchronously retrieve documents using a vector similarity metric.

integrations/opensearch/src/haystack_integrations/components/retrievers/opensearch/open_search_hybrid_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 TYPE_CHECKING, Any, Optional, Union
5+
from typing import TYPE_CHECKING, Any
66

77
from haystack import Document, Pipeline, default_from_dict, default_to_dict, logging, super_component
88
from haystack.components.embedders.types import TextEmbedder
@@ -92,22 +92,22 @@ def __init__(
9292
*,
9393
embedder: TextEmbedder,
9494
# OpenSearchBM25Retriever
95-
filters_bm25: Optional[dict[str, Any]] = None,
96-
fuzziness: Union[int, str] = "AUTO",
95+
filters_bm25: dict[str, Any] | None = None,
96+
fuzziness: int | str = "AUTO",
9797
top_k_bm25: int = 10,
9898
scale_score: bool = False,
9999
all_terms_must_match: bool = False,
100-
filter_policy_bm25: Union[str, FilterPolicy] = FilterPolicy.REPLACE,
101-
custom_query_bm25: Optional[dict[str, Any]] = None,
100+
filter_policy_bm25: str | FilterPolicy = FilterPolicy.REPLACE,
101+
custom_query_bm25: dict[str, Any] | None = None,
102102
# OpenSearchEmbeddingRetriever
103-
filters_embedding: Optional[dict[str, Any]] = None,
103+
filters_embedding: dict[str, Any] | None = None,
104104
top_k_embedding: int = 10,
105-
filter_policy_embedding: Union[str, FilterPolicy] = FilterPolicy.REPLACE,
106-
custom_query_embedding: Optional[dict[str, Any]] = None,
105+
filter_policy_embedding: str | FilterPolicy = FilterPolicy.REPLACE,
106+
custom_query_embedding: dict[str, Any] | None = None,
107107
# DocumentJoiner
108-
join_mode: Union[str, JoinMode] = JoinMode.RECIPROCAL_RANK_FUSION,
109-
weights: Optional[list[float]] = None,
110-
top_k: Optional[int] = None,
108+
join_mode: str | JoinMode = JoinMode.RECIPROCAL_RANK_FUSION,
109+
weights: list[float] | None = None,
110+
top_k: int | None = None,
111111
sort_by_score: bool = True,
112112
# extra kwargs
113113
**kwargs: Any,
@@ -242,10 +242,10 @@ def warm_up(self) -> None: ...
242242
def run(
243243
self,
244244
query: str,
245-
filters_bm25: Optional[dict[str, Any]] = None,
246-
filters_embedding: Optional[dict[str, Any]] = None,
247-
top_k_bm25: Optional[int] = None,
248-
top_k_embedding: Optional[int] = None,
245+
filters_bm25: dict[str, Any] | None = None,
246+
filters_embedding: dict[str, Any] | None = None,
247+
top_k_bm25: int | None = None,
248+
top_k_embedding: int | None = None,
249249
) -> dict[str, list[Document]]: ...
250250

251251
def _create_pipeline(self, data: dict[str, Any]) -> Pipeline:

integrations/opensearch/src/haystack_integrations/document_stores/opensearch/auth.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ class AWSConfigurationError(DocumentStoreError):
2727
"""Exception raised when AWS is not configured correctly"""
2828

2929

30-
def _resolve_secret(secret: Optional[Secret]) -> Optional[str]:
30+
def _resolve_secret(secret: Secret | None) -> str | None:
3131
return secret.resolve_value() if secret else None
3232

3333

3434
def _get_aws_session(
35-
aws_access_key_id: Optional[str] = None,
36-
aws_secret_access_key: Optional[str] = None,
37-
aws_session_token: Optional[str] = None,
38-
aws_region_name: Optional[str] = None,
39-
aws_profile_name: Optional[str] = None,
35+
aws_access_key_id: str | None = None,
36+
aws_secret_access_key: str | None = None,
37+
aws_session_token: str | None = None,
38+
aws_region_name: str | None = None,
39+
aws_profile_name: str | None = None,
4040
**kwargs: Any,
4141
) -> "boto3.Session":
4242
"""
@@ -78,19 +78,19 @@ class AWSAuth:
7878
the necessary `Urllib3AWSV4SignerAuth` creation steps including boto3 Sessions and boto3 credentials.
7979
"""
8080

81-
aws_access_key_id: Optional[Secret] = field(
81+
aws_access_key_id: Secret | None = field(
8282
default_factory=lambda: Secret.from_env_var("AWS_ACCESS_KEY_ID", strict=False)
8383
)
84-
aws_secret_access_key: Optional[Secret] = field(
84+
aws_secret_access_key: Secret | None = field(
8585
default_factory=lambda: Secret.from_env_var("AWS_SECRET_ACCESS_KEY", strict=False)
8686
)
87-
aws_session_token: Optional[Secret] = field(
87+
aws_session_token: Secret | None = field(
8888
default_factory=lambda: Secret.from_env_var("AWS_SESSION_TOKEN", strict=False)
8989
)
90-
aws_region_name: Optional[Secret] = field(
90+
aws_region_name: Secret | None = field(
9191
default_factory=lambda: Secret.from_env_var("AWS_DEFAULT_REGION", strict=False)
9292
)
93-
aws_profile_name: Optional[Secret] = field(default_factory=lambda: Secret.from_env_var("AWS_PROFILE", strict=False))
93+
aws_profile_name: Secret | None = field(default_factory=lambda: Secret.from_env_var("AWS_PROFILE", strict=False))
9494
aws_service: str = field(default="es")
9595

9696
def __post_init__(self) -> None:
@@ -106,7 +106,7 @@ def to_dict(self) -> dict[str, Any]:
106106
_fields = {}
107107
for _field in fields(self):
108108
field_value = getattr(self, _field.name)
109-
if _field.type == Optional[Secret]:
109+
if _field.type == Secret | None:
110110
_fields[_field.name] = field_value.to_dict() if field_value is not None else None
111111
else:
112112
_fields[_field.name] = field_value

0 commit comments

Comments
 (0)