Skip to content

Supports typing around documents with Mapping #896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions meilisearch/_httprequests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import json
from functools import lru_cache
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
from typing import Any, Callable, List, Mapping, Optional, Sequence, Tuple, Union

import requests

Expand All @@ -27,7 +27,9 @@ def send_request(
self,
http_method: Callable,
path: str,
body: Optional[Union[Dict[str, Any], List[Dict[str, Any]], List[str], str]] = None,
body: Optional[
Union[Mapping[str, Any], Sequence[Mapping[str, Any]], List[str], str]
] = None,
content_type: Optional[str] = None,
) -> Any:
if content_type:
Expand Down Expand Up @@ -67,31 +69,37 @@ def get(self, path: str) -> Any:
def post(
self,
path: str,
body: Optional[Union[Dict[str, Any], List[Dict[str, Any]], List[str], str]] = None,
body: Optional[
Union[Mapping[str, Any], Sequence[Mapping[str, Any]], List[str], str]
] = None,
content_type: Optional[str] = "application/json",
) -> Any:
return self.send_request(requests.post, path, body, content_type)

def patch(
self,
path: str,
body: Optional[Union[Dict[str, Any], List[Dict[str, Any]], List[str], str]] = None,
body: Optional[
Union[Mapping[str, Any], Sequence[Mapping[str, Any]], List[str], str]
] = None,
content_type: Optional[str] = "application/json",
) -> Any:
return self.send_request(requests.patch, path, body, content_type)

def put(
self,
path: str,
body: Optional[Union[Dict[str, Any], List[Dict[str, Any]], List[str], str]] = None,
body: Optional[
Union[Mapping[str, Any], Sequence[Mapping[str, Any]], List[str], str]
] = None,
content_type: Optional[str] = "application/json",
) -> Any:
return self.send_request(requests.put, path, body, content_type)

def delete(
self,
path: str,
body: Optional[Union[Dict[str, Any], List[Dict[str, Any]], List[str]]] = None,
body: Optional[Union[Mapping[str, Any], Sequence[Mapping[str, Any]], List[str]]] = None,
) -> Any:
return self.send_request(requests.delete, path, body)

Expand Down
28 changes: 15 additions & 13 deletions meilisearch/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import hmac
import json
import re
from typing import Any, Dict, List, Optional, Tuple, Union
from typing import Any, Dict, List, Mapping, MutableMapping, Optional, Sequence, Tuple, Union
from urllib import parse

from meilisearch._httprequests import HttpRequests
Expand Down Expand Up @@ -55,7 +55,7 @@ def __init__(

self.task_handler = TaskHandler(self.config)

def create_index(self, uid: str, options: Optional[Dict[str, Any]] = None) -> TaskInfo:
def create_index(self, uid: str, options: Optional[Mapping[str, Any]] = None) -> TaskInfo:
"""Create an index.

Parameters
Expand Down Expand Up @@ -102,7 +102,7 @@ def delete_index(self, uid: str) -> TaskInfo:

return TaskInfo(**task)

def get_indexes(self, parameters: Optional[Dict[str, Any]] = None) -> Dict[str, List[Index]]:
def get_indexes(self, parameters: Optional[Mapping[str, Any]] = None) -> Dict[str, List[Index]]:
"""Get all indexes.

Parameters
Expand Down Expand Up @@ -135,7 +135,9 @@ def get_indexes(self, parameters: Optional[Dict[str, Any]] = None) -> Dict[str,
]
return response

def get_raw_indexes(self, parameters: Optional[Dict[str, Any]] = None) -> List[Dict[str, Any]]:
def get_raw_indexes(
self, parameters: Optional[Mapping[str, Any]] = None
) -> List[Dict[str, Any]]:
"""Get all indexes in dictionary format.

Parameters
Expand Down Expand Up @@ -217,7 +219,7 @@ def index(self, uid: str) -> Index:
return Index(self.config, uid=uid)
raise ValueError("The index UID should not be None")

def multi_search(self, queries: List[Dict[str, Any]]) -> Dict[str, List[Dict[str, Any]]]:
def multi_search(self, queries: Sequence[Mapping[str, Any]]) -> Dict[str, List[Dict[str, Any]]]:
"""Multi-index search.

Parameters
Expand Down Expand Up @@ -305,7 +307,7 @@ def get_key(self, key_or_uid: str) -> Key:

return Key(**key)

def get_keys(self, parameters: Optional[Dict[str, Any]] = None) -> KeysResults:
def get_keys(self, parameters: Optional[Mapping[str, Any]] = None) -> KeysResults:
"""Gets the Meilisearch API keys.

Parameters
Expand All @@ -330,7 +332,7 @@ def get_keys(self, parameters: Optional[Dict[str, Any]] = None) -> KeysResults:

return KeysResults(**keys)

def create_key(self, options: Dict[str, Any]) -> Key:
def create_key(self, options: Mapping[str, Any]) -> Key:
"""Creates a new API key.

Parameters
Expand All @@ -357,7 +359,7 @@ def create_key(self, options: Dict[str, Any]) -> Key:

return Key(**task)

def update_key(self, key_or_uid: str, options: Dict[str, Any]) -> Key:
def update_key(self, key_or_uid: str, options: Mapping[str, Any]) -> Key:
"""Update an API key.

Parameters
Expand Down Expand Up @@ -455,7 +457,7 @@ def create_dump(self) -> TaskInfo:

return TaskInfo(**task)

def swap_indexes(self, parameters: List[Dict[str, List[str]]]) -> TaskInfo:
def swap_indexes(self, parameters: List[Mapping[str, List[str]]]) -> TaskInfo:
"""Swap two indexes.

Parameters
Expand All @@ -476,7 +478,7 @@ def swap_indexes(self, parameters: List[Dict[str, List[str]]]) -> TaskInfo:
"""
return TaskInfo(**self.http.post(self.config.paths.swap, parameters))

def get_tasks(self, parameters: Optional[Dict[str, Any]] = None) -> TaskResults:
def get_tasks(self, parameters: Optional[MutableMapping[str, Any]] = None) -> TaskResults:
"""Get all tasks.

Parameters
Expand Down Expand Up @@ -517,7 +519,7 @@ def get_task(self, uid: int) -> Task:
"""
return self.task_handler.get_task(uid)

def cancel_tasks(self, parameters: Dict[str, Any]) -> TaskInfo:
def cancel_tasks(self, parameters: MutableMapping[str, Any]) -> TaskInfo:
"""Cancel a list of enqueued or processing tasks.

Parameters
Expand All @@ -538,7 +540,7 @@ def cancel_tasks(self, parameters: Dict[str, Any]) -> TaskInfo:
"""
return self.task_handler.cancel_tasks(parameters=parameters)

def delete_tasks(self, parameters: Dict[str, Any]) -> TaskInfo:
def delete_tasks(self, parameters: MutableMapping[str, Any]) -> TaskInfo:
"""Delete a list of finished tasks.

Parameters
Expand Down Expand Up @@ -589,7 +591,7 @@ def wait_for_task(
def generate_tenant_token(
self,
api_key_uid: str,
search_rules: Union[Dict[str, Any], List[str]],
search_rules: Union[Mapping[str, Any], Sequence[str]],
*,
expires_at: Optional[datetime.datetime] = None,
api_key: Optional[str] = None,
Expand Down
34 changes: 18 additions & 16 deletions meilisearch/index.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from datetime import datetime
from typing import Any, Dict, Generator, List, Optional, Union
from typing import Any, Dict, Generator, List, Mapping, MutableMapping, Optional, Sequence, Union
from urllib import parse
from warnings import warn

Expand Down Expand Up @@ -118,7 +118,7 @@ def get_primary_key(self) -> str | None:
return self.fetch_info().primary_key

@staticmethod
def create(config: Config, uid: str, options: Optional[Dict[str, Any]] = None) -> TaskInfo:
def create(config: Config, uid: str, options: Optional[Mapping[str, Any]] = None) -> TaskInfo:
"""Create the index.

Parameters
Expand Down Expand Up @@ -146,7 +146,7 @@ def create(config: Config, uid: str, options: Optional[Dict[str, Any]] = None) -

return TaskInfo(**task)

def get_tasks(self, parameters: Optional[Dict[str, Any]] = None) -> TaskResults:
def get_tasks(self, parameters: Optional[MutableMapping[str, Any]] = None) -> TaskResults:
"""Get all tasks of a specific index from the last one.

Parameters
Expand Down Expand Up @@ -244,7 +244,7 @@ def get_stats(self) -> IndexStats:
return IndexStats(stats)

@version_error_hint_message
def search(self, query: str, opt_params: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
def search(self, query: str, opt_params: Optional[Mapping[str, Any]] = None) -> Dict[str, Any]:
"""Search in the index.

Parameters
Expand Down Expand Up @@ -284,7 +284,7 @@ def facet_search(
self,
facet_name: str,
facet_query: Optional[str] = None,
opt_params: Optional[Dict[str, Any]] = None,
opt_params: Optional[Mapping[str, Any]] = None,
) -> Dict[str, Any]:
"""
Perform a facet search based on the given facet query and facet name.
Expand Down Expand Up @@ -313,7 +313,7 @@ def facet_search(
)

def get_document(
self, document_id: Union[str, int], parameters: Optional[Dict[str, Any]] = None
self, document_id: Union[str, int], parameters: Optional[MutableMapping[str, Any]] = None
) -> Document:
"""Get one document with given document identifier.

Expand Down Expand Up @@ -345,7 +345,9 @@ def get_document(
return Document(document)

@version_error_hint_message
def get_documents(self, parameters: Optional[Dict[str, Any]] = None) -> DocumentsResults:
def get_documents(
self, parameters: Optional[MutableMapping[str, Any]] = None
) -> DocumentsResults:
"""Get a set of documents from the index.

Parameters
Expand Down Expand Up @@ -387,7 +389,7 @@ def get_documents(self, parameters: Optional[Dict[str, Any]] = None) -> Document

def add_documents(
self,
documents: List[Dict[str, Any]],
documents: Sequence[Mapping[str, Any]],
primary_key: Optional[str] = None,
) -> TaskInfo:
"""Add documents to the index.
Expand Down Expand Up @@ -416,7 +418,7 @@ def add_documents(

def add_documents_in_batches(
self,
documents: List[Dict[str, Any]],
documents: Sequence[Mapping[str, Any]],
batch_size: int = 1000,
primary_key: Optional[str] = None,
) -> List[TaskInfo]:
Expand Down Expand Up @@ -573,7 +575,7 @@ def add_documents_raw(
return TaskInfo(**response)

def update_documents(
self, documents: List[Dict[str, Any]], primary_key: Optional[str] = None
self, documents: Sequence[Mapping[str, Any]], primary_key: Optional[str] = None
) -> TaskInfo:
"""Update documents in the index.

Expand Down Expand Up @@ -721,7 +723,7 @@ def update_documents_raw(

def update_documents_in_batches(
self,
documents: List[Dict[str, Any]],
documents: Sequence[Mapping[str, Any]],
batch_size: int = 1000,
primary_key: Optional[str] = None,
) -> List[TaskInfo]:
Expand Down Expand Up @@ -865,7 +867,7 @@ def get_settings(self) -> Dict[str, Any]:
"""
return self.http.get(f"{self.config.paths.index}/{self.uid}/{self.config.paths.setting}")

def update_settings(self, body: Dict[str, Any]) -> TaskInfo:
def update_settings(self, body: Mapping[str, Any]) -> TaskInfo:
"""Update settings of the index.

https://www.meilisearch.com/docs/reference/api/settings#update-settings
Expand Down Expand Up @@ -1413,7 +1415,7 @@ def get_typo_tolerance(self) -> TypoTolerance:

return TypoTolerance(**typo_tolerance)

def update_typo_tolerance(self, body: Union[Dict[str, Any], None]) -> TaskInfo:
def update_typo_tolerance(self, body: Union[Mapping[str, Any], None]) -> TaskInfo:
"""Update typo tolerance of the index.

Parameters
Expand Down Expand Up @@ -1535,7 +1537,7 @@ def get_faceting_settings(self) -> Faceting:

return Faceting(**faceting)

def update_faceting_settings(self, body: Union[Dict[str, Any], None]) -> TaskInfo:
def update_faceting_settings(self, body: Union[Mapping[str, Any], None]) -> TaskInfo:
"""Update the faceting settings of the index.

Parameters
Expand Down Expand Up @@ -1757,8 +1759,8 @@ def reset_non_separator_tokens(self) -> TaskInfo:

@staticmethod
def _batch(
documents: List[Dict[str, Any]], batch_size: int
) -> Generator[List[Dict[str, Any]], None, None]:
documents: Sequence[Mapping[str, Any]], batch_size: int
) -> Generator[Sequence[Mapping[str, Any]], None, None]:
total_len = len(documents)
for i in range(0, total_len, batch_size):
yield documents[i : i + batch_size]
Expand Down
8 changes: 4 additions & 4 deletions meilisearch/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from datetime import datetime
from time import sleep
from typing import Any, Dict, Optional
from typing import Any, MutableMapping, Optional
from urllib import parse

from meilisearch._httprequests import HttpRequests
Expand All @@ -27,7 +27,7 @@ def __init__(self, config: Config):
self.config = config
self.http = HttpRequests(config)

def get_tasks(self, parameters: Optional[Dict[str, Any]] = None) -> TaskResults:
def get_tasks(self, parameters: Optional[MutableMapping[str, Any]] = None) -> TaskResults:
"""Get all tasks.

Parameters
Expand Down Expand Up @@ -75,7 +75,7 @@ def get_task(self, uid: int) -> Task:
task = self.http.get(f"{self.config.paths.task}/{uid}")
return Task(**task)

def cancel_tasks(self, parameters: Dict[str, Any]) -> TaskInfo:
def cancel_tasks(self, parameters: MutableMapping[str, Any]) -> TaskInfo:
"""Cancel a list of enqueued or processing tasks.

Parameters
Expand All @@ -100,7 +100,7 @@ def cancel_tasks(self, parameters: Dict[str, Any]) -> TaskInfo:
response = self.http.post(f"{self.config.paths.task}/cancel?{parse.urlencode(parameters)}")
return TaskInfo(**response)

def delete_tasks(self, parameters: Dict[str, Any]) -> TaskInfo:
def delete_tasks(self, parameters: MutableMapping[str, Any]) -> TaskInfo:
"""Delete a list of enqueued or processing tasks.
Parameters
----------
Expand Down