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 099a52f
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 40 deletions.
56 changes: 28 additions & 28 deletions elasticsearch_dsl/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5078,49 +5078,49 @@ class HistogramBucket(AttrDict[Any]):

class Hit(AttrDict[Any]):
"""
:arg _index: (required)
:arg _id:
:arg _score:
:arg _explanation:
:arg index: (required)
:arg id:
:arg score:
:arg explanation:
:arg fields:
:arg highlight:
:arg inner_hits:
:arg matched_queries:
:arg _nested:
:arg _ignored:
:arg nested:
:arg ignored:
:arg ignored_field_values:
:arg _shard:
:arg _node:
:arg _routing:
:arg _source:
:arg _rank:
:arg _seq_no:
:arg _primary_term:
:arg _version:
:arg shard:
:arg node:
:arg routing:
:arg source:
:arg rank:
:arg seq_no:
:arg primary_term:
:arg version:
:arg sort:
"""

_index: str
_id: str
_score: Union[float, None]
_explanation: "Explanation"
index: str
id: str
score: Union[float, None]
explanation: "Explanation"
fields: Mapping[str, Any]
highlight: Mapping[str, Sequence[str]]
inner_hits: Mapping[str, "InnerHitsResult"]
matched_queries: Union[Sequence[str], Mapping[str, float]]
_nested: "NestedIdentity"
_ignored: Sequence[str]
nested: "NestedIdentity"
ignored: Sequence[str]
ignored_field_values: Mapping[
str, Sequence[Union[int, float, str, bool, None, Any]]
]
_shard: str
_node: str
_routing: str
_source: Any
_rank: int
_seq_no: int
_primary_term: int
_version: int
shard: str
node: str
routing: str
source: Any
rank: int
seq_no: int
primary_term: int
version: int
sort: Sequence[Union[int, float, str, bool, None, Any]]


Expand Down
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
15 changes: 10 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 All @@ -733,6 +733,11 @@ def interface_to_python_class(
)
k["buckets_as_dict"] = generic_type
else:
if interface == "Hit" and arg["name"].startswith("_"):
# Python DSL removes the undersore prefix from all the
# properties of the hit, so we do the same
arg["name"] = arg["name"][1:]

self.add_attribute(
k, arg, for_types_py=for_types_py, for_response=for_response
)
Expand Down

0 comments on commit 099a52f

Please sign in to comment.