Skip to content

Commit ade0b13

Browse files
authored
Deprecate 'doc_type' for query parameters
1 parent 28a82a0 commit ade0b13

File tree

12 files changed

+117
-26
lines changed

12 files changed

+117
-26
lines changed

elasticsearch/_async/client/license.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ async def post_start_basic(self, params=None, headers=None):
9797
"POST", "/_license/start_basic", params=params, headers=headers
9898
)
9999

100-
@query_params("acknowledge", "doc_type")
100+
@query_params("acknowledge", "type")
101101
async def post_start_trial(self, params=None, headers=None):
102102
"""
103103
starts a limited time trial license.
@@ -106,12 +106,9 @@ async def post_start_trial(self, params=None, headers=None):
106106
107107
:arg acknowledge: whether the user has acknowledged acknowledge
108108
messages (default: false)
109-
:arg doc_type: The type of trial license to generate (default:
109+
:arg type: The type of trial license to generate (default:
110110
"trial")
111111
"""
112-
if "doc_type" in params:
113-
params["type"] = params.pop("doc_type")
114-
115112
return await self.transport.perform_request(
116113
"POST", "/_license/start_trial", params=params, headers=headers
117114
)

elasticsearch/_async/client/license.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class LicenseClient(NamespacedClient):
134134
self,
135135
*,
136136
acknowledge: Optional[bool] = ...,
137-
doc_type: Optional[Any] = ...,
137+
type: Optional[Any] = ...,
138138
pretty: Optional[bool] = ...,
139139
human: Optional[bool] = ...,
140140
error_trace: Optional[bool] = ...,

elasticsearch/_async/client/nodes.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ async def stats(
124124
)
125125

126126
@query_params(
127-
"doc_type", "ignore_idle_threads", "interval", "snapshots", "threads", "timeout"
127+
"ignore_idle_threads", "interval", "snapshots", "threads", "timeout", "type"
128128
)
129129
async def hot_threads(self, node_id=None, params=None, headers=None):
130130
"""
@@ -136,8 +136,6 @@ async def hot_threads(self, node_id=None, params=None, headers=None):
136136
limit the returned information; use `_local` to return information from
137137
the node you're connecting to, leave empty to get information from all
138138
nodes
139-
:arg doc_type: The type to sample (default: cpu) Valid choices:
140-
cpu, wait, block
141139
:arg ignore_idle_threads: Don't show threads that are in known-
142140
idle places, such as waiting on a socket select or pulling from an empty
143141
task queue (default: true)
@@ -147,10 +145,9 @@ async def hot_threads(self, node_id=None, params=None, headers=None):
147145
:arg threads: Specify the number of threads to provide
148146
information for (default: 3)
149147
:arg timeout: Explicit operation timeout
148+
:arg type: The type to sample (default: cpu) Valid choices:
149+
cpu, wait, block
150150
"""
151-
if "doc_type" in params:
152-
params["type"] = params.pop("doc_type")
153-
154151
return await self.transport.perform_request(
155152
"GET",
156153
_make_path("_nodes", node_id, "hot_threads"),

elasticsearch/_async/client/nodes.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ class NodesClient(NamespacedClient):
100100
self,
101101
*,
102102
node_id: Optional[Any] = ...,
103-
doc_type: Optional[Any] = ...,
104103
ignore_idle_threads: Optional[bool] = ...,
105104
interval: Optional[Any] = ...,
106105
snapshots: Optional[Any] = ...,
107106
threads: Optional[Any] = ...,
108107
timeout: Optional[Any] = ...,
108+
type: Optional[Any] = ...,
109109
pretty: Optional[bool] = ...,
110110
human: Optional[bool] = ...,
111111
error_trace: Optional[bool] = ...,

elasticsearch/client/license.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def post_start_basic(self, params=None, headers=None):
9797
"POST", "/_license/start_basic", params=params, headers=headers
9898
)
9999

100-
@query_params("acknowledge", "doc_type")
100+
@query_params("acknowledge", "type")
101101
def post_start_trial(self, params=None, headers=None):
102102
"""
103103
starts a limited time trial license.
@@ -106,12 +106,9 @@ def post_start_trial(self, params=None, headers=None):
106106
107107
:arg acknowledge: whether the user has acknowledged acknowledge
108108
messages (default: false)
109-
:arg doc_type: The type of trial license to generate (default:
109+
:arg type: The type of trial license to generate (default:
110110
"trial")
111111
"""
112-
if "doc_type" in params:
113-
params["type"] = params.pop("doc_type")
114-
115112
return self.transport.perform_request(
116113
"POST", "/_license/start_trial", params=params, headers=headers
117114
)

elasticsearch/client/license.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class LicenseClient(NamespacedClient):
134134
self,
135135
*,
136136
acknowledge: Optional[bool] = ...,
137-
doc_type: Optional[Any] = ...,
137+
type: Optional[Any] = ...,
138138
pretty: Optional[bool] = ...,
139139
human: Optional[bool] = ...,
140140
error_trace: Optional[bool] = ...,

elasticsearch/client/nodes.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def stats(
124124
)
125125

126126
@query_params(
127-
"doc_type", "ignore_idle_threads", "interval", "snapshots", "threads", "timeout"
127+
"ignore_idle_threads", "interval", "snapshots", "threads", "timeout", "type"
128128
)
129129
def hot_threads(self, node_id=None, params=None, headers=None):
130130
"""
@@ -136,8 +136,6 @@ def hot_threads(self, node_id=None, params=None, headers=None):
136136
limit the returned information; use `_local` to return information from
137137
the node you're connecting to, leave empty to get information from all
138138
nodes
139-
:arg doc_type: The type to sample (default: cpu) Valid choices:
140-
cpu, wait, block
141139
:arg ignore_idle_threads: Don't show threads that are in known-
142140
idle places, such as waiting on a socket select or pulling from an empty
143141
task queue (default: true)
@@ -147,10 +145,9 @@ def hot_threads(self, node_id=None, params=None, headers=None):
147145
:arg threads: Specify the number of threads to provide
148146
information for (default: 3)
149147
:arg timeout: Explicit operation timeout
148+
:arg type: The type to sample (default: cpu) Valid choices:
149+
cpu, wait, block
150150
"""
151-
if "doc_type" in params:
152-
params["type"] = params.pop("doc_type")
153-
154151
return self.transport.perform_request(
155152
"GET",
156153
_make_path("_nodes", node_id, "hot_threads"),

elasticsearch/client/nodes.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ class NodesClient(NamespacedClient):
100100
self,
101101
*,
102102
node_id: Optional[Any] = ...,
103-
doc_type: Optional[Any] = ...,
104103
ignore_idle_threads: Optional[bool] = ...,
105104
interval: Optional[Any] = ...,
106105
snapshots: Optional[Any] = ...,
107106
threads: Optional[Any] = ...,
108107
timeout: Optional[Any] = ...,
108+
type: Optional[Any] = ...,
109109
pretty: Optional[bool] = ...,
110110
human: Optional[bool] = ...,
111111
error_trace: Optional[bool] = ...,

elasticsearch/client/utils.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ def query_params(*es_query_params, **kwargs):
133133
body_only_params = set(body_params or ()) - set(es_query_params)
134134
body_name = kwargs.pop("body_name", None)
135135
body_required = kwargs.pop("body_required", False)
136+
type_possible_in_params = "type" in es_query_params
136137

137138
# There should be no APIs defined with both 'body_params' and a named body.
138139
assert not (body_name and body_params)
@@ -163,6 +164,27 @@ def _wrapped(*args, **kwargs):
163164
using_body_kwarg = kwargs.get("body", None) is not None
164165
using_positional_args = args and len(args) > 1
165166

167+
# The 'doc_type' parameter is deprecated in the query
168+
# string. This was generated and missed in 7.x so to
169+
# push users to use 'type' instead of 'doc_type' in 8.x
170+
# we deprecate it here.
171+
if type_possible_in_params:
172+
doc_type_in_params = params and "doc_type" in params
173+
doc_type_in_kwargs = "doc_type" in kwargs
174+
175+
if doc_type_in_params or doc_type_in_kwargs:
176+
warnings.warn(
177+
"The 'doc_type' parameter is deprecated, use 'type' for this "
178+
"API instead. See https://github.com/elastic/elasticsearch-py/"
179+
"issues/1698 for more information",
180+
category=DeprecationWarning,
181+
stacklevel=2,
182+
)
183+
if doc_type_in_params:
184+
params["type"] = params.pop("doc_type")
185+
if doc_type_in_kwargs:
186+
kwargs["type"] = kwargs.pop("doc_type")
187+
166188
if using_body_kwarg or using_positional_args:
167189
# If there are any body-only parameters then we raise a 'TypeError'
168190
# to alert the user they have to either not use a 'body' parameter

test_elasticsearch/test_async/test_server/test_rest_api_spec.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from elasticsearch.helpers.test import _get_version
3030

3131
from ...test_server.test_rest_api_spec import (
32+
APIS_USING_TYPE_INSTEAD_OF_DOC_TYPE,
3233
APIS_WITH_BODY_FIELDS,
3334
IMPLEMENTED_FEATURES,
3435
PARAMS_RENAMES,
@@ -138,6 +139,11 @@ async def run_do(self, action):
138139
# some parameters had to be renamed to not clash with python builtins,
139140
# compensate
140141
for k in PARAMS_RENAMES:
142+
143+
# Don't do the 'doc_type' rename for APIs that actually want 'type'
144+
if k == "type" and method in APIS_USING_TYPE_INSTEAD_OF_DOC_TYPE:
145+
continue
146+
141147
if k in args:
142148
args[PARAMS_RENAMES[k]] = args.pop(k)
143149

test_elasticsearch/test_client/test_overrides.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@
1616
# specific language governing permissions and limitations
1717
# under the License.
1818

19+
# Licensed to Elasticsearch B.V. under one or more contributor
20+
# license agreements. See the NOTICE file distributed with
21+
# this work for additional information regarding copyright
22+
# ownership. Elasticsearch B.V. licenses this file to you under
23+
# the Apache License, Version 2.0 (the "License"); you may
24+
# not use this file except in compliance with the License.
25+
# You may obtain a copy of the License at
26+
#
27+
# http://www.apache.org/licenses/LICENSE-2.0
28+
#
29+
# Unless required by applicable law or agreed to in writing,
30+
# software distributed under the License is distributed on an
31+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
32+
# KIND, either express or implied. See the License for the
33+
# specific language governing permissions and limitations
34+
# under the License.
35+
import warnings
36+
1937
import pytest
2038

2139
from test_elasticsearch.test_cases import ElasticsearchTestCase
@@ -184,3 +202,51 @@ def test_clear_scroll(self):
184202
assert calls == {
185203
("DELETE", "/_search/scroll"): [({}, {}, {"scroll_id": "scroll-id"})]
186204
}
205+
206+
def test_doc_type_works_for_apis_with_type(self):
207+
with warnings.catch_warnings(record=True) as w:
208+
self.client.license.post_start_trial(type="trial")
209+
210+
assert w == []
211+
calls = self.client.transport.calls
212+
assert calls == {
213+
("POST", "/_license/start_trial"): [({"type": b"trial"}, {}, None)]
214+
}
215+
self.client.transport.calls.pop(("POST", "/_license/start_trial"))
216+
217+
with warnings.catch_warnings(record=True) as w:
218+
self.client.license.post_start_trial(params={"type": "trial"})
219+
220+
assert w == []
221+
calls = self.client.transport.calls
222+
assert calls == {
223+
("POST", "/_license/start_trial"): [({"type": "trial"}, {}, None)]
224+
}
225+
self.client.transport.calls.pop(("POST", "/_license/start_trial"))
226+
227+
# Now we try using 'doc_type' in all the same places and see
228+
# that things still work but we get deprecation warnings.
229+
with pytest.warns(DeprecationWarning) as w:
230+
self.client.license.post_start_trial(doc_type="trial")
231+
232+
assert str(w[0].message) == (
233+
"The 'doc_type' parameter is deprecated, use 'type' for this API instead. See "
234+
"https://github.com/elastic/elasticsearch-py/issues/1698 for more information"
235+
)
236+
calls = self.client.transport.calls
237+
assert calls == {
238+
("POST", "/_license/start_trial"): [({"type": b"trial"}, {}, None)]
239+
}
240+
self.client.transport.calls.pop(("POST", "/_license/start_trial"))
241+
242+
with pytest.warns(DeprecationWarning) as w:
243+
self.client.license.post_start_trial(params={"doc_type": "trial"})
244+
245+
assert str(w[0].message) == (
246+
"The 'doc_type' parameter is deprecated, use 'type' for this API instead. See "
247+
"https://github.com/elastic/elasticsearch-py/issues/1698 for more information"
248+
)
249+
calls = self.client.transport.calls
250+
assert calls == {
251+
("POST", "/_license/start_trial"): [({"type": "trial"}, {}, None)]
252+
}

test_elasticsearch/test_server/test_rest_api_spec.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
# some params had to be changed in python, keep track of them so we can rename
4343
# those in the tests accordingly
4444
PARAMS_RENAMES = {"type": "doc_type", "from": "from_"}
45+
APIS_USING_TYPE_INSTEAD_OF_DOC_TYPE = {
46+
"nodes.hot_threads",
47+
"license.post_start_trial",
48+
}
4549

4650
# mapping from catch values to http status codes
4751
CATCH_CODES = {"missing": 404, "conflict": 409, "unauthorized": 401}
@@ -235,6 +239,11 @@ def run_do(self, action):
235239
# some parameters had to be renamed to not clash with python builtins,
236240
# compensate
237241
for k in PARAMS_RENAMES:
242+
243+
# Don't do the 'doc_type' rename for APIs that actually want 'type'
244+
if k == "type" and method in APIS_USING_TYPE_INSTEAD_OF_DOC_TYPE:
245+
continue
246+
238247
if k in args:
239248
args[PARAMS_RENAMES[k]] = args.pop(k)
240249

0 commit comments

Comments
 (0)