Skip to content

Commit 8307786

Browse files
bors[bot]paul
andauthored
Merge #625
625: Change typing using the typing lib r=alallema a=pbrochar # Pull Request ## Related issue Fixes #611 ## What does this PR do? - Change typing for more coherence in the definition of functions ## PR checklist Please check if your PR fulfills the following requirements: - [X] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [X] Have you read the contributing guidelines? - [X] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: paul <[email protected]>
2 parents 1c8a8d6 + 679f150 commit 8307786

File tree

8 files changed

+134
-126
lines changed

8 files changed

+134
-126
lines changed

meilisearch/_httprequests.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import json
4-
from typing import Any, Callable
4+
from typing import Any, Callable, Dict, List, Optional, Union
55

66
import requests
77

@@ -26,8 +26,8 @@ def send_request(
2626
self,
2727
http_method: Callable,
2828
path: str,
29-
body: dict[str, Any] | list[dict[str, Any]] | list[str] | str | None = None,
30-
content_type: str | None = None,
29+
body: Optional[Union[Dict[str, Any], List[Dict[str, Any]], List[str], str]] = None,
30+
content_type: Optional[str] = None,
3131
) -> Any:
3232
if content_type:
3333
self.headers["Content-Type"] = content_type
@@ -60,31 +60,31 @@ def get(self, path: str) -> Any:
6060
def post(
6161
self,
6262
path: str,
63-
body: dict[str, Any] | list[dict[str, Any]] | list[str] | str | None = None,
64-
content_type: str | None = "application/json",
63+
body: Optional[Union[Dict[str, Any], List[Dict[str, Any]], List[str], str]] = None,
64+
content_type: Optional[str] = "application/json",
6565
) -> Any:
6666
return self.send_request(requests.post, path, body, content_type)
6767

6868
def patch(
6969
self,
7070
path: str,
71-
body: dict[str, Any] | list[dict[str, Any]] | list[str] | str | None = None,
72-
content_type: str | None = "application/json",
71+
body: Optional[Union[Dict[str, Any], List[Dict[str, Any]], List[str], str]] = None,
72+
content_type: Optional[str] = "application/json",
7373
) -> Any:
7474
return self.send_request(requests.patch, path, body, content_type)
7575

7676
def put(
7777
self,
7878
path: str,
79-
body: dict[str, Any] | list[dict[str, Any]] | list[str] | str | None = None,
80-
content_type: str | None = "application/json",
79+
body: Optional[Union[Dict[str, Any], List[Dict[str, Any]], List[str], str]] = None,
80+
content_type: Optional[str] = "application/json",
8181
) -> Any:
8282
return self.send_request(requests.put, path, body, content_type)
8383

8484
def delete(
8585
self,
8686
path: str,
87-
body: dict[str, Any] | list[dict[str, Any]] | list[str] | None = None,
87+
body: Optional[Union[Dict[str, Any], List[Dict[str, Any]], List[str]]] = None,
8888
) -> Any:
8989
return self.send_request(requests.delete, path, body)
9090

meilisearch/client.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import hmac
77
import json
88
import re
9-
from typing import Any
9+
from typing import Any, Dict, List, Optional, Union
1010
from urllib import parse
1111

1212
from meilisearch._httprequests import HttpRequests
@@ -25,7 +25,9 @@ class Client:
2525
Meilisearch and its permissions.
2626
"""
2727

28-
def __init__(self, url: str, api_key: str | None = None, timeout: int | None = None) -> None:
28+
def __init__(
29+
self, url: str, api_key: Optional[str] = None, timeout: Optional[int] = None
30+
) -> None:
2931
"""
3032
Parameters
3133
----------
@@ -38,7 +40,7 @@ def __init__(self, url: str, api_key: str | None = None, timeout: int | None = N
3840

3941
self.http = HttpRequests(self.config)
4042

41-
def create_index(self, uid: str, options: dict[str, Any] | None = None) -> dict[str, Any]:
43+
def create_index(self, uid: str, options: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
4244
"""Create an index.
4345
4446
Parameters
@@ -61,7 +63,7 @@ def create_index(self, uid: str, options: dict[str, Any] | None = None) -> dict[
6163
"""
6264
return Index.create(self.config, uid, options)
6365

64-
def delete_index(self, uid: str) -> dict[str, Any]:
66+
def delete_index(self, uid: str) -> Dict[str, Any]:
6567
"""Deletes an index
6668
6769
Parameters
@@ -83,7 +85,7 @@ def delete_index(self, uid: str) -> dict[str, Any]:
8385

8486
return self.http.delete(f"{self.config.paths.index}/{uid}")
8587

86-
def get_indexes(self, parameters: dict[str, Any] | None = None) -> dict[str, list[Index]]:
88+
def get_indexes(self, parameters: Optional[Dict[str, Any]] = None) -> Dict[str, List[Index]]:
8789
"""Get all indexes.
8890
8991
Parameters
@@ -116,7 +118,7 @@ def get_indexes(self, parameters: dict[str, Any] | None = None) -> dict[str, lis
116118
]
117119
return response
118120

119-
def get_raw_indexes(self, parameters: dict[str, Any] | None = None) -> list[dict[str, Any]]:
121+
def get_raw_indexes(self, parameters: Optional[Dict[str, Any]] = None) -> List[Dict[str, Any]]:
120122
"""Get all indexes in dictionary format.
121123
122124
Parameters
@@ -159,7 +161,7 @@ def get_index(self, uid: str) -> Index:
159161
"""
160162
return Index(self.config, uid).fetch_info()
161163

162-
def get_raw_index(self, uid: str) -> dict[str, Any]:
164+
def get_raw_index(self, uid: str) -> Dict[str, Any]:
163165
"""Get the index as a dictionary.
164166
This index should already exist.
165167
@@ -198,7 +200,7 @@ def index(self, uid: str) -> Index:
198200
return Index(self.config, uid=uid)
199201
raise Exception("The index UID should not be None")
200202

201-
def get_all_stats(self) -> dict[str, Any]:
203+
def get_all_stats(self) -> Dict[str, Any]:
202204
"""Get all stats of Meilisearch
203205
204206
Get information about database size and all indexes
@@ -216,7 +218,7 @@ def get_all_stats(self) -> dict[str, Any]:
216218
"""
217219
return self.http.get(self.config.paths.stat)
218220

219-
def health(self) -> dict[str, str]:
221+
def health(self) -> Dict[str, str]:
220222
"""Get health of the Meilisearch server.
221223
222224
Returns
@@ -239,7 +241,7 @@ def is_healthy(self) -> bool:
239241
return False
240242
return True
241243

242-
def get_key(self, key_or_uid: str) -> dict[str, Any]:
244+
def get_key(self, key_or_uid: str) -> Dict[str, Any]:
243245
"""Gets information about a specific API key.
244246
245247
Parameters
@@ -260,7 +262,7 @@ def get_key(self, key_or_uid: str) -> dict[str, Any]:
260262
"""
261263
return self.http.get(f"{self.config.paths.keys}/{key_or_uid}")
262264

263-
def get_keys(self, parameters: dict[str, Any] | None = None) -> dict[str, Any]:
265+
def get_keys(self, parameters: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
264266
"""Gets the Meilisearch API keys.
265267
266268
Parameters
@@ -283,7 +285,7 @@ def get_keys(self, parameters: dict[str, Any] | None = None) -> dict[str, Any]:
283285
parameters = {}
284286
return self.http.get(f"{self.config.paths.keys}?{parse.urlencode(parameters)}")
285287

286-
def create_key(self, options: dict[str, Any]) -> dict[str, Any]:
288+
def create_key(self, options: Dict[str, Any]) -> Dict[str, Any]:
287289
"""Creates a new API key.
288290
289291
Parameters
@@ -308,7 +310,7 @@ def create_key(self, options: dict[str, Any]) -> dict[str, Any]:
308310
"""
309311
return self.http.post(f"{self.config.paths.keys}", options)
310312

311-
def update_key(self, key_or_uid: str, options: dict[str, Any]) -> dict[str, Any]:
313+
def update_key(self, key_or_uid: str, options: Dict[str, Any]) -> Dict[str, Any]:
312314
"""Update an API key.
313315
314316
Parameters
@@ -333,7 +335,7 @@ def update_key(self, key_or_uid: str, options: dict[str, Any]) -> dict[str, Any]
333335
url = f"{self.config.paths.keys}/{key_or_uid}"
334336
return self.http.patch(url, options)
335337

336-
def delete_key(self, key_or_uid: str) -> dict[str, int]:
338+
def delete_key(self, key_or_uid: str) -> Dict[str, int]:
337339
"""Deletes an API key.
338340
339341
Parameters
@@ -354,7 +356,7 @@ def delete_key(self, key_or_uid: str) -> dict[str, int]:
354356
"""
355357
return self.http.delete(f"{self.config.paths.keys}/{key_or_uid}")
356358

357-
def get_version(self) -> dict[str, str]:
359+
def get_version(self) -> Dict[str, str]:
358360
"""Get version Meilisearch
359361
360362
Returns
@@ -369,7 +371,7 @@ def get_version(self) -> dict[str, str]:
369371
"""
370372
return self.http.get(self.config.paths.version)
371373

372-
def version(self) -> dict[str, str]:
374+
def version(self) -> Dict[str, str]:
373375
"""Alias for get_version
374376
375377
Returns
@@ -384,7 +386,7 @@ def version(self) -> dict[str, str]:
384386
"""
385387
return self.get_version()
386388

387-
def create_dump(self) -> dict[str, str]:
389+
def create_dump(self) -> Dict[str, str]:
388390
"""Trigger the creation of a Meilisearch dump.
389391
390392
Returns
@@ -400,7 +402,7 @@ def create_dump(self) -> dict[str, str]:
400402
"""
401403
return self.http.post(self.config.paths.dumps)
402404

403-
def swap_indexes(self, parameters: list[dict[str, list[str]]]) -> TaskInfo:
405+
def swap_indexes(self, parameters: List[Dict[str, List[str]]]) -> TaskInfo:
404406
"""Swap two indexes.
405407
406408
Parameters
@@ -422,8 +424,8 @@ def swap_indexes(self, parameters: list[dict[str, list[str]]]) -> TaskInfo:
422424
return TaskInfo(**self.http.post(self.config.paths.swap, parameters))
423425

424426
def get_tasks(
425-
self, parameters: dict[str, Any] | None = None
426-
) -> dict[str, list[dict[str, Any]]]:
427+
self, parameters: Optional[Dict[str, Any]] = None
428+
) -> Dict[str, List[Dict[str, Any]]]:
427429
"""Get all tasks.
428430
429431
Parameters
@@ -443,7 +445,7 @@ def get_tasks(
443445
"""
444446
return get_tasks(self.config, parameters=parameters)
445447

446-
def get_task(self, uid: int) -> dict[str, Any]:
448+
def get_task(self, uid: int) -> Dict[str, Any]:
447449
"""Get one task.
448450
449451
Parameters
@@ -463,7 +465,7 @@ def get_task(self, uid: int) -> dict[str, Any]:
463465
"""
464466
return get_task(self.config, uid)
465467

466-
def cancel_tasks(self, parameters: dict[str, Any]) -> TaskInfo:
468+
def cancel_tasks(self, parameters: Dict[str, Any]) -> TaskInfo:
467469
"""Cancel a list of enqueued or processing tasks.
468470
469471
Parameters
@@ -484,7 +486,7 @@ def cancel_tasks(self, parameters: dict[str, Any]) -> TaskInfo:
484486
"""
485487
return cancel_tasks(self.config, parameters=parameters)
486488

487-
def delete_tasks(self, parameters: dict[str, Any]) -> TaskInfo:
489+
def delete_tasks(self, parameters: Dict[str, Any]) -> TaskInfo:
488490
"""Delete a list of finished tasks.
489491
490492
Parameters
@@ -508,7 +510,7 @@ def wait_for_task(
508510
uid: int,
509511
timeout_in_ms: int = 5000,
510512
interval_in_ms: int = 50,
511-
) -> dict[str, Any]:
513+
) -> Dict[str, Any]:
512514
"""Wait until Meilisearch processes a task until it fails or succeeds.
513515
514516
Parameters
@@ -535,10 +537,10 @@ def wait_for_task(
535537
def generate_tenant_token(
536538
self,
537539
api_key_uid: str,
538-
search_rules: dict[str, Any] | list[str],
540+
search_rules: Union[Dict[str, Any], List[str]],
539541
*,
540-
expires_at: datetime.datetime | None = None,
541-
api_key: str | None = None,
542+
expires_at: Optional[datetime.datetime] = None,
543+
api_key: Optional[str] = None,
542544
) -> str:
543545
"""Generate a JWT token for the use of multitenancy.
544546

meilisearch/config.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
from typing import Optional
4+
35

46
class Config:
57
"""
@@ -31,7 +33,9 @@ class Paths:
3133
faceting = "faceting"
3234
swap = "swap-indexes"
3335

34-
def __init__(self, url: str, api_key: str | None = None, timeout: int | None = None) -> None:
36+
def __init__(
37+
self, url: str, api_key: Optional[str] = None, timeout: Optional[int] = None
38+
) -> None:
3539
"""
3640
Parameters
3741
----------

0 commit comments

Comments
 (0)