Skip to content

Commit db05110

Browse files
authored
fix(python): remove warnings for HighlightResult and SnippetResult (#5284)
1 parent 92ebb0f commit db05110

File tree

5 files changed

+71
-35
lines changed

5 files changed

+71
-35
lines changed

playground/python/app/search.py

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,47 @@
1111
def main():
1212
print("SearchClient version", __version__)
1313

14-
# client = SearchClientSync(
14+
client = SearchClientSync(
15+
environ.get("ALGOLIA_APPLICATION_ID"), environ.get("ALGOLIA_ADMIN_KEY")
16+
)
17+
client.add_user_agent("playground")
18+
client.add_user_agent("bar", "baz")
19+
20+
print("user_agent", client._config._user_agent.get())
21+
print("client initialized", client)
22+
23+
try:
24+
resp = client.search_single_index("poussing-RECORDS")
25+
print(resp.to_dict())
26+
finally:
27+
client.close()
28+
29+
print("client closed")
30+
31+
# print("with transformations")
32+
#
33+
# config = SearchConfig(
1534
# environ.get("ALGOLIA_APPLICATION_ID"), environ.get("ALGOLIA_ADMIN_KEY")
1635
# )
17-
# client.add_user_agent("playground")
18-
# client.add_user_agent("bar", "baz")
36+
#
37+
# config.set_transformation_region("eu")
38+
#
39+
# print("region in playground")
40+
# print(config.region)
41+
#
42+
# client = SearchClientSync.create_with_config(config)
43+
# client.add_user_agent("playground search with ingestion")
1944
#
2045
# print("user_agent", client._config._user_agent.get())
21-
# print("client initialized", client)
2246
#
2347
# try:
24-
# resp = client.search_synonyms("foo")
48+
# resp = client.replace_all_objects_with_transformation(
49+
# "boyd", [{"objectID": "bar"},{"objectID": "bar"},{"objectID": "bar"},{"objectID": "bar"},{"objectID": "bar"}], 2
50+
# )
2551
# print(resp)
26-
# client.browse_synonyms("foo", lambda _resp: print(_resp))
52+
# except Exception as e:
53+
# print(e)
2754
# finally:
2855
# client.close()
2956
#
3057
# print("client closed")
31-
#
32-
# print("with transformations")
33-
#
34-
config = SearchConfig(
35-
environ.get("ALGOLIA_APPLICATION_ID"), environ.get("ALGOLIA_ADMIN_KEY")
36-
)
37-
38-
config.set_transformation_region("eu")
39-
40-
print("region in playground")
41-
print(config.region)
42-
43-
client = SearchClientSync.create_with_config(config)
44-
client.add_user_agent("playground search with ingestion")
45-
46-
print("user_agent", client._config._user_agent.get())
47-
48-
try:
49-
resp = client.replace_all_objects_with_transformation(
50-
"boyd", [{"objectID": "bar"},{"objectID": "bar"},{"objectID": "bar"},{"objectID": "bar"},{"objectID": "bar"}], 2
51-
)
52-
print(resp)
53-
except Exception as e:
54-
print(e)
55-
finally:
56-
client.close()
57-
58-
print("client closed")

playground/python/poetry.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

specs/search/common/schemas/Hit.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ hit:
55
66
A hit is a record from your index, augmented with special attributes for highlighting, snippeting, and ranking.
77
x-is-generic: true
8+
x-is-hit-object: true
89
additionalProperties: true
910
required:
1011
- objectID

templates/python/imports.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ from pydantic import (
2424
StrictStr,
2525
ValidationError,
2626
field_validator,
27+
field_serializer,
2728
model_serializer,
2829
)
2930
from typing import (

templates/python/model_generic.mustache

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,41 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
7979
extra='allow',
8080
)
8181

82+
{{#vendorExtensions.x-is-hit-object}}
83+
@staticmethod
84+
def __dump_item(item):
85+
return item.model_dump(
86+
by_alias=True,
87+
exclude_none=True,
88+
exclude_unset=True,
89+
warnings="none",
90+
) if isinstance(item, BaseModel) else item
91+
92+
@field_serializer("highlight_result")
93+
def serialize_highlight_result(self, v: HighlightResult | None):
94+
if v is None:
95+
return None
96+
97+
if isinstance(v, dict):
98+
return {k: self.__dump_item(val) for k, val in v.items()}
99+
elif isinstance(v, list):
100+
return [self.__dump_item(val) for val in v]
101+
else:
102+
return self.__dump_item(v)
103+
104+
@field_serializer("snippet_result")
105+
def serialize_snippet_result(self, v: SnippetResult | None):
106+
if v is None:
107+
return None
108+
109+
if isinstance(v, dict):
110+
return {k: self.__dump_item(val) for k, val in v.items()}
111+
elif isinstance(v, list):
112+
return [self.__dump_item(val) for val in v]
113+
else:
114+
return self.__dump_item(v)
115+
{{/vendorExtensions.x-is-hit-object}}
116+
82117
def to_json(self) -> str:
83118
return self.model_dump_json(by_alias=True, exclude_unset=True)
84119

0 commit comments

Comments
 (0)