Repository: opensearch-project/opensearch-py
Pull request #555 (2023-10-26 16:13:19)

Fix: don't hardcode client class name.
### Description

The generator copies then patches all async class names to be sync, but it doesn't know how to patch a string inside a warning message. This avoids the problem and adds a test for the correct warning message.

### Issues Resolved

https://github.com/opensearch-project/opensearch-py/pull/553/files#r1373341397

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check [here](https://github.com/opensearch-project/OpenSearch/blob/main/CONTRIBUTING.md#developer-certificate-of-origin).


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/opensearch-project/opensearch-py/pull/555?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project) Report
> Merging [#555](https://app.codecov.io/gh/opensearch-project/opensearch-py/pull/555?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project) (6984b89) into [main](https://app.codecov.io/gh/opensearch-project/opensearch-py/commit/17794bab75d69b645964e9af8b0ed9209c5631fc?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project) (17794ba) will **decrease** coverage by `0.08%`.
> The diff coverage is `n/a`.

```diff
@@            Coverage Diff             @@
##             main     #555      +/-   ##
==========================================
- Coverage   71.09%   71.01%   -0.08%     
==========================================
  Files          85       85              
  Lines        7774     7774              
==========================================
- Hits         5527     5521       -6     
- Misses       2247     2253       +6     
```


| [Files](https://app.codecov.io/gh/opensearch-project/opensearch-py/pull/555?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project) | Coverage Δ | |
|---|---|---|
| [opensearchpy/\_async/client/plugins.py](https://app.codecov.io/gh/opensearch-project/opensearch-py/pull/555?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project#diff-b3BlbnNlYXJjaHB5L19hc3luYy9jbGllbnQvcGx1Z2lucy5weQ==) | `93.75% <ø> (ø)` | |
| [opensearchpy/client/plugins.py](https://app.codecov.io/gh/opensearch-project/opensearch-py/pull/555?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project#diff-b3BlbnNlYXJjaHB5L2NsaWVudC9wbHVnaW5zLnB5) | `100.00% <ø> (+6.25%)` | :arrow_up: |

... and [3 files with indirect coverage changes](https://app.codecov.io/gh/opensearch-project/opensearch-py/pull/555/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project)



Patch:
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 @@ class PluginsClient(NamespacedClient):
                 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 @@ class PluginsClient(NamespacedClient):
                 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,
                 )


Test patch:
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.",
+            )