From 01daad817ee9bf41db40d62edc880e0e4a399c5e Mon Sep 17 00:00:00 2001 From: Silvano Cerza Date: Thu, 5 Oct 2023 17:04:51 -0700 Subject: [PATCH] Fix linting issues --- src/elasticsearch_haystack/bm25_retriever.py | 5 +++-- src/elasticsearch_haystack/document_store.py | 12 +++++++----- tests/test_bm25_retriever.py | 8 ++++---- tests/test_document_store.py | 4 ++-- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/elasticsearch_haystack/bm25_retriever.py b/src/elasticsearch_haystack/bm25_retriever.py index 56cc16c..485ce2a 100644 --- a/src/elasticsearch_haystack/bm25_retriever.py +++ b/src/elasticsearch_haystack/bm25_retriever.py @@ -1,9 +1,9 @@ # SPDX-FileCopyrightText: 2023-present Silvano Cerza # # SPDX-License-Identifier: Apache-2.0 -from typing import Optional, Dict, List, Any +from typing import Any, Dict, List, Optional -from haystack.preview import component, default_to_dict, default_from_dict +from haystack.preview import component, default_from_dict, default_to_dict from haystack.preview.dataclasses import Document from elasticsearch_haystack.document_store import ElasticsearchDocumentStore @@ -13,6 +13,7 @@ class ElasticsearchBM25Retriever: def __init__( self, + *, document_store: ElasticsearchDocumentStore, filters: Optional[Dict[str, Any]] = None, top_k: int = 10, diff --git a/src/elasticsearch_haystack/document_store.py b/src/elasticsearch_haystack/document_store.py index af56665..16659d4 100644 --- a/src/elasticsearch_haystack/document_store.py +++ b/src/elasticsearch_haystack/document_store.py @@ -8,7 +8,7 @@ import numpy as np from elastic_transport import NodeConfig from elasticsearch import Elasticsearch, helpers -from haystack.preview import default_to_dict, default_from_dict +from haystack.preview import default_from_dict, default_to_dict from haystack.preview.dataclasses import Document from haystack.preview.document_stores.decorator import document_store from haystack.preview.document_stores.errors import DuplicateDocumentError @@ -262,6 +262,7 @@ def delete_documents(self, document_ids: List[str]) -> None: def _bm25_retrieval( self, query: str, + *, filters: Optional[Dict[str, Any]] = None, top_k: int = 10, scale_score: bool = True, @@ -278,7 +279,8 @@ def _bm25_retrieval( `query` must be a non empty string, otherwise a `ValueError` will be raised. :param query: String to search in saved Documents' text. - :param filters: Filters applied to the retrieved Documents, for more info see `ElasticsearchDocumentStore.filter_documents`, defaults to None + :param filters: Filters applied to the retrieved Documents, for more info + see `ElasticsearchDocumentStore.filter_documents`, defaults to None :param top_k: Maximum number of Documents to return, defaults to 10 :param scale_score: If `True` scales the Document`s scores between 0 and 1, defaults to True :raises ValueError: If `query` is an empty string @@ -286,9 +288,10 @@ def _bm25_retrieval( """ if not query: - raise ValueError("query must be a non empty string") + msg = "query must be a non empty string" + raise ValueError(msg) - body = { + body: Dict[str, Any] = { "size": top_k, "query": { "bool": { @@ -304,7 +307,6 @@ def _bm25_retrieval( } }, } - {"query": {"bool": {"must": [{"multi_match": {"query": "PHP", "type": "most_fields", "operator": "AND"}}]}}} if filters: body["query"]["bool"]["filter"] = _normalize_filters(filters) diff --git a/tests/test_bm25_retriever.py b/tests/test_bm25_retriever.py index 8c51d1f..530e85e 100644 --- a/tests/test_bm25_retriever.py +++ b/tests/test_bm25_retriever.py @@ -1,12 +1,12 @@ # SPDX-FileCopyrightText: 2023-present Silvano Cerza # # SPDX-License-Identifier: Apache-2.0 -from unittest.mock import patch, Mock +from unittest.mock import Mock, patch from haystack.preview.dataclasses import Document -from elasticsearch_haystack.document_store import ElasticsearchDocumentStore from elasticsearch_haystack.bm25_retriever import ElasticsearchBM25Retriever +from elasticsearch_haystack.document_store import ElasticsearchDocumentStore def test_init_default(): @@ -19,7 +19,7 @@ def test_init_default(): @patch("elasticsearch_haystack.document_store.Elasticsearch") -def test_to_dict(mock_elasticsearch_client): +def test_to_dict(_mock_elasticsearch_client): document_store = ElasticsearchDocumentStore(hosts="some fake host") retriever = ElasticsearchBM25Retriever(document_store=document_store) res = retriever.to_dict() @@ -38,7 +38,7 @@ def test_to_dict(mock_elasticsearch_client): @patch("elasticsearch_haystack.document_store.Elasticsearch") -def test_from_dict(mock_elasticsearch_client): +def test_from_dict(_mock_elasticsearch_client): data = { "type": "ElasticsearchBM25Retriever", "init_parameters": { diff --git a/tests/test_document_store.py b/tests/test_document_store.py index 07330b4..b40e04b 100644 --- a/tests/test_document_store.py +++ b/tests/test_document_store.py @@ -33,7 +33,7 @@ def docstore(self, request): store._client.options(ignore_status=[400, 404]).indices.delete(index=index) @patch("elasticsearch_haystack.document_store.Elasticsearch") - def test_to_dict(self, mock_elasticsearch_client): + def test_to_dict(self, _mock_elasticsearch_client): document_store = ElasticsearchDocumentStore(hosts="some hosts") res = document_store.to_dict() assert res == { @@ -45,7 +45,7 @@ def test_to_dict(self, mock_elasticsearch_client): } @patch("elasticsearch_haystack.document_store.Elasticsearch") - def test_from_dict(self, mock_elasticsearch_client): + def test_from_dict(self, _mock_elasticsearch_client): data = { "type": "ElasticsearchDocumentStore", "init_parameters": {