Skip to content

Commit

Permalink
typing of the meta attribute of hits
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Oct 22, 2024
1 parent 18e596e commit 2d66ea4
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 12 deletions.
12 changes: 11 additions & 1 deletion elasticsearch_dsl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
Iterable,
Iterator,
List,
Mapping,
Optional,
Tuple,
Type,
Expand All @@ -48,6 +49,7 @@
from .field import Field
from .index_base import IndexBase
from .response import Hit # noqa: F401
from .types import Hit as HitBaseType

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


class HitMeta(AttrDict[Any]):
if TYPE_CHECKING:
HitMetaBase = HitBaseType
else:
HitMetaBase = AttrDict[Any]


class HitMeta(HitMetaBase):
inner_hits: Mapping[str, Any]

def __init__(
self,
document: Dict[str, Any],
Expand Down
2 changes: 1 addition & 1 deletion examples/async/parent_child.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ async def get_answers(self) -> List[Any]:
elasticsearch.
"""
if "inner_hits" in self.meta and "answer" in self.meta.inner_hits:
return cast(List[Any], self.meta.inner_hits.answer.hits)
return cast(List[Any], self.meta.inner_hits["answer"].hits)
return [a async for a in self.search_answers()]

async def save(self, **kwargs: Any) -> None: # type: ignore[override]
Expand Down
2 changes: 1 addition & 1 deletion examples/async/sparse_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ async def main() -> None:
)
print(f"Summary: {hit.summary}")
if args.show_inner_hits:
for passage in hit.meta.inner_hits.passages:
for passage in hit.meta.inner_hits["passages"]:
print(f" - [Score: {passage.meta.score}] {passage.content!r}")
print("")

Expand Down
2 changes: 1 addition & 1 deletion examples/async/vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ async def main() -> None:
)
print(f"Summary: {hit.summary}")
if args.show_inner_hits:
for passage in hit.meta.inner_hits.passages:
for passage in hit.meta.inner_hits["passages"]:
print(f" - [Score: {passage.meta.score}] {passage.content!r}")
print("")

Expand Down
2 changes: 1 addition & 1 deletion examples/parent_child.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def get_answers(self) -> List[Any]:
elasticsearch.
"""
if "inner_hits" in self.meta and "answer" in self.meta.inner_hits:
return cast(List[Any], self.meta.inner_hits.answer.hits)
return cast(List[Any], self.meta.inner_hits["answer"].hits)
return [a for a in self.search_answers()]

def save(self, **kwargs: Any) -> None: # type: ignore[override]
Expand Down
2 changes: 1 addition & 1 deletion examples/sparse_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def main() -> None:
)
print(f"Summary: {hit.summary}")
if args.show_inner_hits:
for passage in hit.meta.inner_hits.passages:
for passage in hit.meta.inner_hits["passages"]:
print(f" - [Score: {passage.meta.score}] {passage.content!r}")
print("")

Expand Down
2 changes: 1 addition & 1 deletion examples/vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def main() -> None:
)
print(f"Summary: {hit.summary}")
if args.show_inner_hits:
for passage in hit.meta.inner_hits.passages:
for passage in hit.meta.inner_hits["passages"]:
print(f" - [Score: {passage.meta.score}] {passage.content!r}")
print("")

Expand Down
10 changes: 5 additions & 5 deletions utils/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,12 +702,12 @@ def interface_to_python_class(
# types via generics, each in array or object configurations.
# Typing this attribute proved very difficult. A solution
# that worked with mypy and pyright is to type "buckets"
# with the array (list) form, and create a `buckets_as_dict`
# property that is typed appropriate for accessing the
# buckets when in object (dictionary) form.
# for the list form, and create a `buckets_as_dict`
# property that is typed appropriately for accessing the
# buckets in dictionary form.
# The generic type is assumed to be the first in the list,
# which is a simplification that should be removed when a
# more complete implementation of generic is added.
# which is a simplification that should be improved when a
# more complete implementation of generics is added.
if generics[0]["type"]["name"] == "Void":
generic_type = "Any"
else:
Expand Down

0 comments on commit 2d66ea4

Please sign in to comment.