From 9e0474fe5300f0dad0f539e5300d5fadfdddf9b6 Mon Sep 17 00:00:00 2001 From: Massimiliano Pippi Date: Fri, 19 Jan 2024 11:30:07 +0100 Subject: [PATCH] mount import paths under haystack_integrations (#244) * mount import paths under haystack_integrations * linter --- integrations/elasticsearch/pyproject.toml | 19 ++++++++++--------- .../retrievers/elasticsearch/__init__.py | 7 +++++++ .../elasticsearch}/bm25_retriever.py | 7 +++---- .../elasticsearch}/embedding_retriever.py | 3 +-- .../elasticsearch}/__init__.py | 2 +- .../elasticsearch}/document_store.py | 5 +++-- .../document_stores/elasticsearch}/filters.py | 0 .../tests/test_bm25_retriever.py | 17 ++++++++--------- .../tests/test_document_store.py | 12 ++++++------ .../tests/test_embedding_retriever.py | 19 ++++++++++--------- .../elasticsearch/tests/test_filters.py | 3 +-- 11 files changed, 50 insertions(+), 44 deletions(-) create mode 100644 integrations/elasticsearch/src/haystack_integrations/components/retrievers/elasticsearch/__init__.py rename integrations/elasticsearch/src/{elasticsearch_haystack => haystack_integrations/components/retrievers/elasticsearch}/bm25_retriever.py (92%) rename integrations/elasticsearch/src/{elasticsearch_haystack => haystack_integrations/components/retrievers/elasticsearch}/embedding_retriever.py (96%) rename integrations/elasticsearch/src/{elasticsearch_haystack => haystack_integrations/document_stores/elasticsearch}/__init__.py (66%) rename integrations/elasticsearch/src/{elasticsearch_haystack => haystack_integrations/document_stores/elasticsearch}/document_store.py (99%) rename integrations/elasticsearch/src/{elasticsearch_haystack => haystack_integrations/document_stores/elasticsearch}/filters.py (100%) diff --git a/integrations/elasticsearch/pyproject.toml b/integrations/elasticsearch/pyproject.toml index 17c9158b9..af3d89c0c 100644 --- a/integrations/elasticsearch/pyproject.toml +++ b/integrations/elasticsearch/pyproject.toml @@ -33,6 +33,9 @@ Documentation = "https://github.com/deepset-ai/haystack-core-integrations/tree/m Issues = "https://github.com/deepset-ai/haystack-core-integrations/issues" Source = "https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/elasticsearch" +[tool.hatch.build.targets.wheel] +packages = ["src/haystack_integrations"] + [tool.hatch.version] source = "vcs" tag-pattern = 'integrations\/elasticsearch-v(?P.*)' @@ -70,7 +73,7 @@ dependencies = [ "ruff>=0.0.243", ] [tool.hatch.envs.lint.scripts] -typing = "mypy --install-types --non-interactive {args:src/elasticsearch_haystack tests}" +typing = "mypy --install-types --non-interactive --explicit-package-bases {args:src/ tests}" style = [ "ruff {args:.}", "black --check --diff {args:.}", @@ -139,26 +142,23 @@ unfixable = [ ] [tool.ruff.isort] -known-first-party = ["elasticsearch_haystack"] +known-first-party = ["src"] [tool.ruff.flake8-tidy-imports] -ban-relative-imports = "all" +ban-relative-imports = "parents" [tool.ruff.per-file-ignores] # Tests can use magic values, assertions, and relative imports "tests/**/*" = ["PLR2004", "S101", "TID252"] [tool.coverage.run] -source_pkgs = ["elasticsearch_haystack", "tests"] +source_pkgs = ["src", "tests"] branch = true parallel = true -omit = [ - "src/elasticsearch_haystack/__about__.py", -] [tool.coverage.paths] -elasticsearch_haystack = ["src/elasticsearch_haystack", "*/elasticsearch-haystack/src/elasticsearch_haystack"] -tests = ["tests", "*/elasticsearch-haystack/tests"] +elasticsearch_haystack = ["src/haystack_integrations", "*/elasticsearch/src/haystack_integrations"] +tests = ["tests", "*/elasticsearch/src/tests"] [tool.coverage.report] exclude_lines = [ @@ -177,6 +177,7 @@ markers = [ [[tool.mypy.overrides]] module = [ "haystack.*", + "haystack_integrations.*", "pytest.*" ] ignore_missing_imports = true diff --git a/integrations/elasticsearch/src/haystack_integrations/components/retrievers/elasticsearch/__init__.py b/integrations/elasticsearch/src/haystack_integrations/components/retrievers/elasticsearch/__init__.py new file mode 100644 index 000000000..bb49c0fd7 --- /dev/null +++ b/integrations/elasticsearch/src/haystack_integrations/components/retrievers/elasticsearch/__init__.py @@ -0,0 +1,7 @@ +# SPDX-FileCopyrightText: 2023-present deepset GmbH +# +# SPDX-License-Identifier: Apache-2.0 +from .bm25_retriever import ElasticsearchBM25Retriever +from .embedding_retriever import ElasticsearchEmbeddingRetriever + +__all__ = ["ElasticsearchBM25Retriever", "ElasticsearchEmbeddingRetriever"] diff --git a/integrations/elasticsearch/src/elasticsearch_haystack/bm25_retriever.py b/integrations/elasticsearch/src/haystack_integrations/components/retrievers/elasticsearch/bm25_retriever.py similarity index 92% rename from integrations/elasticsearch/src/elasticsearch_haystack/bm25_retriever.py rename to integrations/elasticsearch/src/haystack_integrations/components/retrievers/elasticsearch/bm25_retriever.py index cb381e2eb..bd96a5fd8 100644 --- a/integrations/elasticsearch/src/elasticsearch_haystack/bm25_retriever.py +++ b/integrations/elasticsearch/src/haystack_integrations/components/retrievers/elasticsearch/bm25_retriever.py @@ -5,8 +5,7 @@ from haystack import component, default_from_dict, default_to_dict from haystack.dataclasses import Document - -from elasticsearch_haystack.document_store import ElasticsearchDocumentStore +from haystack_integrations.document_stores.elasticsearch.document_store import ElasticsearchDocumentStore @component @@ -19,8 +18,8 @@ class ElasticsearchBM25Retriever: Usage example: ```python from haystack import Document - from elasticsearch_haystack.document_store import ElasticsearchDocumentStore - from elasticsearch_haystack.bm25_retriever import ElasticsearchBM25Retriever + from haystack_integrations.document_stores.elasticsearch import ElasticsearchDocumentStore + from haystack_integrations.components.retrievers.elasticsearch import ElasticsearchBM25Retriever document_store = ElasticsearchDocumentStore(hosts="http://localhost:9200") retriever = ElasticsearchBM25Retriever(document_store=document_store) diff --git a/integrations/elasticsearch/src/elasticsearch_haystack/embedding_retriever.py b/integrations/elasticsearch/src/haystack_integrations/components/retrievers/elasticsearch/embedding_retriever.py similarity index 96% rename from integrations/elasticsearch/src/elasticsearch_haystack/embedding_retriever.py rename to integrations/elasticsearch/src/haystack_integrations/components/retrievers/elasticsearch/embedding_retriever.py index 40c455a4f..a2c825d66 100644 --- a/integrations/elasticsearch/src/elasticsearch_haystack/embedding_retriever.py +++ b/integrations/elasticsearch/src/haystack_integrations/components/retrievers/elasticsearch/embedding_retriever.py @@ -5,8 +5,7 @@ from haystack import component, default_from_dict, default_to_dict from haystack.dataclasses import Document - -from elasticsearch_haystack.document_store import ElasticsearchDocumentStore +from haystack_integrations.document_stores.elasticsearch.document_store import ElasticsearchDocumentStore @component diff --git a/integrations/elasticsearch/src/elasticsearch_haystack/__init__.py b/integrations/elasticsearch/src/haystack_integrations/document_stores/elasticsearch/__init__.py similarity index 66% rename from integrations/elasticsearch/src/elasticsearch_haystack/__init__.py rename to integrations/elasticsearch/src/haystack_integrations/document_stores/elasticsearch/__init__.py index 0c9feacb2..4489971dc 100644 --- a/integrations/elasticsearch/src/elasticsearch_haystack/__init__.py +++ b/integrations/elasticsearch/src/haystack_integrations/document_stores/elasticsearch/__init__.py @@ -1,6 +1,6 @@ # SPDX-FileCopyrightText: 2023-present deepset GmbH # # SPDX-License-Identifier: Apache-2.0 -from elasticsearch_haystack.document_store import ElasticsearchDocumentStore +from .document_store import ElasticsearchDocumentStore __all__ = ["ElasticsearchDocumentStore"] diff --git a/integrations/elasticsearch/src/elasticsearch_haystack/document_store.py b/integrations/elasticsearch/src/haystack_integrations/document_stores/elasticsearch/document_store.py similarity index 99% rename from integrations/elasticsearch/src/elasticsearch_haystack/document_store.py rename to integrations/elasticsearch/src/haystack_integrations/document_stores/elasticsearch/document_store.py index 20469f79f..013acacd1 100644 --- a/integrations/elasticsearch/src/elasticsearch_haystack/document_store.py +++ b/integrations/elasticsearch/src/haystack_integrations/document_stores/elasticsearch/document_store.py @@ -8,14 +8,15 @@ # There are no import stubs for elastic_transport and elasticsearch so mypy fails from elastic_transport import NodeConfig # type: ignore[import-not-found] -from elasticsearch import Elasticsearch, helpers # type: ignore[import-not-found] from haystack import default_from_dict, default_to_dict from haystack.dataclasses import Document from haystack.document_stores.errors import DocumentStoreError, DuplicateDocumentError from haystack.document_stores.types import DuplicatePolicy from haystack.utils.filters import convert -from elasticsearch_haystack.filters import _normalize_filters +from elasticsearch import Elasticsearch, helpers # type: ignore[import-not-found] + +from .filters import _normalize_filters logger = logging.getLogger(__name__) diff --git a/integrations/elasticsearch/src/elasticsearch_haystack/filters.py b/integrations/elasticsearch/src/haystack_integrations/document_stores/elasticsearch/filters.py similarity index 100% rename from integrations/elasticsearch/src/elasticsearch_haystack/filters.py rename to integrations/elasticsearch/src/haystack_integrations/document_stores/elasticsearch/filters.py diff --git a/integrations/elasticsearch/tests/test_bm25_retriever.py b/integrations/elasticsearch/tests/test_bm25_retriever.py index bc1fc55bb..dd88cd0a8 100644 --- a/integrations/elasticsearch/tests/test_bm25_retriever.py +++ b/integrations/elasticsearch/tests/test_bm25_retriever.py @@ -4,9 +4,8 @@ from unittest.mock import Mock, patch from haystack.dataclasses import Document - -from elasticsearch_haystack.bm25_retriever import ElasticsearchBM25Retriever -from elasticsearch_haystack.document_store import ElasticsearchDocumentStore +from haystack_integrations.components.retrievers.elasticsearch import ElasticsearchBM25Retriever +from haystack_integrations.document_stores.elasticsearch import ElasticsearchDocumentStore def test_init_default(): @@ -18,13 +17,13 @@ def test_init_default(): assert not retriever._scale_score -@patch("elasticsearch_haystack.document_store.Elasticsearch") +@patch("haystack_integrations.document_stores.elasticsearch.document_store.Elasticsearch") def test_to_dict(_mock_elasticsearch_client): document_store = ElasticsearchDocumentStore(hosts="some fake host") retriever = ElasticsearchBM25Retriever(document_store=document_store) res = retriever.to_dict() assert res == { - "type": "elasticsearch_haystack.bm25_retriever.ElasticsearchBM25Retriever", + "type": "haystack_integrations.components.retrievers.elasticsearch.bm25_retriever.ElasticsearchBM25Retriever", "init_parameters": { "document_store": { "init_parameters": { @@ -32,7 +31,7 @@ def test_to_dict(_mock_elasticsearch_client): "index": "default", "embedding_similarity_function": "cosine", }, - "type": "elasticsearch_haystack.document_store.ElasticsearchDocumentStore", + "type": "haystack_integrations.document_stores.elasticsearch.document_store.ElasticsearchDocumentStore", }, "filters": {}, "fuzziness": "AUTO", @@ -42,14 +41,14 @@ def test_to_dict(_mock_elasticsearch_client): } -@patch("elasticsearch_haystack.document_store.Elasticsearch") +@patch("haystack_integrations.document_stores.elasticsearch.document_store.Elasticsearch") def test_from_dict(_mock_elasticsearch_client): data = { - "type": "elasticsearch_haystack.bm25_retriever.ElasticsearchBM25Retriever", + "type": "haystack_integrations.components.retrievers.elasticsearch.bm25_retriever.ElasticsearchBM25Retriever", "init_parameters": { "document_store": { "init_parameters": {"hosts": "some fake host", "index": "default"}, - "type": "elasticsearch_haystack.document_store.ElasticsearchDocumentStore", + "type": "haystack_integrations.document_stores.elasticsearch.document_store.ElasticsearchDocumentStore", }, "filters": {}, "fuzziness": "AUTO", diff --git a/integrations/elasticsearch/tests/test_document_store.py b/integrations/elasticsearch/tests/test_document_store.py index b892d9ae4..8d2eab1fb 100644 --- a/integrations/elasticsearch/tests/test_document_store.py +++ b/integrations/elasticsearch/tests/test_document_store.py @@ -12,10 +12,10 @@ from haystack.document_stores.errors import DocumentStoreError, DuplicateDocumentError from haystack.document_stores.types import DuplicatePolicy from haystack.testing.document_store import DocumentStoreBaseTests - -from elasticsearch_haystack.document_store import ElasticsearchDocumentStore +from haystack_integrations.document_stores.elasticsearch import ElasticsearchDocumentStore +@pytest.mark.integration class TestDocumentStore(DocumentStoreBaseTests): """ Common test cases will be provided by `DocumentStoreBaseTests` but @@ -67,12 +67,12 @@ def assert_documents_are_equal(self, received: List[Document], expected: List[Do super().assert_documents_are_equal(received, expected) - @patch("elasticsearch_haystack.document_store.Elasticsearch") + @patch("haystack_integrations.document_stores.elasticsearch.document_store.Elasticsearch") def test_to_dict(self, _mock_elasticsearch_client): document_store = ElasticsearchDocumentStore(hosts="some hosts") res = document_store.to_dict() assert res == { - "type": "elasticsearch_haystack.document_store.ElasticsearchDocumentStore", + "type": "haystack_integrations.document_stores.elasticsearch.document_store.ElasticsearchDocumentStore", "init_parameters": { "hosts": "some hosts", "index": "default", @@ -80,10 +80,10 @@ def test_to_dict(self, _mock_elasticsearch_client): }, } - @patch("elasticsearch_haystack.document_store.Elasticsearch") + @patch("haystack_integrations.document_stores.elasticsearch.document_store.Elasticsearch") def test_from_dict(self, _mock_elasticsearch_client): data = { - "type": "elasticsearch_haystack.document_store.ElasticsearchDocumentStore", + "type": "haystack_integrations.document_stores.elasticsearch.document_store.ElasticsearchDocumentStore", "init_parameters": { "hosts": "some hosts", "index": "default", diff --git a/integrations/elasticsearch/tests/test_embedding_retriever.py b/integrations/elasticsearch/tests/test_embedding_retriever.py index fd60b0940..f632c3655 100644 --- a/integrations/elasticsearch/tests/test_embedding_retriever.py +++ b/integrations/elasticsearch/tests/test_embedding_retriever.py @@ -4,9 +4,8 @@ from unittest.mock import Mock, patch from haystack.dataclasses import Document - -from elasticsearch_haystack.document_store import ElasticsearchDocumentStore -from elasticsearch_haystack.embedding_retriever import ElasticsearchEmbeddingRetriever +from haystack_integrations.components.retrievers.elasticsearch import ElasticsearchEmbeddingRetriever +from haystack_integrations.document_stores.elasticsearch import ElasticsearchDocumentStore def test_init_default(): @@ -18,13 +17,14 @@ def test_init_default(): assert retriever._num_candidates is None -@patch("elasticsearch_haystack.document_store.Elasticsearch") +@patch("haystack_integrations.document_stores.elasticsearch.document_store.Elasticsearch") def test_to_dict(_mock_elasticsearch_client): document_store = ElasticsearchDocumentStore(hosts="some fake host") retriever = ElasticsearchEmbeddingRetriever(document_store=document_store) res = retriever.to_dict() + t = "haystack_integrations.components.retrievers.elasticsearch.embedding_retriever.ElasticsearchEmbeddingRetriever" assert res == { - "type": "elasticsearch_haystack.embedding_retriever.ElasticsearchEmbeddingRetriever", + "type": t, "init_parameters": { "document_store": { "init_parameters": { @@ -32,7 +32,7 @@ def test_to_dict(_mock_elasticsearch_client): "index": "default", "embedding_similarity_function": "cosine", }, - "type": "elasticsearch_haystack.document_store.ElasticsearchDocumentStore", + "type": "haystack_integrations.document_stores.elasticsearch.document_store.ElasticsearchDocumentStore", }, "filters": {}, "top_k": 10, @@ -41,14 +41,15 @@ def test_to_dict(_mock_elasticsearch_client): } -@patch("elasticsearch_haystack.document_store.Elasticsearch") +@patch("haystack_integrations.document_stores.elasticsearch.document_store.Elasticsearch") def test_from_dict(_mock_elasticsearch_client): + t = "haystack_integrations.components.retrievers.elasticsearch.embedding_retriever.ElasticsearchEmbeddingRetriever" data = { - "type": "elasticsearch_haystack.embedding_retriever.ElasticsearchEmbeddingRetriever", + "type": t, "init_parameters": { "document_store": { "init_parameters": {"hosts": "some fake host", "index": "default"}, - "type": "elasticsearch_haystack.document_store.ElasticsearchDocumentStore", + "type": "haystack_integrations.document_stores.elasticsearch.document_store.ElasticsearchDocumentStore", }, "filters": {}, "top_k": 10, diff --git a/integrations/elasticsearch/tests/test_filters.py b/integrations/elasticsearch/tests/test_filters.py index 3cf125fc7..86e5cba74 100644 --- a/integrations/elasticsearch/tests/test_filters.py +++ b/integrations/elasticsearch/tests/test_filters.py @@ -3,8 +3,7 @@ # SPDX-License-Identifier: Apache-2.0 import pytest from haystack.errors import FilterError - -from elasticsearch_haystack.filters import _normalize_filters, _normalize_ranges +from haystack_integrations.document_stores.elasticsearch.filters import _normalize_filters, _normalize_ranges filters_data = [ (