Skip to content

Commit 2d66ea4

Browse files
typing of the meta attribute of hits
1 parent 18e596e commit 2d66ea4

File tree

8 files changed

+22
-12
lines changed

8 files changed

+22
-12
lines changed

elasticsearch_dsl/utils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
Iterable,
2929
Iterator,
3030
List,
31+
Mapping,
3132
Optional,
3233
Tuple,
3334
Type,
@@ -48,6 +49,7 @@
4849
from .field import Field
4950
from .index_base import IndexBase
5051
from .response import Hit # noqa: F401
52+
from .types import Hit as HitBaseType
5153

5254
UsingType: TypeAlias = Union[str, "Elasticsearch"]
5355
AsyncUsingType: TypeAlias = Union[str, "AsyncElasticsearch"]
@@ -468,7 +470,15 @@ def _clone(self) -> Self:
468470
return c
469471

470472

471-
class HitMeta(AttrDict[Any]):
473+
if TYPE_CHECKING:
474+
HitMetaBase = HitBaseType
475+
else:
476+
HitMetaBase = AttrDict[Any]
477+
478+
479+
class HitMeta(HitMetaBase):
480+
inner_hits: Mapping[str, Any]
481+
472482
def __init__(
473483
self,
474484
document: Dict[str, Any],

examples/async/parent_child.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ async def get_answers(self) -> List[Any]:
190190
elasticsearch.
191191
"""
192192
if "inner_hits" in self.meta and "answer" in self.meta.inner_hits:
193-
return cast(List[Any], self.meta.inner_hits.answer.hits)
193+
return cast(List[Any], self.meta.inner_hits["answer"].hits)
194194
return [a async for a in self.search_answers()]
195195

196196
async def save(self, **kwargs: Any) -> None: # type: ignore[override]

examples/async/sparse_vectors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ async def main() -> None:
186186
)
187187
print(f"Summary: {hit.summary}")
188188
if args.show_inner_hits:
189-
for passage in hit.meta.inner_hits.passages:
189+
for passage in hit.meta.inner_hits["passages"]:
190190
print(f" - [Score: {passage.meta.score}] {passage.content!r}")
191191
print("")
192192

examples/async/vectors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ async def main() -> None:
175175
)
176176
print(f"Summary: {hit.summary}")
177177
if args.show_inner_hits:
178-
for passage in hit.meta.inner_hits.passages:
178+
for passage in hit.meta.inner_hits["passages"]:
179179
print(f" - [Score: {passage.meta.score}] {passage.content!r}")
180180
print("")
181181

examples/parent_child.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def get_answers(self) -> List[Any]:
189189
elasticsearch.
190190
"""
191191
if "inner_hits" in self.meta and "answer" in self.meta.inner_hits:
192-
return cast(List[Any], self.meta.inner_hits.answer.hits)
192+
return cast(List[Any], self.meta.inner_hits["answer"].hits)
193193
return [a for a in self.search_answers()]
194194

195195
def save(self, **kwargs: Any) -> None: # type: ignore[override]

examples/sparse_vectors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def main() -> None:
185185
)
186186
print(f"Summary: {hit.summary}")
187187
if args.show_inner_hits:
188-
for passage in hit.meta.inner_hits.passages:
188+
for passage in hit.meta.inner_hits["passages"]:
189189
print(f" - [Score: {passage.meta.score}] {passage.content!r}")
190190
print("")
191191

examples/vectors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def main() -> None:
174174
)
175175
print(f"Summary: {hit.summary}")
176176
if args.show_inner_hits:
177-
for passage in hit.meta.inner_hits.passages:
177+
for passage in hit.meta.inner_hits["passages"]:
178178
print(f" - [Score: {passage.meta.score}] {passage.content!r}")
179179
print("")
180180

utils/generator.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -702,12 +702,12 @@ def interface_to_python_class(
702702
# types via generics, each in array or object configurations.
703703
# Typing this attribute proved very difficult. A solution
704704
# that worked with mypy and pyright is to type "buckets"
705-
# with the array (list) form, and create a `buckets_as_dict`
706-
# property that is typed appropriate for accessing the
707-
# buckets when in object (dictionary) form.
705+
# for the list form, and create a `buckets_as_dict`
706+
# property that is typed appropriately for accessing the
707+
# buckets in dictionary form.
708708
# The generic type is assumed to be the first in the list,
709-
# which is a simplification that should be removed when a
710-
# more complete implementation of generic is added.
709+
# which is a simplification that should be improved when a
710+
# more complete implementation of generics is added.
711711
if generics[0]["type"]["name"] == "Void":
712712
generic_type = "Any"
713713
else:

0 commit comments

Comments
 (0)