Skip to content

Commit cfde494

Browse files
committed
refactor: apply PEP 810 directly to transport modules instead of __init__ to fully address parthea's feedback
1 parent b528945 commit cfde494

5 files changed

Lines changed: 27 additions & 83 deletions

File tree

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

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,6 @@
2626

2727
import abc
2828
import http.client as http_client
29-
from typing import Set
30-
31-
# PEP 0810: Explicit Lazy Imports
32-
# Python 3.15+ natively intercepts and defers these imports.
33-
# Developers can disable this behavior and force eager imports.
34-
# For more information, see:
35-
# https://docs.python.org/3.15/library/sys.html#sys.set_lazy_imports_filter
36-
# Older Python versions safely ignore this variable.
37-
__lazy_modules__: Set[str] = {
38-
"_aiohttp_requests",
39-
"_custom_tls_signer",
40-
"_http_client",
41-
"_mtls_helper",
42-
"_requests_base",
43-
"grpc",
44-
"mtls",
45-
"requests",
46-
"urllib3",
47-
}
4829

4930
DEFAULT_RETRYABLE_STATUS_CODES = (
5031
http_client.INTERNAL_SERVER_ERROR,
@@ -121,49 +102,3 @@ def __call__(
121102
# pylint: disable=redundant-returns-doc, missing-raises-doc
122103
# (pylint doesn't play well with abstract docstrings.)
123104
raise NotImplementedError("__call__ must be implemented.")
124-
125-
126-
try:
127-
from google.auth.transport import _aiohttp_requests # noqa: F401, E402
128-
except ImportError:
129-
pass
130-
131-
try:
132-
from google.auth.transport import _custom_tls_signer # noqa: F401, E402
133-
except ImportError:
134-
pass
135-
136-
try:
137-
from google.auth.transport import _http_client # noqa: F401, E402
138-
except ImportError:
139-
pass
140-
141-
try:
142-
from google.auth.transport import _mtls_helper # noqa: F401, E402
143-
except ImportError:
144-
pass
145-
146-
try:
147-
from google.auth.transport import _requests_base # noqa: F401, E402
148-
except ImportError:
149-
pass
150-
151-
try:
152-
from google.auth.transport import grpc # noqa: F401, E402
153-
except ImportError:
154-
pass
155-
156-
try:
157-
from google.auth.transport import mtls # noqa: F401, E402
158-
except ImportError:
159-
pass
160-
161-
try:
162-
from google.auth.transport import requests # noqa: F401, E402
163-
except ImportError:
164-
pass
165-
166-
try:
167-
from google.auth.transport import urllib3 # noqa: F401, E402
168-
except ImportError:
169-
pass

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
from __future__ import absolute_import
1818

19+
from typing import Set
20+
21+
__lazy_modules__: Set[str] = {"grpc"}
22+
1923
import logging
2024

2125
from google.auth import exceptions

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
from __future__ import absolute_import
1818

19+
from typing import Set
20+
21+
__lazy_modules__: Set[str] = {'requests', 'requests.adapters', 'requests.exceptions'}
22+
1923
import functools
2024
import http.client as http_client
2125
import logging

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
from __future__ import absolute_import
1818

19+
from typing import Set
20+
21+
__lazy_modules__: Set[str] = {"urllib3", "urllib3.exceptions", "certifi", "packaging", "packaging.version"}
22+
1923
import http.client as http_client
2024
import logging
2125
import warnings

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,38 @@
1919

2020
SCRIPT_PYTHON_315 = """
2121
import sys
22-
import google.auth.transport
22+
import google.auth.transport.requests
23+
import google.auth.transport.urllib3
24+
import google.auth.transport.grpc
2325
24-
LAZY_MODULES = [
25-
"google.auth.transport.requests",
26-
"google.auth.transport.urllib3",
27-
"google.auth.transport.grpc",
28-
]
26+
# The heavy underlying modules should NOT be loaded yet
27+
HEAVY_MODULES = ["requests", "urllib3", "grpc"]
2928
30-
for mod in LAZY_MODULES:
29+
for mod in HEAVY_MODULES:
3130
if mod in sys.modules:
3231
print(f"FAILED: {mod} was eagerly loaded into sys.modules")
3332
sys.exit(1)
3433
35-
from google.auth.transport import requests
34+
# Trigger reification
35+
import requests
3636
_ = requests.__name__
3737
38-
if "google.auth.transport.requests" not in sys.modules:
39-
print("FAILED: google.auth.transport.requests was not lazily loaded upon access")
38+
if "requests" not in sys.modules:
39+
print("FAILED: requests was not lazily loaded upon access")
4040
sys.exit(2)
4141
4242
sys.exit(0)
4343
"""
4444

4545
SCRIPT_PRE_315 = """
4646
import sys
47-
import google.auth.transport
48-
from google.auth.transport import requests
47+
import google.auth.transport.requests
48+
import google.auth.transport.urllib3
49+
import google.auth.transport.grpc
4950
50-
LAZY_MODULES = [
51-
"google.auth.transport.requests",
52-
"google.auth.transport.urllib3",
53-
"google.auth.transport.grpc",
54-
]
51+
HEAVY_MODULES = ["requests", "urllib3", "grpc"]
5552
56-
for mod in LAZY_MODULES:
53+
for mod in HEAVY_MODULES:
5754
if mod not in sys.modules:
5855
print(f"FAILED: {mod} was not eagerly loaded into sys.modules")
5956
sys.exit(1)

0 commit comments

Comments
 (0)