Skip to content

Commit 3f90d26

Browse files
committed
stricter type
1 parent 42c45ed commit 3f90d26

2 files changed

Lines changed: 26 additions & 17 deletions

File tree

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

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def __init__(
8282
mappings: dict[str, Any] | None = None,
8383
settings: dict[str, Any] | None = DEFAULT_SETTINGS,
8484
create_index: bool = True,
85-
http_auth: Any = (
85+
http_auth: tuple[Secret, Secret] | tuple[str, str] | list[str] | str | AWSAuth | None = (
8686
Secret.from_env_var("OPENSEARCH_USERNAME", strict=False), # noqa: B008
8787
Secret.from_env_var("OPENSEARCH_PASSWORD", strict=False), # noqa: B008
8888
),
@@ -207,13 +207,16 @@ def to_dict(self) -> dict[str, Any]:
207207
Dictionary with serialized data.
208208
"""
209209
# Handle http_auth serialization
210-
http_auth: list[dict[str, Any]] | dict[str, Any] | None
211-
if isinstance(self._http_auth, (tuple, list)) and all(isinstance(s, Secret) for s in self._http_auth):
212-
http_auth = [s.to_dict() for s in self._http_auth]
210+
http_auth: list[dict[str, Any]] | dict[str, Any] | None = None
211+
if (
212+
isinstance(self._http_auth, (tuple, list))
213+
and len(self._http_auth) == 2 # noqa: PLR2004
214+
and isinstance(self._http_auth[0], Secret)
215+
and isinstance(self._http_auth[1], Secret)
216+
):
217+
http_auth = [self._http_auth[0].to_dict(), self._http_auth[1].to_dict()]
213218
elif isinstance(self._http_auth, AWSAuth):
214219
http_auth = self._http_auth.to_dict()
215-
else:
216-
http_auth = None
217220

218221
return default_to_dict(
219222
self,
@@ -252,18 +255,24 @@ def from_dict(cls, data: dict[str, Any]) -> "OpenSearchDocumentStore":
252255
init_params["http_auth"] = [Secret.from_dict(item) for item in http_auth] if are_secrets else http_auth
253256
return default_from_dict(cls, data)
254257

255-
def _resolve_http_auth(self) -> Any:
258+
def _resolve_http_auth(self) -> tuple[str, str] | list[str] | str | AWSAuth | None:
256259
"""Resolves Secret objects in http_auth to their plain values."""
257-
if not (isinstance(self._http_auth, (tuple, list)) and all(isinstance(s, Secret) for s in self._http_auth)):
260+
if isinstance(self._http_auth, (tuple, list)) and len(self._http_auth) == 2: # noqa: PLR2004
261+
first, second = self._http_auth
262+
if isinstance(first, Secret) and isinstance(second, Secret):
263+
username = first.resolve_value()
264+
password = second.resolve_value()
265+
if username and password:
266+
return [username, password]
267+
if not username and not password:
268+
return None
269+
msg = "http_auth requires both username and password to be set, but only one was provided."
270+
raise DocumentStoreError(msg)
271+
if isinstance(first, str) and isinstance(second, str):
272+
return (first, second)
273+
if isinstance(self._http_auth, (str, AWSAuth)):
258274
return self._http_auth
259-
260-
resolved = [s.resolve_value() for s in self._http_auth]
261-
if all(resolved):
262-
return resolved
263-
if not any(resolved):
264-
return None
265-
msg = "http_auth requires both username and password to be set, but only one was provided."
266-
raise DocumentStoreError(msg)
275+
return None
267276

268277
def _ensure_initialized(self):
269278
# Ideally, we have a warm-up stage for document stores as well as components.

integrations/opensearch/tests/test_auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def test_ds_from_dict_basic_auth(self, _mock_opensearch_client):
254254
document_store._ensure_initialized()
255255
assert document_store._client
256256
_mock_opensearch_client.assert_called_once()
257-
assert _mock_opensearch_client.call_args[1]["http_auth"] == ["user", "pw"]
257+
assert _mock_opensearch_client.call_args[1]["http_auth"] == ("user", "pw")
258258

259259
@patch("haystack_integrations.document_stores.opensearch.document_store.OpenSearch")
260260
def test_ds_from_dict_aws_auth(self, _mock_opensearch_client, monkeypatch: pytest.MonkeyPatch):

0 commit comments

Comments
 (0)