Skip to content

Commit 5184b42

Browse files
committed
fix(auth): implement conditional base classes using TYPE_CHECKING in requests and urllib3 transports
1 parent 7cfeded commit 5184b42

2 files changed

Lines changed: 30 additions & 14 deletions

File tree

packages/google-auth/google/auth/transport/requests.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import time
2525
from typing import Optional, Set, TYPE_CHECKING
2626

27-
2827
# PEP 0810: Explicit Lazy Imports
2928
# Python 3.15+ natively intercepts and defers these imports.
3029
# Developers can disable this behavior and force eager imports.
@@ -54,13 +53,18 @@
5453
create_urllib3_context,
5554
) # pylint: disable=ungrouped-imports
5655

57-
from google.auth import _helpers
58-
from google.auth import exceptions
59-
from google.auth import transport
56+
from google.auth import _helpers, exceptions, transport
6057
from google.auth.transport import _mtls_helper
6158
import google.auth.transport._mtls_helper
6259
from google.oauth2 import service_account
6360

61+
if TYPE_CHECKING:
62+
_HTTPAdapterBase = requests.adapters.HTTPAdapter
63+
_SessionBase = requests.Session
64+
else:
65+
_HTTPAdapterBase = _helpers.HeapDummy
66+
_SessionBase = _helpers.HeapDummy
67+
6468

6569
class _LazyBasesMeta(_helpers.LazyBasesMeta):
6670
def _perform_resolve_bases(cls):
@@ -194,7 +198,7 @@ def __call__(
194198
body=None,
195199
headers=None,
196200
timeout=_DEFAULT_TIMEOUT,
197-
**kwargs
201+
**kwargs,
198202
):
199203
"""Make an HTTP request using requests.
200204
@@ -242,9 +246,10 @@ class _MutualTlsAdapter(_HTTPAdapterBase, metaclass=_LazyBasesMeta):
242246
"""
243247

244248
def __init__(self, cert, key):
245-
import certifi
246249
import ssl
247250

251+
import certifi
252+
248253
ctx_poolmanager = create_urllib3_context()
249254
ctx_poolmanager.load_verify_locations(cafile=certifi.where())
250255

@@ -318,6 +323,7 @@ class _MutualTlsOffloadAdapter(_HTTPAdapterBase, metaclass=_LazyBasesMeta):
318323

319324
def __init__(self, enterprise_cert_file_path):
320325
import certifi
326+
321327
from google.auth.transport import _custom_tls_signer
322328

323329
self.signer = _custom_tls_signer.CustomTlsSigner(enterprise_cert_file_path)
@@ -537,7 +543,7 @@ def request(
537543
headers=None,
538544
max_allowed_time=None,
539545
timeout=_DEFAULT_TIMEOUT,
540-
**kwargs
546+
**kwargs,
541547
):
542548
"""Implementation of Requests' request.
543549
@@ -599,7 +605,7 @@ def request(
599605
data=data,
600606
headers=request_headers,
601607
timeout=timeout,
602-
**kwargs
608+
**kwargs,
603609
)
604610
remaining_time = guard.remaining_timeout
605611

@@ -671,7 +677,7 @@ def request(
671677
max_allowed_time=remaining_time,
672678
timeout=timeout,
673679
_credential_refresh_attempt=_credential_refresh_attempt + 1,
674-
**kwargs
680+
**kwargs,
675681
)
676682

677683
return response

packages/google-auth/google/auth/transport/urllib3.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
from __future__ import absolute_import
1818

19-
2019
import http.client as http_client
2120
import logging
2221
from typing import Set, TYPE_CHECKING
@@ -52,12 +51,22 @@
5251
) from caught_exc
5352

5453

55-
from google.auth import _helpers
56-
from google.auth import exceptions
57-
from google.auth import transport
54+
from google.auth import _helpers, exceptions, transport
5855
from google.auth.transport import _mtls_helper
5956
from google.oauth2 import service_account
6057

58+
if TYPE_CHECKING:
59+
try:
60+
from urllib3._request_methods import RequestMethods as _HttpBase
61+
except ImportError:
62+
try:
63+
from urllib3.request import RequestMethods as _HttpBase # type: ignore
64+
except ImportError:
65+
_HttpBase = object # type: ignore
66+
else:
67+
_HttpBase = _helpers.HeapDummy
68+
69+
6170
# PEP 0810: Explicit Lazy Imports
6271
# Python 3.15+ natively intercepts and defers these imports.
6372
# Developers can disable this behavior and force eager imports.
@@ -199,9 +208,10 @@ def _make_mutual_tls_http(cert, key):
199208
Raises:
200209
google.auth.exceptions.MutualTLSChannelError: If the cert or key is invalid.
201210
"""
202-
import certifi
203211
import ssl
204212

213+
import certifi
214+
205215
ctx = urllib3.util.ssl_.create_urllib3_context()
206216
ctx.load_verify_locations(cafile=certifi.where())
207217

0 commit comments

Comments
 (0)