From 4fdaec28aa9eb654053d6a9e414e8e4c05107a1b Mon Sep 17 00:00:00 2001 From: Quentin Pradet Date: Tue, 11 Feb 2025 16:52:37 +0400 Subject: [PATCH 1/2] Switch to Black 2025 and isort 6 (#2779) * Switch to Black 2025 code style * Run format with isort 6 (cherry picked from commit 68f80128b61a8162b58e3082e35564e7aa7b341a) --- elasticsearch/dsl/search_base.py | 6 +++--- noxfile.py | 8 +++++--- test_elasticsearch/test_dsl/test_result.py | 4 ++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/elasticsearch/dsl/search_base.py b/elasticsearch/dsl/search_base.py index ad4a56059..c513fc78d 100644 --- a/elasticsearch/dsl/search_base.py +++ b/elasticsearch/dsl/search_base.py @@ -698,12 +698,12 @@ def ensure_strings(fields: "InstrumentedField") -> str: ... @overload def ensure_strings( - fields: List[Union[str, "InstrumentedField"]] + fields: List[Union[str, "InstrumentedField"]], ) -> List[str]: ... @overload def ensure_strings( - fields: Dict[str, List[Union[str, "InstrumentedField"]]] + fields: Dict[str, List[Union[str, "InstrumentedField"]]], ) -> Dict[str, List[str]]: ... def ensure_strings( @@ -712,7 +712,7 @@ def ensure_strings( "InstrumentedField", List[Union[str, "InstrumentedField"]], Dict[str, List[Union[str, "InstrumentedField"]]], - ] + ], ) -> Union[str, List[str], Dict[str, List[str]]]: if isinstance(fields, dict): return {k: ensure_strings(v) for k, v in fields.items()} diff --git a/noxfile.py b/noxfile.py index 90172d49e..c9e961900 100644 --- a/noxfile.py +++ b/noxfile.py @@ -66,7 +66,9 @@ def test_otel(session): @nox.session() def format(session): - session.install(".", "black~=24.0", "isort", "flynt", "unasync>=0.6.0", "jinja2") + session.install( + ".", "black~=25.0", "isort~=6.0", "flynt", "unasync>=0.6.0", "jinja2" + ) session.run("python", "utils/run-unasync.py") session.run("python", "utils/run-unasync-dsl.py") @@ -88,9 +90,9 @@ def lint(session): session.install( "flake8", - "black~=24.0", + "black~=25.0", "mypy", - "isort", + "isort~=6.0", "types-requests", "types-python-dateutil", "unasync>=0.6.0", diff --git a/test_elasticsearch/test_dsl/test_result.py b/test_elasticsearch/test_dsl/test_result.py index 46707c715..2acd810d5 100644 --- a/test_elasticsearch/test_dsl/test_result.py +++ b/test_elasticsearch/test_dsl/test_result.py @@ -130,7 +130,7 @@ def test_iterating_over_response_gives_you_hits(dummy_response: Dict[str, Any]) def test_hits_get_wrapped_to_contain_additional_attrs( - dummy_response: Dict[str, Any] + dummy_response: Dict[str, Any], ) -> None: res = response.Response(Search(), dummy_response) hits = res.hits @@ -140,7 +140,7 @@ def test_hits_get_wrapped_to_contain_additional_attrs( def test_hits_provide_dot_and_bracket_access_to_attrs( - dummy_response: Dict[str, Any] + dummy_response: Dict[str, Any], ) -> None: res = response.Response(Search(), dummy_response) h = res.hits[0] From b15073811d48ac0d78aa7626d07994130375fe18 Mon Sep 17 00:00:00 2001 From: Quentin Pradet Date: Wed, 12 Feb 2025 19:21:28 +0400 Subject: [PATCH 2/2] Run nox -s format --- elasticsearch/dsl/_sync/search.py | 10 ++- elasticsearch/dsl/response/__init__.py | 68 ++++++++++++++----- elasticsearch/dsl/types.py | 8 +-- .../helpers/vectorstore/_sync/vectorstore.py | 5 +- examples/dsl/search_as_you_type.py | 7 +- .../test_dsl/_sync/test_index.py | 9 ++- .../test_dsl/_sync/test_search.py | 10 ++- .../test_integration/_sync/test_search.py | 10 ++- 8 files changed, 100 insertions(+), 27 deletions(-) diff --git a/elasticsearch/dsl/_sync/search.py b/elasticsearch/dsl/_sync/search.py index f46364a67..ae826a12f 100644 --- a/elasticsearch/dsl/_sync/search.py +++ b/elasticsearch/dsl/_sync/search.py @@ -16,7 +16,15 @@ # under the License. import contextlib -from typing import TYPE_CHECKING, Any, Dict, Iterator, List, Optional, cast +from typing import ( + TYPE_CHECKING, + Any, + Dict, + Iterator, + List, + Optional, + cast, +) from typing_extensions import Self diff --git a/elasticsearch/dsl/response/__init__.py b/elasticsearch/dsl/response/__init__.py index f6f3d551d..2ae863fff 100644 --- a/elasticsearch/dsl/response/__init__.py +++ b/elasticsearch/dsl/response/__init__.py @@ -53,9 +53,19 @@ class Response(AttrDict[Any], Generic[_R]): """An Elasticsearch search response. - :arg took: (required) - :arg timed_out: (required) - :arg _shards: (required) + :arg took: (required) The number of milliseconds it took Elasticsearch + to run the request. This value is calculated by measuring the time + elapsed between receipt of a request on the coordinating node and + the time at which the coordinating node is ready to send the + response. It includes: * Communication time between the + coordinating node and data nodes * Time the request spends in the + search thread pool, queued for execution * Actual run time It + does not include: * Time needed to send the request to + Elasticsearch * Time needed to serialize the JSON response * Time + needed to send the response to a client + :arg timed_out: (required) If `true`, the request timed out before + completion; returned results may be partial or empty. + :arg _shards: (required) A count of shards used for the request. :arg hits: search results :arg aggregations: aggregation results :arg _clusters: @@ -64,7 +74,11 @@ class Response(AttrDict[Any], Generic[_R]): :arg num_reduce_phases: :arg profile: :arg pit_id: - :arg _scroll_id: + :arg _scroll_id: The identifier for the search and its search context. + You can use this scroll ID with the scroll API to retrieve the + next batch of search results for the request. This property is + returned only if the `scroll` query parameter is specified in the + request. :arg suggest: :arg terminated_early: """ @@ -303,22 +317,42 @@ def __iter__(self) -> Iterator[AggregateResponseType]: # type: ignore[override] class UpdateByQueryResponse(AttrDict[Any], Generic[_R]): """An Elasticsearch update by query response. - :arg batches: - :arg failures: - :arg noops: - :arg deleted: - :arg requests_per_second: - :arg retries: + :arg batches: The number of scroll responses pulled back by the update + by query. + :arg failures: Array of failures if there were any unrecoverable + errors during the process. If this is non-empty then the request + ended because of those failures. Update by query is implemented + using batches. Any failure causes the entire process to end, but + all failures in the current batch are collected into the array. + You can use the `conflicts` option to prevent reindex from ending + when version conflicts occur. + :arg noops: The number of documents that were ignored because the + script used for the update by query returned a noop value for + `ctx.op`. + :arg deleted: The number of documents that were successfully deleted. + :arg requests_per_second: The number of requests per second + effectively run during the update by query. + :arg retries: The number of retries attempted by update by query. + `bulk` is the number of bulk actions retried. `search` is the + number of search actions retried. :arg task: - :arg timed_out: - :arg took: - :arg total: - :arg updated: - :arg version_conflicts: + :arg timed_out: If true, some requests timed out during the update by + query. + :arg took: The number of milliseconds from start to end of the whole + operation. + :arg total: The number of documents that were successfully processed. + :arg updated: The number of documents that were successfully updated. + :arg version_conflicts: The number of version conflicts that the + update by query hit. :arg throttled: - :arg throttled_millis: + :arg throttled_millis: The number of milliseconds the request slept to + conform to `requests_per_second`. :arg throttled_until: - :arg throttled_until_millis: + :arg throttled_until_millis: This field should always be equal to zero + in an _update_by_query response. It only has meaning when using + the task API, where it indicates the next time (in milliseconds + since epoch) a throttled request will be run again in order to + conform to `requests_per_second`. """ _search: "UpdateByQueryBase[_R]" diff --git a/elasticsearch/dsl/types.py b/elasticsearch/dsl/types.py index ce639c4ed..80332ce32 100644 --- a/elasticsearch/dsl/types.py +++ b/elasticsearch/dsl/types.py @@ -406,9 +406,9 @@ class FieldAndFormat(AttrDict[Any]): A reference to a field with formatting instructions on how to return the value - :arg field: (required) Wildcard pattern. The request returns values + :arg field: (required) A wildcard pattern. The request returns values for field names matching this pattern. - :arg format: Format in which the values are returned. + :arg format: The format in which the values are returned. :arg include_unmapped: """ @@ -5630,8 +5630,8 @@ class RateAggregate(AttrDict[Any]): class Retries(AttrDict[Any]): """ - :arg bulk: (required) - :arg search: (required) + :arg bulk: (required) The number of bulk actions retried. + :arg search: (required) The number of search actions retried. """ bulk: int diff --git a/elasticsearch/helpers/vectorstore/_sync/vectorstore.py b/elasticsearch/helpers/vectorstore/_sync/vectorstore.py index 3c4a0d51a..6a6a5ee2a 100644 --- a/elasticsearch/helpers/vectorstore/_sync/vectorstore.py +++ b/elasticsearch/helpers/vectorstore/_sync/vectorstore.py @@ -22,7 +22,10 @@ from elasticsearch import Elasticsearch from elasticsearch._version import __versionstr__ as lib_version from elasticsearch.helpers import BulkIndexError, bulk -from elasticsearch.helpers.vectorstore import EmbeddingService, RetrievalStrategy +from elasticsearch.helpers.vectorstore import ( + EmbeddingService, + RetrievalStrategy, +) from elasticsearch.helpers.vectorstore._utils import maximal_marginal_relevance logger = logging.getLogger(__name__) diff --git a/examples/dsl/search_as_you_type.py b/examples/dsl/search_as_you_type.py index c1ebc99a4..a70de8ccb 100644 --- a/examples/dsl/search_as_you_type.py +++ b/examples/dsl/search_as_you_type.py @@ -28,7 +28,12 @@ import os from typing import TYPE_CHECKING, Optional -from elasticsearch.dsl import Document, SearchAsYouType, connections, mapped_field +from elasticsearch.dsl import ( + Document, + SearchAsYouType, + connections, + mapped_field, +) from elasticsearch.dsl.query import MultiMatch diff --git a/test_elasticsearch/test_dsl/_sync/test_index.py b/test_elasticsearch/test_dsl/_sync/test_index.py index c6d1b7904..327efa047 100644 --- a/test_elasticsearch/test_dsl/_sync/test_index.py +++ b/test_elasticsearch/test_dsl/_sync/test_index.py @@ -22,7 +22,14 @@ import pytest from pytest import raises -from elasticsearch.dsl import Date, Document, Index, IndexTemplate, Text, analyzer +from elasticsearch.dsl import ( + Date, + Document, + Index, + IndexTemplate, + Text, + analyzer, +) class Post(Document): diff --git a/test_elasticsearch/test_dsl/_sync/test_search.py b/test_elasticsearch/test_dsl/_sync/test_search.py index 04b0ad53e..1fa7da1c7 100644 --- a/test_elasticsearch/test_dsl/_sync/test_search.py +++ b/test_elasticsearch/test_dsl/_sync/test_search.py @@ -21,7 +21,15 @@ import pytest from pytest import raises -from elasticsearch.dsl import Document, EmptySearch, Q, Search, query, types, wrappers +from elasticsearch.dsl import ( + Document, + EmptySearch, + Q, + Search, + query, + types, + wrappers, +) from elasticsearch.dsl.exceptions import IllegalOperation diff --git a/test_elasticsearch/test_dsl/test_integration/_sync/test_search.py b/test_elasticsearch/test_dsl/test_integration/_sync/test_search.py index 54060d311..41dd720a3 100644 --- a/test_elasticsearch/test_dsl/test_integration/_sync/test_search.py +++ b/test_elasticsearch/test_dsl/test_integration/_sync/test_search.py @@ -20,7 +20,15 @@ from pytest import raises from elasticsearch import ApiError, Elasticsearch -from elasticsearch.dsl import Date, Document, Keyword, MultiSearch, Q, Search, Text +from elasticsearch.dsl import ( + Date, + Document, + Keyword, + MultiSearch, + Q, + Search, + Text, +) from elasticsearch.dsl.response import aggs from ..test_data import FLAT_DATA