Skip to content

Commit b218090

Browse files
more minor adjustments to response types
1 parent 099a52f commit b218090

File tree

6 files changed

+17
-16
lines changed

6 files changed

+17
-16
lines changed

elasticsearch_dsl/response/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,14 @@
4040
from ..search_base import Request, SearchBase
4141
from ..update_by_query_base import UpdateByQueryBase
4242

43-
__all__ = ["Response", "AggResponse", "UpdateByQueryResponse", "Hit", "HitMeta"]
43+
__all__ = [
44+
"Response",
45+
"AggResponse",
46+
"UpdateByQueryResponse",
47+
"Hit",
48+
"HitMeta",
49+
"AggregateResponseType",
50+
]
4451

4552

4653
class Response(AttrDict[Any], Generic[_R]):

examples/async/composite_agg.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717

1818
import asyncio
1919
import os
20-
from typing import Any, AsyncIterator, Dict, Mapping, Sequence
20+
from typing import Any, AsyncIterator, Dict, Mapping, Sequence, cast
2121

2222
from elasticsearch.helpers import async_bulk
2323

2424
from elasticsearch_dsl import Agg, AsyncSearch, Response, aggs, async_connections
25+
from elasticsearch_dsl.types import CompositeAggregate
2526
from tests.test_integration.test_data import DATA, GIT_INDEX
2627

2728

@@ -30,7 +31,7 @@ async def scan_aggs(
3031
source_aggs: Sequence[Mapping[str, Agg]],
3132
inner_aggs: Dict[str, Agg] = {},
3233
size: int = 10,
33-
) -> AsyncIterator[Any]:
34+
) -> AsyncIterator[CompositeAggregate]:
3435
"""
3536
Helper function used to iterate over all possible bucket combinations of
3637
``source_aggs``, returning results of ``inner_aggs`` for each. Uses the
@@ -54,7 +55,7 @@ async def run_search(**kwargs: Any) -> Response:
5455
response = await run_search()
5556
while response.aggregations["comp"].buckets:
5657
for b in response.aggregations["comp"].buckets:
57-
yield b
58+
yield cast(CompositeAggregate, b)
5859
if "after_key" in response.aggregations["comp"]:
5960
after = response.aggregations["comp"].after_key
6061
else:

examples/async/parent_child.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646

4747
from elasticsearch_dsl import (
4848
AsyncDocument,
49-
AsyncIndex,
5049
AsyncSearch,
5150
Date,
5251
InnerDoc,
@@ -92,7 +91,6 @@ class Post(AsyncDocument):
9291
# definitions here help type checkers understand additional arguments
9392
# that are allowed in the constructor
9493
_routing: str = mapped_field(default=None)
95-
_index: AsyncIndex = mapped_field(default=None)
9694
_id: Optional[int] = mapped_field(default=None)
9795

9896
created: Optional[datetime] = mapped_field(default=None)
@@ -161,8 +159,6 @@ async def add_answer(
161159
answer = Answer(
162160
# required make sure the answer is stored in the same shard
163161
_routing=self.meta.id,
164-
# since we don't have explicit index, ensure same index as self
165-
_index=self.meta.index,
166162
# set up the parent/child mapping
167163
question_answer={"name": "answer", "parent": self.meta.id},
168164
# pass in the field values

examples/composite_agg.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
# under the License.
1717

1818
import os
19-
from typing import Any, Dict, Iterator, Mapping, Sequence
19+
from typing import Any, Dict, Iterator, Mapping, Sequence, cast
2020

2121
from elasticsearch.helpers import bulk
2222

2323
from elasticsearch_dsl import Agg, Response, Search, aggs, connections
24+
from elasticsearch_dsl.types import CompositeAggregate
2425
from tests.test_integration.test_data import DATA, GIT_INDEX
2526

2627

@@ -29,7 +30,7 @@ def scan_aggs(
2930
source_aggs: Sequence[Mapping[str, Agg]],
3031
inner_aggs: Dict[str, Agg] = {},
3132
size: int = 10,
32-
) -> Iterator[Any]:
33+
) -> Iterator[CompositeAggregate]:
3334
"""
3435
Helper function used to iterate over all possible bucket combinations of
3536
``source_aggs``, returning results of ``inner_aggs`` for each. Uses the
@@ -53,7 +54,7 @@ def run_search(**kwargs: Any) -> Response:
5354
response = run_search()
5455
while response.aggregations["comp"].buckets:
5556
for b in response.aggregations["comp"].buckets:
56-
yield b
57+
yield cast(CompositeAggregate, b)
5758
if "after_key" in response.aggregations["comp"]:
5859
after = response.aggregations["comp"].after_key
5960
else:

examples/parent_child.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
from elasticsearch_dsl import (
4747
Date,
4848
Document,
49-
Index,
5049
InnerDoc,
5150
Join,
5251
Keyword,
@@ -91,7 +90,6 @@ class Post(Document):
9190
# definitions here help type checkers understand additional arguments
9291
# that are allowed in the constructor
9392
_routing: str = mapped_field(default=None)
94-
_index: Index = mapped_field(default=None)
9593
_id: Optional[int] = mapped_field(default=None)
9694

9795
created: Optional[datetime] = mapped_field(default=None)
@@ -160,8 +158,6 @@ def add_answer(
160158
answer = Answer(
161159
# required make sure the answer is stored in the same shard
162160
_routing=self.meta.id,
163-
# since we don't have explicit index, ensure same index as self
164-
_index=self.meta.index,
165161
# set up the parent/child mapping
166162
question_answer={"name": "answer", "parent": self.meta.id},
167163
# pass in the field values

utils/templates/response.__init__.py.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ if TYPE_CHECKING:
4040
from ..update_by_query_base import UpdateByQueryBase
4141
from .. import types
4242

43-
__all__ = ["Response", "AggResponse", "UpdateByQueryResponse", "Hit", "HitMeta"]
43+
__all__ = ["Response", "AggResponse", "UpdateByQueryResponse", "Hit", "HitMeta", "AggregateResponseType"]
4444

4545

4646
class Response(AttrDict[Any], Generic[_R]):

0 commit comments

Comments
 (0)