diff --git a/sdk/core/azure-core/azure/core/pipeline/transport/__init__.py b/sdk/core/azure-core/azure/core/pipeline/transport/__init__.py index 03d5b0d19c88..7daa8fe4c959 100644 --- a/sdk/core/azure-core/azure/core/pipeline/transport/__init__.py +++ b/sdk/core/azure-core/azure/core/pipeline/transport/__init__.py @@ -24,137 +24,84 @@ # # -------------------------------------------------------------------------- -import sys from ._base import HttpTransport, HttpRequest, HttpResponse +from ._base_async import AsyncHttpTransport, AsyncHttpResponse + +# pylint: disable=undefined-all-variable __all__ = [ 'HttpTransport', 'HttpRequest', 'HttpResponse', + 'RequestsTransport', + 'RequestsTransportResponse', + 'AsyncHttpTransport', + 'AsyncHttpResponse', + 'AsyncioRequestsTransport', + 'AsyncioRequestsTransportResponse' + 'TrioRequestsTransport', + 'TrioRequestsTransportResponse', + 'AioHttpTransport', + 'AioHttpTransportResponse', ] -# pylint: disable=unused-import, redefined-outer-name -try: - from ._requests_basic import RequestsTransport, RequestsTransportResponse - __all__.extend([ - 'RequestsTransport', - 'RequestsTransportResponse', - ]) - try: - from ._base_async import AsyncHttpTransport, AsyncHttpResponse - from ._requests_asyncio import AsyncioRequestsTransport, AsyncioRequestsTransportResponse - - __all__.extend([ - 'AsyncHttpTransport', - 'AsyncHttpResponse', - 'AsyncioRequestsTransport', - 'AsyncioRequestsTransportResponse' - ]) - - if sys.version_info >= (3, 7): - __all__.extend([ - 'TrioRequestsTransport', - 'TrioRequestsTransportResponse', - 'AioHttpTransport', - 'AioHttpTransportResponse', - ]) - - def __dir__(): - return __all__ - - def __getattr__(name): - if name == 'AioHttpTransport': - try: - from ._aiohttp import AioHttpTransport - return AioHttpTransport - except ImportError: - raise ImportError("aiohttp package is not installed") - if name == 'AioHttpTransportResponse': - try: - from ._aiohttp import AioHttpTransportResponse - return AioHttpTransportResponse - except ImportError: - raise ImportError("aiohttp package is not installed") - if name == 'TrioRequestsTransport': - try: - from ._requests_trio import TrioRequestsTransport - return TrioRequestsTransport - except ImportError: - raise ImportError("trio package is not installed") - if name == 'TrioRequestsTransportResponse': - try: - from ._requests_trio import TrioRequestsTransportResponse - return TrioRequestsTransportResponse - except ImportError: - raise ImportError("trio package is not installed") - if name == '__bases__': - raise AttributeError("module 'azure.core.pipeline.transport' has no attribute '__bases__'") - return name - - else: - try: - from ._requests_trio import TrioRequestsTransport, TrioRequestsTransportResponse - - __all__.extend([ - 'TrioRequestsTransport', - 'TrioRequestsTransportResponse' - ]) - except ImportError: - pass # Trio not installed - - try: - from ._aiohttp import AioHttpTransport, AioHttpTransportResponse - - __all__.extend([ - 'AioHttpTransport', - 'AioHttpTransportResponse', - ]) - except ImportError: - pass # Aiohttp not installed - except (ImportError, SyntaxError): - # requests library is installed but asynchronous pipelines not supported. - pass -except (ImportError, SyntaxError): - # requests library is not installed - try: - from ._base_async import AsyncHttpTransport, AsyncHttpResponse - __all__.extend([ - 'AsyncHttpTransport', - 'AsyncHttpResponse', - ]) - - if sys.version_info >= (3, 7): - __all__.extend([ - 'AioHttpTransport', - 'AioHttpTransportResponse', - ]) - - def __dir__(): - return __all__ +# pylint: disable=unused-import, redefined-outer-name, no-member, too-many-statements, too-many-branches - def __getattr__(name): - if name == 'AioHttpTransport': - try: - from ._aiohttp import AioHttpTransport - return AioHttpTransport - except ImportError: - raise ImportError("aiohttp package is not installed") - if name == 'AioHttpTransportResponse': - try: - from ._aiohttp import AioHttpTransportResponse - return AioHttpTransportResponse - except ImportError: - raise ImportError("aiohttp package is not installed") - return name +def __dir__(): + return __all__ - else: - try: - from ._aiohttp import AioHttpTransport, AioHttpTransportResponse - __all__.extend([ - 'AioHttpTransport', - 'AioHttpTransportResponse', - ]) - except ImportError: - pass # Aiohttp not installed - except (ImportError, SyntaxError): - pass # Asynchronous pipelines not supported. +def __getattr__(name): + transport = None + if name == 'AsyncioRequestsTransport': + try: + from ._requests_asyncio import AsyncioRequestsTransport + transport = AsyncioRequestsTransport + except ImportError: + raise ImportError("requests package is not installed") + if name == 'AsyncioRequestsTransportResponse': + try: + from ._requests_asyncio import AsyncioRequestsTransportResponse + transport = AsyncioRequestsTransportResponse + except ImportError: + raise ImportError("requests package is not installed") + if name == 'RequestsTransport': + try: + from ._requests_basic import RequestsTransport + transport = RequestsTransport + except ImportError: + raise ImportError("requests package is not installed") + if name == 'RequestsTransportResponse': + try: + from ._requests_basic import RequestsTransportResponse + transport = RequestsTransportResponse + except ImportError: + raise ImportError("requests package is not installed") + if name == 'AioHttpTransport': + try: + from ._aiohttp import AioHttpTransport + transport = AioHttpTransport + except ImportError: + raise ImportError("aiohttp package is not installed") + if name == 'AioHttpTransportResponse': + try: + from ._aiohttp import AioHttpTransportResponse + transport = AioHttpTransportResponse + except ImportError: + raise ImportError("aiohttp package is not installed") + if name == 'TrioRequestsTransport': + try: + from ._requests_trio import TrioRequestsTransport + transport = TrioRequestsTransport + except ImportError as ex: + if ex.msg.endswith("'requests'"): + raise ImportError("requests package is not installed") + raise ImportError("trio package is not installed") + if name == 'TrioRequestsTransportResponse': + try: + from ._requests_trio import TrioRequestsTransportResponse + transport = TrioRequestsTransportResponse + except ImportError: + raise ImportError("trio package is not installed") + if transport: + return transport + raise AttributeError(f"module 'azure.core.pipeline.transport' has no attribute {name}")