Skip to content

Commit 9376c2f

Browse files
authored
use getattr to lazy load aiohttp and trio (Azure#15878)
* use getattr to lazy load aiohttp and trio
1 parent 4b6911f commit 9376c2f

File tree

1 file changed

+53
-14
lines changed
  • sdk/core/azure-core/azure/core/pipeline/transport

1 file changed

+53
-14
lines changed

sdk/core/azure-core/azure/core/pipeline/transport/__init__.py

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#
2525
# --------------------------------------------------------------------------
2626

27+
import sys
2728
from ._base import HttpTransport, HttpRequest, HttpResponse
2829
from ._requests_basic import RequestsTransport, RequestsTransportResponse
2930

@@ -35,8 +36,7 @@
3536
'RequestsTransportResponse',
3637
]
3738

38-
#pylint: disable=unused-import
39-
39+
# pylint: disable=unused-import, redefined-outer-name
4040
try:
4141
from ._base_async import AsyncHttpTransport, AsyncHttpResponse
4242
from ._requests_asyncio import AsyncioRequestsTransport, AsyncioRequestsTransportResponse
@@ -47,22 +47,61 @@
4747
'AsyncioRequestsTransportResponse'
4848
])
4949

50-
try:
51-
from ._requests_trio import TrioRequestsTransport, TrioRequestsTransportResponse
50+
if sys.version_info >= (3, 7):
5251
__all__.extend([
5352
'TrioRequestsTransport',
54-
'TrioRequestsTransportResponse'
55-
])
56-
except ImportError:
57-
pass # Trio not installed
58-
59-
try:
60-
from ._aiohttp import AioHttpTransport, AioHttpTransportResponse
61-
__all__.extend([
53+
'TrioRequestsTransportResponse',
6254
'AioHttpTransport',
6355
'AioHttpTransportResponse',
6456
])
65-
except ImportError:
66-
pass # Aiohttp not installed
57+
58+
def __dir__():
59+
return __all__
60+
61+
def __getattr__(name):
62+
if name == 'AioHttpTransport':
63+
try:
64+
from ._aiohttp import AioHttpTransport
65+
return AioHttpTransport
66+
except ImportError:
67+
raise ImportError("aiohttp package is not installed")
68+
if name == 'AioHttpTransportResponse':
69+
try:
70+
from ._aiohttp import AioHttpTransportResponse
71+
return AioHttpTransportResponse
72+
except ImportError:
73+
raise ImportError("aiohttp package is not installed")
74+
if name == 'TrioRequestsTransport':
75+
try:
76+
from ._requests_trio import TrioRequestsTransport
77+
return TrioRequestsTransport
78+
except ImportError:
79+
raise ImportError("trio package is not installed")
80+
if name == 'TrioRequestsTransportResponse':
81+
try:
82+
from ._requests_trio import TrioRequestsTransportResponse
83+
return TrioRequestsTransportResponse
84+
except ImportError:
85+
raise ImportError("trio package is not installed")
86+
return name
87+
88+
else:
89+
try:
90+
from ._requests_trio import TrioRequestsTransport, TrioRequestsTransportResponse
91+
__all__.extend([
92+
'TrioRequestsTransport',
93+
'TrioRequestsTransportResponse'
94+
])
95+
except ImportError:
96+
pass # Trio not installed
97+
98+
try:
99+
from ._aiohttp import AioHttpTransport, AioHttpTransportResponse
100+
__all__.extend([
101+
'AioHttpTransport',
102+
'AioHttpTransportResponse',
103+
])
104+
except ImportError:
105+
pass # Aiohttp not installed
67106
except (ImportError, SyntaxError):
68107
pass # Asynchronous pipelines not supported.

0 commit comments

Comments
 (0)