Skip to content

Commit 305b32b

Browse files
committed
Apply suggestions from code review
- wrapping localized_attributes in a model - using Mapping instead of Dict
1 parent 93d0d72 commit 305b32b

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

meilisearch/index.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
Faceting,
3030
HuggingFaceEmbedder,
3131
IndexStats,
32+
LocalizedAttributes,
3233
OpenAiEmbedder,
3334
Pagination,
3435
ProximityPrecision,
@@ -2044,8 +2045,8 @@ def reset_proximity_precision(self) -> TaskInfo:
20442045

20452046
# LOCALIZED ATTRIBUTES SETTINGS
20462047

2047-
def get_localized_attributes(self) -> Union[List[Dict[str, List[str]]], None]:
2048-
"""Get the proximity_precision of the index.
2048+
def get_localized_attributes(self) -> Union[List[LocalizedAttributes], None]:
2049+
"""Get the localized_attributes of the index.
20492050
20502051
Returns
20512052
-------
@@ -2057,10 +2058,15 @@ def get_localized_attributes(self) -> Union[List[Dict[str, List[str]]], None]:
20572058
MeilisearchApiError
20582059
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
20592060
"""
2060-
return self.http.get(self.__settings_url_for(self.config.paths.localized_attributes))
2061+
response = self.http.get(self.__settings_url_for(self.config.paths.localized_attributes))
2062+
2063+
if not response:
2064+
return None
2065+
2066+
return [LocalizedAttributes(**attrs) for attrs in response]
20612067

20622068
def update_localized_attributes(
2063-
self, body: Union[List[Dict[str, List[str]]], None]
2069+
self, body: Union[List[Mapping[str, List[str]]], None]
20642070
) -> TaskInfo:
20652071
"""Update the localized_attributes of the index.
20662072

meilisearch/models/index.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ class ProximityPrecision(str, Enum):
5454
BY_ATTRIBUTE = "byAttribute"
5555

5656

57+
class LocalizedAttributes(CamelBase):
58+
attribute_patterns: List[str]
59+
locales: List[str]
60+
61+
5762
class OpenAiEmbedder(CamelBase):
5863
source: str = "openAi"
5964
model: Optional[str] = None # Defaults to text-embedding-3-small

tests/settings/test_settings_localized_attributes_meilisearch.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
from typing import List
2+
3+
from meilisearch.models.index import LocalizedAttributes
4+
15
NEW_LOCALIZED_ATTRIBUTES = [{"attributePatterns": ["title"], "locales": ["eng"]}]
26

37

8+
def unpack_loc_attrs_response(response: List[LocalizedAttributes]):
9+
return [loc_attrs.model_dump(by_alias=True) for loc_attrs in response]
10+
11+
412
def test_get_localized_attributes(empty_index):
513
"""Tests getting default localized_attributes."""
614
response = empty_index().get_localized_attributes()
@@ -14,7 +22,7 @@ def test_update_localized_attributes(empty_index):
1422
update = index.wait_for_task(response.task_uid)
1523
assert update.status == "succeeded"
1624
response = index.get_localized_attributes()
17-
assert NEW_LOCALIZED_ATTRIBUTES == response
25+
assert NEW_LOCALIZED_ATTRIBUTES == unpack_loc_attrs_response(response)
1826

1927

2028
def test_reset_localized_attributes(empty_index):
@@ -26,7 +34,7 @@ def test_reset_localized_attributes(empty_index):
2634
assert update.status == "succeeded"
2735
# Check the settings have been correctly updated
2836
response = index.get_localized_attributes()
29-
assert NEW_LOCALIZED_ATTRIBUTES == response
37+
assert NEW_LOCALIZED_ATTRIBUTES == unpack_loc_attrs_response(response)
3038
# Check the reset of the settings
3139
response = index.reset_localized_attributes()
3240
update = index.wait_for_task(response.task_uid)

0 commit comments

Comments
 (0)