From ed32d8ea3e47a15d943cc3b779780e122365cc8a Mon Sep 17 00:00:00 2001 From: "Daniel (dB.) Doubrovkine" Date: Thu, 26 Oct 2023 12:30:23 -0400 Subject: [PATCH] Fix: don't hardcode client class name. (#555) Signed-off-by: dblock Signed-off-by: roma2023 --- opensearchpy/_async/client/plugins.py | 2 +- opensearchpy/client/plugins.py | 5 +--- .../test_async/test_plugins_client.py | 24 +++++++++++++++++++ .../test_plugins/test_plugins_client.py | 24 +++++++++++++++++++ 4 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 test_opensearchpy/test_async/test_plugins_client.py create mode 100644 test_opensearchpy/test_client/test_plugins/test_plugins_client.py diff --git a/opensearchpy/_async/client/plugins.py b/opensearchpy/_async/client/plugins.py index 2b762ba3..b39576c1 100644 --- a/opensearchpy/_async/client/plugins.py +++ b/opensearchpy/_async/client/plugins.py @@ -44,7 +44,7 @@ def _dynamic_lookup(self, client): setattr(client, plugin, getattr(self, plugin)) else: warnings.warn( - f"Cannot load `{plugin}` directly to AsyncOpenSearch. `{plugin}` already exists in AsyncOpenSearch. Please use `AsyncOpenSearch.plugin.{plugin}` instead.", + f"Cannot load `{plugin}` directly to {self.client.__class__.__name__} as it already exists. Use `{self.client.__class__.__name__}.plugin.{plugin}` instead.", category=RuntimeWarning, stacklevel=2, ) diff --git a/opensearchpy/client/plugins.py b/opensearchpy/client/plugins.py index 7fba8c32..b39576c1 100644 --- a/opensearchpy/client/plugins.py +++ b/opensearchpy/client/plugins.py @@ -7,7 +7,6 @@ # Modifications Copyright OpenSearch Contributors. See # GitHub history for details. - import warnings from ..plugins.alerting import AlertingClient @@ -45,9 +44,7 @@ def _dynamic_lookup(self, client): setattr(client, plugin, getattr(self, plugin)) else: warnings.warn( - "Cannot load `{plugin}` directly to OpenSearch. `{plugin}` already exists in OpenSearch. Please use `OpenSearch.plugin.{plugin}` instead.".format( - plugin=plugin - ), + f"Cannot load `{plugin}` directly to {self.client.__class__.__name__} as it already exists. Use `{self.client.__class__.__name__}.plugin.{plugin}` instead.", category=RuntimeWarning, stacklevel=2, ) diff --git a/test_opensearchpy/test_async/test_plugins_client.py b/test_opensearchpy/test_async/test_plugins_client.py new file mode 100644 index 00000000..c620873c --- /dev/null +++ b/test_opensearchpy/test_async/test_plugins_client.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# SPDX-License-Identifier: Apache-2.0 +# +# The OpenSearch Contributors require contributions made to +# this file be licensed under the Apache-2.0 license or a +# compatible open source license. +# +# Modifications Copyright OpenSearch Contributors. See +# GitHub history for details. + +from unittest import TestCase + +from opensearchpy._async.client import AsyncOpenSearch + + +class TestPluginsClient(TestCase): + async def test_plugins_client(self): + with self.assertWarns(Warning) as w: + client = AsyncOpenSearch() + client.plugins.__init__(client) # double-init + self.assertEqual( + str(w.warnings[0].message), + "Cannot load `alerting` directly to AsyncOpenSearch as it already exists. Use `AsyncOpenSearch.plugin.alerting` instead.", + ) diff --git a/test_opensearchpy/test_client/test_plugins/test_plugins_client.py b/test_opensearchpy/test_client/test_plugins/test_plugins_client.py new file mode 100644 index 00000000..e717d9cb --- /dev/null +++ b/test_opensearchpy/test_client/test_plugins/test_plugins_client.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# SPDX-License-Identifier: Apache-2.0 +# +# The OpenSearch Contributors require contributions made to +# this file be licensed under the Apache-2.0 license or a +# compatible open source license. +# +# Modifications Copyright OpenSearch Contributors. See +# GitHub history for details. + +from opensearchpy.client import OpenSearch + +from ...test_cases import TestCase + + +class TestPluginsClient(TestCase): + def test_plugins_client(self): + with self.assertWarns(Warning) as w: + client = OpenSearch() + client.plugins.__init__(client) # double-init + self.assertEqual( + str(w.warnings[0].message), + "Cannot load `alerting` directly to OpenSearch as it already exists. Use `OpenSearch.plugin.alerting` instead.", + )