From 0d08f08aec261b656abc92261cadb9c003f43c8c Mon Sep 17 00:00:00 2001 From: dblock Date: Wed, 15 Nov 2023 18:56:13 -0500 Subject: [PATCH] Fix: sync opensearchpy without iohttp. Signed-off-by: dblock --- noxfile.py | 7 +++++- opensearchpy/__init__.py | 36 +++++++++++++++-------------- opensearchpy/connection/__init__.py | 11 +++++++-- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/noxfile.py b/noxfile.py index 296ea8a4..90e86a13 100644 --- a/noxfile.py +++ b/noxfile.py @@ -45,11 +45,16 @@ @nox.session(python=["3.6", "3.7", "3.8", "3.9", "3.10", "3.11"]) # type: ignore def test(session: Any) -> None: session.install(".") + # ensure client can be imported without aiohttp + session.run("python", "-c", "import opensearchpy\nprint(opensearchpy.OpenSearch())") + # ensure client can be imported with aiohttp + session.install(".[async]") + session.run("python", "-c", "import opensearchpy\nprint(opensearchpy.AsyncOpenSearch())") + session.install("-r", "dev-requirements.txt") session.run("python", "setup.py", "test") - @nox.session(python=["3.7"]) # type: ignore def format(session: Any) -> None: session.install("black", "isort") diff --git a/opensearchpy/__init__.py b/opensearchpy/__init__.py index e9ef6485..aa528c98 100644 --- a/opensearchpy/__init__.py +++ b/opensearchpy/__init__.py @@ -44,12 +44,8 @@ logger = logging.getLogger("opensearch") logger.addHandler(logging.NullHandler()) -from ._async.client import AsyncOpenSearch -from ._async.http_aiohttp import AIOHttpConnection, AsyncConnection -from ._async.transport import AsyncTransport from .client import OpenSearch from .connection import ( - AsyncHttpConnection, Connection, RequestsHttpConnection, Urllib3HttpConnection, @@ -76,12 +72,7 @@ UnknownDslObject, ValidationException, ) -from .helpers import ( - AWSV4SignerAsyncAuth, - AWSV4SignerAuth, - RequestsAWSV4SignerAuth, - Urllib3AWSV4SignerAuth, -) +from .helpers import AWSV4SignerAuth, RequestsAWSV4SignerAuth, Urllib3AWSV4SignerAuth from .helpers.aggs import A from .helpers.analysis import analyzer, char_filter, normalizer, token_filter, tokenizer from .helpers.document import Document, InnerDoc, MetaField @@ -159,7 +150,6 @@ "JSONSerializer", "Connection", "RequestsHttpConnection", - "AsyncHttpConnection", "Urllib3HttpConnection", "ImproperlyConfigured", "OpenSearchException", @@ -178,7 +168,6 @@ "AWSV4SignerAuth", "Urllib3AWSV4SignerAuth", "RequestsAWSV4SignerAuth", - "AWSV4SignerAsyncAuth", "A", "AttrDict", "AttrList", @@ -251,10 +240,23 @@ "normalizer", "token_filter", "tokenizer", - "AIOHttpConnection", - "AsyncConnection", - "AsyncTransport", - "AsyncOpenSearch", - "AsyncHttpConnection", "__versionstr__", ] + +try: + from ._async.client import AsyncOpenSearch + from ._async.http_aiohttp import AIOHttpConnection, AsyncConnection + from ._async.transport import AsyncTransport + from .connection import AsyncHttpConnection + from .helpers import AWSV4SignerAsyncAuth + + __all__ += [ + "AIOHttpConnection", + "AsyncConnection", + "AsyncTransport", + "AsyncOpenSearch", + "AsyncHttpConnection", + "AWSV4SignerAsyncAuth", + ] +except (ImportError, SyntaxError): + pass diff --git a/opensearchpy/connection/__init__.py b/opensearchpy/connection/__init__.py index 40037859..287b7ecb 100644 --- a/opensearchpy/connection/__init__.py +++ b/opensearchpy/connection/__init__.py @@ -27,7 +27,6 @@ from .base import Connection -from .http_async import AsyncHttpConnection from .http_requests import RequestsHttpConnection from .http_urllib3 import Urllib3HttpConnection, create_ssl_context @@ -36,5 +35,13 @@ "RequestsHttpConnection", "Urllib3HttpConnection", "create_ssl_context", - "AsyncHttpConnection", ] + +try: + from .http_async import AsyncHttpConnection + + __all__ += [ + "AsyncHttpConnection", + ] +except (ImportError, SyntaxError): + pass