Skip to content

Commit 8cb8076

Browse files
committed
Deprecate parameter aliases in body parameter
1 parent 7d4a34b commit 8cb8076

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

elasticsearch/_sync/client/utils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,11 +411,21 @@ def wrapped(*args: Any, **kwargs: Any) -> Any:
411411
"issues/1698 for more information"
412412
)
413413
kwargs[body_name] = body
414-
415414
elif body_fields is not None:
416415
_merge_body_fields_no_duplicates(body, kwargs, body_fields)
417416
kwargs["body"] = body
418417

418+
if parameter_aliases and not isinstance(body, (str, bytes)):
419+
for alias, rename_to in parameter_aliases.items():
420+
if rename_to in body:
421+
warnings.warn(
422+
f"Using '{rename_to}' alias in 'body' is deprecated and will be removed in a future version of elasticsearch-py. "
423+
f"Use '{alias}' directly instead.",
424+
category=DeprecationWarning,
425+
stacklevel=2,
426+
)
427+
body[alias] = body.pop(rename_to)
428+
419429
if parameter_aliases:
420430
for alias, rename_to in parameter_aliases.items():
421431
try:

test_elasticsearch/test_client/test_rewrite_parameters.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,21 @@ def test_parameter_aliases(self):
238238
self.wrapped_func_aliases(source=["key3"])
239239
assert self.calls[-1] == ((), {"source": ["key3"]})
240240

241+
def test_parameter_aliases_body(self):
242+
with pytest.warns(
243+
DeprecationWarning,
244+
match=(
245+
"Using 'source' alias in 'body' is deprecated and will be removed in a future version of elasticsearch-py. "
246+
"Use '_source' directly instead."
247+
),
248+
):
249+
self.wrapped_func_aliases(body={"source": ["key4"]})
250+
251+
# using the correct name does not warn
252+
with warnings.catch_warnings():
253+
warnings.simplefilter("error")
254+
self.wrapped_func_aliases(body={"_source": ["key4"]})
255+
241256
@pytest.mark.parametrize("client_cls", [Elasticsearch, AsyncElasticsearch])
242257
def test_positional_argument_error(self, client_cls):
243258
client = client_cls("https://localhost:9200")

0 commit comments

Comments
 (0)