Skip to content

Commit 0d94881

Browse files
committed
use Protocol over ABC
1 parent 11c8825 commit 0d94881

File tree

6 files changed

+19
-37
lines changed

6 files changed

+19
-37
lines changed

elasticsearch/helpers/vectorstore/_async/embedding_service.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
from abc import ABC, abstractmethod
19-
from typing import List, Optional
18+
from typing import List, Optional, Protocol
2019

2120
from elasticsearch import AsyncElasticsearch
2221

2322

24-
class AsyncEmbeddingService(ABC):
25-
@abstractmethod
23+
class AsyncEmbeddingService(Protocol):
2624
async def embed_documents(self, texts: List[str]) -> List[List[float]]:
2725
"""Generate embeddings for a list of documents.
2826
@@ -33,7 +31,6 @@ async def embed_documents(self, texts: List[str]) -> List[List[float]]:
3331
A list of embeddings, one for each document in the input.
3432
"""
3533

36-
@abstractmethod
3734
async def embed_query(self, query: str) -> List[float]:
3835
"""Generate an embedding for a single query text.
3936

elasticsearch/helpers/vectorstore/_async/strategies.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
from abc import ABC, abstractmethod
19-
from typing import Any, Dict, List, Optional, Tuple, Union, cast
18+
from typing import Any, Dict, List, Optional, Protocol, Tuple, Union, cast
2019

2120
from elasticsearch import AsyncElasticsearch
2221
from elasticsearch.helpers.vectorstore._async._utils import model_must_be_deployed
2322
from elasticsearch.helpers.vectorstore._utils import DistanceMetric
2423

2524

26-
class AsyncRetrievalStrategy(ABC):
27-
@abstractmethod
25+
class AsyncRetrievalStrategy(Protocol):
2826
def es_query(
2927
self,
3028
query: Optional[str],
@@ -50,7 +48,6 @@ def es_query(
5048
Dict: The Elasticsearch query body.
5149
"""
5250

53-
@abstractmethod
5451
def es_mappings_settings(
5552
self,
5653
text_field: str,

elasticsearch/helpers/vectorstore/_sync/embedding_service.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
from abc import ABC, abstractmethod
19-
from typing import List, Optional
18+
from typing import List, Optional, Protocol
2019

2120
from elasticsearch import Elasticsearch
2221

2322

24-
class EmbeddingService(ABC):
25-
@abstractmethod
23+
class EmbeddingService(Protocol):
2624
def embed_documents(self, texts: List[str]) -> List[List[float]]:
2725
"""Generate embeddings for a list of documents.
2826
@@ -33,7 +31,6 @@ def embed_documents(self, texts: List[str]) -> List[List[float]]:
3331
A list of embeddings, one for each document in the input.
3432
"""
3533

36-
@abstractmethod
3734
def embed_query(self, query: str) -> List[float]:
3835
"""Generate an embedding for a single query text.
3936

elasticsearch/helpers/vectorstore/_sync/strategies.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
from abc import ABC, abstractmethod
19-
from typing import Any, Dict, List, Optional, Tuple, Union, cast
18+
from typing import Any, Dict, List, Optional, Protocol, Tuple, Union, cast
2019

2120
from elasticsearch import Elasticsearch
2221
from elasticsearch.helpers.vectorstore._sync._utils import model_must_be_deployed
2322
from elasticsearch.helpers.vectorstore._utils import DistanceMetric
2423

2524

26-
class RetrievalStrategy(ABC):
27-
@abstractmethod
25+
class RetrievalStrategy(Protocol):
2826
def es_query(
2927
self,
3028
query: Optional[str],
@@ -50,7 +48,6 @@ def es_query(
5048
Dict: The Elasticsearch query body.
5149
"""
5250

53-
@abstractmethod
5451
def es_mappings_settings(
5552
self,
5653
text_field: str,

test_elasticsearch/utils.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -215,16 +215,10 @@ def wipe_data_streams(client):
215215

216216

217217
def wipe_indices(client):
218-
response = client.indices.get(index="_all")
219-
index_names = response.keys()
220-
for index_name in index_names:
221-
if index_name.startswith("test_"):
222-
client.indices.delete(index=index_name)
223-
client.indices.refresh(index="_all")
224-
# client.options(ignore_status=404).indices.delete(
225-
# index="*,-.ds-ilm-history-*",
226-
# expand_wildcards="all",
227-
# )
218+
client.options(ignore_status=404).indices.delete(
219+
index="*,-.ds-ilm-history-*",
220+
expand_wildcards="all",
221+
)
228222

229223

230224
def wipe_searchable_snapshot_indices(client):

utils/run-unasync.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@
2424

2525

2626
def cleanup(source_dir: Path, output_dir: Path, patterns: list[str]):
27-
if patterns:
28-
for file in glob("*.py", root_dir=source_dir):
29-
path = Path(output_dir) / file
30-
for pattern in patterns:
31-
subprocess.check_call(["sed", "-i.bak", pattern, str(path)])
32-
subprocess.check_call(["rm", f"{path}.bak"])
27+
for file in glob("*.py", root_dir=source_dir):
28+
path = Path(output_dir) / file
29+
for pattern in patterns:
30+
subprocess.check_call(["sed", "-i.bak", pattern, str(path)])
31+
subprocess.check_call(["rm", f"{path}.bak"])
3332

3433

3534
def format_dir(dir: Path):
@@ -57,7 +56,8 @@ def run(
5756

5857
unasync.unasync_files(filepaths, [rule])
5958

60-
cleanup(source_dir, output_dir, cleanup_patterns)
59+
if cleanup_patterns:
60+
cleanup(source_dir, output_dir, cleanup_patterns)
6161

6262
if format:
6363
format_dir(source_dir)

0 commit comments

Comments
 (0)