Skip to content

Commit faa70d1

Browse files
committed
fix: address code review feedback on base class types, import order and remove lazy imports test
1 parent 7ae442e commit faa70d1

4 files changed

Lines changed: 22 additions & 139 deletions

File tree

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from __future__ import absolute_import
1818

1919
import logging
20-
from typing import Set
2120

2221
from google.auth import exceptions
2322
from google.auth.transport import _mtls_helper
@@ -33,18 +32,8 @@
3332

3433
_LOGGER = logging.getLogger(__name__)
3534

36-
# PEP 0810: Explicit Lazy Imports
37-
# Python 3.15+ natively intercepts and defers these imports.
38-
# Developers can disable this behavior and force eager imports.
39-
# For more information, see:
40-
# https://docs.python.org/3.15/library/sys.html#sys.set_lazy_imports_filter
41-
# Older Python versions safely ignore this variable.
42-
__lazy_modules__: Set[str] = {"grpc"}
4335

44-
45-
# Inheriting from object instead of grpc.AuthMetadataPlugin is intentional.
46-
# This prevents eagerly loading grpc at module import time, preserving lazy loading support.
47-
class AuthMetadataPlugin(object):
36+
class AuthMetadataPlugin(grpc.AuthMetadataPlugin):
4837
"""A `gRPC AuthMetadataPlugin`_ that inserts the credentials into each
4938
request.
5039
@@ -62,6 +51,10 @@ class AuthMetadataPlugin(object):
6251
"""
6352

6453
def __init__(self, credentials, request, default_host=None):
54+
# pylint: disable=no-value-for-parameter
55+
# pylint doesn't realize that the super method takes no arguments
56+
# because this class is the same name as the superclass.
57+
super(AuthMetadataPlugin, self).__init__()
6558
self._credentials = credentials
6659
self._request = request
6760
self._default_host = default_host

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@
2525
from typing import Optional, Set
2626

2727

28+
# PEP 0810: Explicit Lazy Imports
29+
# Python 3.15+ natively intercepts and defers these imports.
30+
# Developers can disable this behavior and force eager imports.
31+
# For more information, see:
32+
# https://docs.python.org/3.15/library/sys.html#sys.set_lazy_imports_filter
33+
# Older Python versions safely ignore this variable.
34+
__lazy_modules__: Set[str] = {"requests", "requests.adapters", "requests.exceptions"}
35+
36+
2837
if importlib.util.find_spec("requests") is None:
2938
raise ImportError(
3039
"The requests library is not installed, please install the requests package to use the requests transport."
@@ -44,14 +53,6 @@
4453
import google.auth.transport._mtls_helper
4554
from google.oauth2 import service_account
4655

47-
# PEP 0810: Explicit Lazy Imports
48-
# Python 3.15+ natively intercepts and defers these imports.
49-
# Developers can disable this behavior and force eager imports.
50-
# For more information, see:
51-
# https://docs.python.org/3.15/library/sys.html#sys.set_lazy_imports_filter
52-
# Older Python versions safely ignore this variable.
53-
__lazy_modules__: Set[str] = {"requests", "requests.adapters", "requests.exceptions"}
54-
5556
_LOGGER = logging.getLogger(__name__)
5657

5758
_DEFAULT_TIMEOUT = 120 # in seconds
@@ -94,9 +95,11 @@ class TimeoutGuard(object):
9495
:class:`requests.exceptions.Timeout`.
9596
"""
9697

97-
def __init__(self, timeout, timeout_error_type=requests.exceptions.Timeout):
98+
def __init__(self, timeout, timeout_error_type=None):
9899
self._timeout = timeout
99100
self.remaining_timeout = timeout
101+
if timeout_error_type is None:
102+
timeout_error_type = requests.exceptions.Timeout
100103
self._timeout_error_type = timeout_error_type
101104

102105
def __enter__(self):

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

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import http.client as http_client
2121
import logging
22-
from typing import Set
2322
import warnings
2423

2524
# Certifi is Mozilla's certificate bundle. Urllib3 needs a certificate bundle
@@ -58,19 +57,10 @@
5857
from google.auth.transport import _mtls_helper
5958
from google.oauth2 import service_account
6059

61-
# PEP 0810: Explicit Lazy Imports
62-
# Python 3.15+ natively intercepts and defers these imports.
63-
# Developers can disable this behavior and force eager imports.
64-
# For more information, see:
65-
# https://docs.python.org/3.15/library/sys.html#sys.set_lazy_imports_filter
66-
# Older Python versions safely ignore this variable.
67-
__lazy_modules__: Set[str] = {
68-
"urllib3",
69-
"urllib3.exceptions",
70-
"certifi",
71-
"packaging",
72-
"packaging.version",
73-
}
60+
if version.parse(urllib3.__version__) >= version.parse("2.0.0"): # pragma: NO COVER
61+
RequestMethods = urllib3._request_methods.RequestMethods # type: ignore
62+
else: # pragma: NO COVER
63+
RequestMethods = urllib3.request.RequestMethods # type: ignore
7464

7565

7666
_LOGGER = logging.getLogger(__name__)
@@ -215,7 +205,7 @@ def _make_mutual_tls_http(cert, key):
215205
return http
216206

217207

218-
class AuthorizedHttp(object):
208+
class AuthorizedHttp(RequestMethods): # type: ignore
219209
"""A urllib3 HTTP class with credentials.
220210
221211
This class is used to perform requests to API endpoints that require
@@ -298,32 +288,6 @@ def my_cert_callback():
298288
account credentials.
299289
"""
300290

301-
_encode_url_methods = {"DELETE", "GET", "HEAD", "OPTIONS"}
302-
_request_methods_class = None
303-
304-
def __getattr__(self, name):
305-
if name in ("request", "request_encode_url", "request_encode_body"):
306-
import types
307-
308-
if AuthorizedHttp._request_methods_class is None:
309-
if version.parse(urllib3.__version__) >= version.parse("2.0.0"):
310-
AuthorizedHttp._request_methods_class = (
311-
urllib3._request_methods.RequestMethods
312-
)
313-
else:
314-
AuthorizedHttp._request_methods_class = (
315-
urllib3.request.RequestMethods
316-
)
317-
318-
method = getattr(AuthorizedHttp._request_methods_class, name)
319-
bound_method = types.MethodType(method, self)
320-
setattr(self, name, bound_method)
321-
return bound_method
322-
323-
raise AttributeError(
324-
f"'{self.__class__.__name__}' object has no attribute '{name}'"
325-
)
326-
327291
def __init__(
328292
self,
329293
credentials,

packages/google-auth/tests/transport/test_lazy_imports.py

Lines changed: 0 additions & 77 deletions
This file was deleted.

0 commit comments

Comments
 (0)