diff --git a/elasticsearch/_async/client/__init__.py b/elasticsearch/_async/client/__init__.py index 93a0a64ca..db36df10c 100644 --- a/elasticsearch/_async/client/__init__.py +++ b/elasticsearch/_async/client/__init__.py @@ -18,7 +18,6 @@ import logging import typing as t -import warnings from elastic_transport import ( AsyncTransport, @@ -179,36 +178,12 @@ def __init__( t.Callable[[t.Dict[str, t.Any], NodeConfig], t.Optional[NodeConfig]] ] = None, meta_header: t.Union[DefaultType, bool] = DEFAULT, - timeout: t.Union[DefaultType, None, float] = DEFAULT, - randomize_hosts: t.Union[DefaultType, bool] = DEFAULT, - host_info_callback: t.Optional[ - t.Callable[ - [t.Dict[str, t.Any], t.Dict[str, t.Union[str, int]]], - t.Optional[t.Dict[str, t.Union[str, int]]], - ] - ] = None, - sniffer_timeout: t.Union[DefaultType, None, float] = DEFAULT, - sniff_on_connection_fail: t.Union[DefaultType, bool] = DEFAULT, - maxsize: t.Union[DefaultType, int] = DEFAULT, # Internal use only _transport: t.Optional[AsyncTransport] = None, ) -> None: if hosts is None and cloud_id is None and _transport is None: raise ValueError("Either 'hosts' or 'cloud_id' must be specified") - if timeout is not DEFAULT: - if request_timeout is not DEFAULT: - raise ValueError( - "Can't specify both 'timeout' and 'request_timeout', " - "instead only specify 'request_timeout'" - ) - warnings.warn( - "The 'timeout' parameter is deprecated in favor of 'request_timeout'", - category=DeprecationWarning, - stacklevel=2, - ) - request_timeout = timeout - if serializer is not None: if serializers is not DEFAULT: raise ValueError( @@ -217,58 +192,6 @@ def __init__( ) serializers = {default_mimetype: serializer} - if randomize_hosts is not DEFAULT: - if randomize_nodes_in_pool is not DEFAULT: - raise ValueError( - "Can't specify both 'randomize_hosts' and 'randomize_nodes_in_pool', " - "instead only specify 'randomize_nodes_in_pool'" - ) - warnings.warn( - "The 'randomize_hosts' parameter is deprecated in favor of 'randomize_nodes_in_pool'", - category=DeprecationWarning, - stacklevel=2, - ) - randomize_nodes_in_pool = randomize_hosts - - if sniffer_timeout is not DEFAULT: - if min_delay_between_sniffing is not DEFAULT: - raise ValueError( - "Can't specify both 'sniffer_timeout' and 'min_delay_between_sniffing', " - "instead only specify 'min_delay_between_sniffing'" - ) - warnings.warn( - "The 'sniffer_timeout' parameter is deprecated in favor of 'min_delay_between_sniffing'", - category=DeprecationWarning, - stacklevel=2, - ) - min_delay_between_sniffing = sniffer_timeout - - if sniff_on_connection_fail is not DEFAULT: - if sniff_on_node_failure is not DEFAULT: - raise ValueError( - "Can't specify both 'sniff_on_connection_fail' and 'sniff_on_node_failure', " - "instead only specify 'sniff_on_node_failure'" - ) - warnings.warn( - "The 'sniff_on_connection_fail' parameter is deprecated in favor of 'sniff_on_node_failure'", - category=DeprecationWarning, - stacklevel=2, - ) - sniff_on_node_failure = sniff_on_connection_fail - - if maxsize is not DEFAULT: - if connections_per_node is not DEFAULT: - raise ValueError( - "Can't specify both 'maxsize' and 'connections_per_node', " - "instead only specify 'connections_per_node'" - ) - warnings.warn( - "The 'maxsize' parameter is deprecated in favor of 'connections_per_node'", - category=DeprecationWarning, - stacklevel=2, - ) - connections_per_node = maxsize - # Setting min_delay_between_sniffing=True implies sniff_before_requests=True if min_delay_between_sniffing is not DEFAULT: sniff_before_requests = True @@ -290,22 +213,7 @@ def __init__( ) sniff_callback = None - if host_info_callback is not None: - if sniffed_node_callback is not None: - raise ValueError( - "Can't specify both 'host_info_callback' and 'sniffed_node_callback', " - "instead only specify 'sniffed_node_callback'" - ) - warnings.warn( - "The 'host_info_callback' parameter is deprecated in favor of 'sniffed_node_callback'", - category=DeprecationWarning, - stacklevel=2, - ) - - sniff_callback = create_sniff_callback( - host_info_callback=host_info_callback - ) - elif sniffed_node_callback is not None: + if sniffed_node_callback is not None: sniff_callback = create_sniff_callback( sniffed_node_callback=sniffed_node_callback ) diff --git a/elasticsearch/_sync/client/__init__.py b/elasticsearch/_sync/client/__init__.py index b33e13de4..0b83f7654 100644 --- a/elasticsearch/_sync/client/__init__.py +++ b/elasticsearch/_sync/client/__init__.py @@ -18,7 +18,6 @@ import logging import typing as t -import warnings from elastic_transport import ( BaseNode, @@ -179,36 +178,12 @@ def __init__( t.Callable[[t.Dict[str, t.Any], NodeConfig], t.Optional[NodeConfig]] ] = None, meta_header: t.Union[DefaultType, bool] = DEFAULT, - timeout: t.Union[DefaultType, None, float] = DEFAULT, - randomize_hosts: t.Union[DefaultType, bool] = DEFAULT, - host_info_callback: t.Optional[ - t.Callable[ - [t.Dict[str, t.Any], t.Dict[str, t.Union[str, int]]], - t.Optional[t.Dict[str, t.Union[str, int]]], - ] - ] = None, - sniffer_timeout: t.Union[DefaultType, None, float] = DEFAULT, - sniff_on_connection_fail: t.Union[DefaultType, bool] = DEFAULT, - maxsize: t.Union[DefaultType, int] = DEFAULT, # Internal use only _transport: t.Optional[Transport] = None, ) -> None: if hosts is None and cloud_id is None and _transport is None: raise ValueError("Either 'hosts' or 'cloud_id' must be specified") - if timeout is not DEFAULT: - if request_timeout is not DEFAULT: - raise ValueError( - "Can't specify both 'timeout' and 'request_timeout', " - "instead only specify 'request_timeout'" - ) - warnings.warn( - "The 'timeout' parameter is deprecated in favor of 'request_timeout'", - category=DeprecationWarning, - stacklevel=2, - ) - request_timeout = timeout - if serializer is not None: if serializers is not DEFAULT: raise ValueError( @@ -217,58 +192,6 @@ def __init__( ) serializers = {default_mimetype: serializer} - if randomize_hosts is not DEFAULT: - if randomize_nodes_in_pool is not DEFAULT: - raise ValueError( - "Can't specify both 'randomize_hosts' and 'randomize_nodes_in_pool', " - "instead only specify 'randomize_nodes_in_pool'" - ) - warnings.warn( - "The 'randomize_hosts' parameter is deprecated in favor of 'randomize_nodes_in_pool'", - category=DeprecationWarning, - stacklevel=2, - ) - randomize_nodes_in_pool = randomize_hosts - - if sniffer_timeout is not DEFAULT: - if min_delay_between_sniffing is not DEFAULT: - raise ValueError( - "Can't specify both 'sniffer_timeout' and 'min_delay_between_sniffing', " - "instead only specify 'min_delay_between_sniffing'" - ) - warnings.warn( - "The 'sniffer_timeout' parameter is deprecated in favor of 'min_delay_between_sniffing'", - category=DeprecationWarning, - stacklevel=2, - ) - min_delay_between_sniffing = sniffer_timeout - - if sniff_on_connection_fail is not DEFAULT: - if sniff_on_node_failure is not DEFAULT: - raise ValueError( - "Can't specify both 'sniff_on_connection_fail' and 'sniff_on_node_failure', " - "instead only specify 'sniff_on_node_failure'" - ) - warnings.warn( - "The 'sniff_on_connection_fail' parameter is deprecated in favor of 'sniff_on_node_failure'", - category=DeprecationWarning, - stacklevel=2, - ) - sniff_on_node_failure = sniff_on_connection_fail - - if maxsize is not DEFAULT: - if connections_per_node is not DEFAULT: - raise ValueError( - "Can't specify both 'maxsize' and 'connections_per_node', " - "instead only specify 'connections_per_node'" - ) - warnings.warn( - "The 'maxsize' parameter is deprecated in favor of 'connections_per_node'", - category=DeprecationWarning, - stacklevel=2, - ) - connections_per_node = maxsize - # Setting min_delay_between_sniffing=True implies sniff_before_requests=True if min_delay_between_sniffing is not DEFAULT: sniff_before_requests = True @@ -290,22 +213,7 @@ def __init__( ) sniff_callback = None - if host_info_callback is not None: - if sniffed_node_callback is not None: - raise ValueError( - "Can't specify both 'host_info_callback' and 'sniffed_node_callback', " - "instead only specify 'sniffed_node_callback'" - ) - warnings.warn( - "The 'host_info_callback' parameter is deprecated in favor of 'sniffed_node_callback'", - category=DeprecationWarning, - stacklevel=2, - ) - - sniff_callback = create_sniff_callback( - host_info_callback=host_info_callback - ) - elif sniffed_node_callback is not None: + if sniffed_node_callback is not None: sniff_callback = create_sniff_callback( sniffed_node_callback=sniffed_node_callback ) diff --git a/test_elasticsearch/test_async/test_transport.py b/test_elasticsearch/test_async/test_transport.py index 76a71f50b..baebc0671 100644 --- a/test_elasticsearch/test_async/test_transport.py +++ b/test_elasticsearch/test_async/test_transport.py @@ -19,7 +19,7 @@ import asyncio import re import warnings -from typing import Any, Dict, Optional, Union +from typing import Any, Dict, Optional import pytest from elastic_transport import ( @@ -562,10 +562,8 @@ async def test_sniff_after_n_seconds(self, event_loop): "kwargs", [ {"sniff_on_start": True}, - {"sniff_on_connection_fail": True}, {"sniff_on_node_failure": True}, {"sniff_before_requests": True}, - {"sniffer_timeout": 1}, {"sniff_timeout": 1}, ], ) @@ -660,42 +658,6 @@ def sniffed_node_callback( ports = {node.config.port for node in client.transport.node_pool.all()} assert ports == {9200, 124} - async def test_sniffing_deprecated_host_info_callback(self): - def host_info_callback( - node_info: Dict[str, Any], host: Dict[str, Union[int, str]] - ) -> Dict[str, Any]: - return ( - host if node_info["http"]["publish_address"].endswith(":124") else None - ) - - with warnings.catch_warnings(record=True) as w: - client = AsyncElasticsearch( # noqa: F821 - [ - NodeConfig( - "http", - "localhost", - 9200, - _extras={"data": CLUSTER_NODES_MASTER_ONLY}, - ) - ], - node_class=DummyNode, - sniff_on_start=True, - host_info_callback=host_info_callback, - ) - await client.transport._async_call() - - assert len(w) == 1 - assert w[0].category == DeprecationWarning - assert ( - str(w[0].message) - == "The 'host_info_callback' parameter is deprecated in favor of 'sniffed_node_callback'" - ) - - assert len(client.transport.node_pool) == 2 - - ports = {node.config.port for node in client.transport.node_pool.all()} - assert ports == {9200, 124} - @pytest.mark.parametrize("headers", [{}, {"X-elastic-product": "BAD HEADER"}]) async def test_unsupported_product_error(headers): diff --git a/test_elasticsearch/test_client/test_deprecated_options.py b/test_elasticsearch/test_client/test_deprecated_options.py deleted file mode 100644 index a4db1fbb7..000000000 --- a/test_elasticsearch/test_client/test_deprecated_options.py +++ /dev/null @@ -1,137 +0,0 @@ -# Licensed to Elasticsearch B.V. under one or more contributor -# license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright -# ownership. Elasticsearch B.V. licenses this file to you under -# the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -import warnings - -import pytest - -from elasticsearch import Elasticsearch, JsonSerializer - -EXPECTED_SERIALIZERS = { - "application/vnd.mapbox-vector-tile", - "application/x-ndjson", - "application/json", - "text/*", - "application/vnd.elasticsearch+json", - "application/vnd.elasticsearch+x-ndjson", -} - - -try: - import pyarrow as pa - - EXPECTED_SERIALIZERS.add("application/vnd.apache.arrow.stream") -except ImportError: - pa = None - - -def test_sniff_on_connection_fail(): - with warnings.catch_warnings(record=True) as w: - client = Elasticsearch("http://localhost:9200", sniff_on_connection_fail=True) - assert client.transport._sniff_on_node_failure is True - assert len(w) == 1 - assert w[0].category == DeprecationWarning - assert str(w[0].message) == ( - "The 'sniff_on_connection_fail' parameter is deprecated in favor of 'sniff_on_node_failure'" - ) - - with pytest.raises(ValueError) as e: - Elasticsearch( - "http://localhost:9200", - sniff_on_connection_fail=True, - sniff_on_node_failure=True, - ) - assert ( - str(e.value) - == "Can't specify both 'sniff_on_connection_fail' and 'sniff_on_node_failure', instead only specify 'sniff_on_node_failure'" - ) - - -def test_sniffer_timeout(): - with warnings.catch_warnings(record=True) as w: - client = Elasticsearch("http://localhost:9200", sniffer_timeout=1) - assert client.transport._min_delay_between_sniffing == 1 - assert len(w) == 1 - assert w[0].category == DeprecationWarning - assert str(w[0].message) == ( - "The 'sniffer_timeout' parameter is deprecated in favor of 'min_delay_between_sniffing'" - ) - - with pytest.raises(ValueError) as e: - Elasticsearch( - "http://localhost:9200", sniffer_timeout=1, min_delay_between_sniffing=1 - ) - assert ( - str(e.value) - == "Can't specify both 'sniffer_timeout' and 'min_delay_between_sniffing', instead only specify 'min_delay_between_sniffing'" - ) - - -def test_randomize_hosts(): - with warnings.catch_warnings(record=True) as w: - Elasticsearch("http://localhost:9200", randomize_hosts=True) - assert len(w) == 1 - assert w[0].category == DeprecationWarning - assert str(w[0].message) == ( - "The 'randomize_hosts' parameter is deprecated in favor of 'randomize_nodes_in_pool'" - ) - - with pytest.raises(ValueError) as e: - Elasticsearch( - "http://localhost:9200", randomize_hosts=True, randomize_nodes_in_pool=True - ) - assert ( - str(e.value) - == "Can't specify both 'randomize_hosts' and 'randomize_nodes_in_pool', instead only specify 'randomize_nodes_in_pool'" - ) - - -def test_serializer_and_serializers(): - with pytest.raises(ValueError) as e: - Elasticsearch( - "http://localhost:9200", - serializer=JsonSerializer(), - serializers={"application/json": JsonSerializer()}, - ) - assert str(e.value) == ( - "Can't specify both 'serializer' and 'serializers' parameters together. " - "Instead only specify one of the other." - ) - - class CustomSerializer(JsonSerializer): - pass - - client = Elasticsearch("http://localhost:9200", serializer=CustomSerializer()) - assert isinstance( - client.transport.serializers.get_serializer("application/json"), - CustomSerializer, - ) - assert set(client.transport.serializers.serializers.keys()) == EXPECTED_SERIALIZERS - - client = Elasticsearch( - "http://localhost:9200", - serializers={ - "application/json": CustomSerializer(), - "application/cbor": CustomSerializer(), - }, - ) - assert isinstance( - client.transport.serializers.get_serializer("application/json"), - CustomSerializer, - ) - expected = EXPECTED_SERIALIZERS | {"application/cbor"} - assert set(client.transport.serializers.serializers.keys()) == expected diff --git a/test_elasticsearch/test_client/test_options.py b/test_elasticsearch/test_client/test_options.py index b7fa3cfda..c2050d186 100644 --- a/test_elasticsearch/test_client/test_options.py +++ b/test_elasticsearch/test_client/test_options.py @@ -19,7 +19,7 @@ from elastic_transport import OpenTelemetrySpan from elastic_transport.client_utils import DEFAULT -from elasticsearch import AsyncElasticsearch, Elasticsearch +from elasticsearch import AsyncElasticsearch, Elasticsearch, JsonSerializer from elasticsearch._sync.client.utils import USER_AGENT from test_elasticsearch.test_cases import ( DummyAsyncTransport, @@ -27,6 +27,22 @@ DummyTransportTestCase, ) +EXPECTED_SERIALIZERS = { + "application/vnd.mapbox-vector-tile", + "application/x-ndjson", + "application/json", + "text/*", + "application/vnd.elasticsearch+json", + "application/vnd.elasticsearch+x-ndjson", +} + +try: + import pyarrow as pa + + EXPECTED_SERIALIZERS.add("application/vnd.apache.arrow.stream") +except ImportError: + pa = None + class TestOptions(DummyTransportTestCase): def assert_called_with_headers(self, client, method, target, headers): @@ -479,3 +495,41 @@ def test_options_timeout_parameters(self): "retry_on_status": (404,), "retry_on_timeout": True, } + + def test_serializer_and_serializers(self): + with pytest.raises(ValueError) as e: + Elasticsearch( + "http://localhost:9200", + serializer=JsonSerializer(), + serializers={"application/json": JsonSerializer()}, + ) + assert str(e.value) == ( + "Can't specify both 'serializer' and 'serializers' parameters together. " + "Instead only specify one of the other." + ) + + class CustomSerializer(JsonSerializer): + pass + + client = Elasticsearch("http://localhost:9200", serializer=CustomSerializer()) + assert isinstance( + client.transport.serializers.get_serializer("application/json"), + CustomSerializer, + ) + assert ( + set(client.transport.serializers.serializers.keys()) == EXPECTED_SERIALIZERS + ) + + client = Elasticsearch( + "http://localhost:9200", + serializers={ + "application/json": CustomSerializer(), + "application/cbor": CustomSerializer(), + }, + ) + assert isinstance( + client.transport.serializers.get_serializer("application/json"), + CustomSerializer, + ) + expected = EXPECTED_SERIALIZERS | {"application/cbor"} + assert set(client.transport.serializers.serializers.keys()) == expected diff --git a/test_elasticsearch/test_transport.py b/test_elasticsearch/test_transport.py index 5161cd8e1..6fb72f98b 100644 --- a/test_elasticsearch/test_transport.py +++ b/test_elasticsearch/test_transport.py @@ -18,7 +18,7 @@ import re import time import warnings -from typing import Any, Dict, Optional, Union +from typing import Any, Dict, Optional import pytest from elastic_transport import ( @@ -525,10 +525,8 @@ def test_sniff_after_n_seconds(self): "kwargs", [ {"sniff_on_start": True}, - {"sniff_on_connection_fail": True}, {"sniff_on_node_failure": True}, {"sniff_before_requests": True}, - {"sniffer_timeout": 1}, {"sniff_timeout": 1}, ], ) @@ -589,41 +587,6 @@ def sniffed_node_callback( ports = {node.config.port for node in client.transport.node_pool.all()} assert ports == {9200, 124} - def test_sniffing_deprecated_host_info_callback(self): - def host_info_callback( - node_info: Dict[str, Any], host: Dict[str, Union[int, str]] - ) -> Dict[str, Any]: - return ( - host if node_info["http"]["publish_address"].endswith(":124") else None - ) - - with warnings.catch_warnings(record=True) as w: - client = Elasticsearch( # noqa: F821 - [ - NodeConfig( - "http", - "localhost", - 9200, - _extras={"data": CLUSTER_NODES_MASTER_ONLY}, - ) - ], - node_class=DummyNode, - sniff_on_start=True, - host_info_callback=host_info_callback, - ) - - assert len(w) == 1 - assert w[0].category == DeprecationWarning - assert ( - str(w[0].message) - == "The 'host_info_callback' parameter is deprecated in favor of 'sniffed_node_callback'" - ) - - assert len(client.transport.node_pool) == 2 - - ports = {node.config.port for node in client.transport.node_pool.all()} - assert ports == {9200, 124} - @pytest.mark.parametrize("headers", [{}, {"X-elastic-product": "BAD HEADER"}]) def test_unsupported_product_error(headers): diff --git a/test_elasticsearch/test_types/aliased_types.py b/test_elasticsearch/test_types/aliased_types.py index 0b5c97f98..ff0aa994a 100644 --- a/test_elasticsearch/test_types/aliased_types.py +++ b/test_elasticsearch/test_types/aliased_types.py @@ -34,9 +34,7 @@ [{"host": "localhost", "port": 9443}], transport_class=Transport, sniff_on_start=True, - sniffer_timeout=0.1, sniff_timeout=1, - sniff_on_connection_fail=False, max_retries=1, retry_on_status={100, 400, 503}, retry_on_timeout=True, @@ -103,9 +101,7 @@ def reindex_types() -> None: es2 = AsyncElasticsearch( [{"host": "localhost", "port": 9443}], sniff_on_start=True, - sniffer_timeout=0.1, sniff_timeout=1, - sniff_on_connection_fail=False, max_retries=1, retry_on_status={100, 400, 503}, retry_on_timeout=True, diff --git a/test_elasticsearch/test_types/async_types.py b/test_elasticsearch/test_types/async_types.py index ac7eed19a..1743fde2d 100644 --- a/test_elasticsearch/test_types/async_types.py +++ b/test_elasticsearch/test_types/async_types.py @@ -28,9 +28,7 @@ es = AsyncElasticsearch( [{"host": "localhost", "port": 9443}], sniff_on_start=True, - sniffer_timeout=0.1, sniff_timeout=1, - sniff_on_connection_fail=False, max_retries=1, retry_on_status={100, 400, 503}, retry_on_timeout=True, diff --git a/test_elasticsearch/test_types/sync_types.py b/test_elasticsearch/test_types/sync_types.py index f4582ddba..8166e50ee 100644 --- a/test_elasticsearch/test_types/sync_types.py +++ b/test_elasticsearch/test_types/sync_types.py @@ -23,9 +23,7 @@ es = Elasticsearch( [{"host": "localhost", "port": 9443}], sniff_on_start=True, - sniffer_timeout=0.1, sniff_timeout=1, - sniff_on_connection_fail=False, max_retries=1, retry_on_status={100, 400, 503}, retry_on_timeout=True,