Skip to content

Commit 364db02

Browse files
committed
feat: implement PEP 0810 lazy loading in operations_v1
1 parent c824b6d commit 364db02

1 file changed

Lines changed: 53 additions & 16 deletions

File tree

  • packages/google-api-core/google/api_core/operations_v1

packages/google-api-core/google/api_core/operations_v1/__init__.py

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,64 @@
1414

1515
"""Package for interacting with the google.longrunning.operations meta-API."""
1616

17-
from google.api_core.operations_v1.abstract_operations_client import AbstractOperationsClient
18-
from google.api_core.operations_v1.operations_async_client import OperationsAsyncClient
19-
from google.api_core.operations_v1.operations_client import OperationsClient
20-
from google.api_core.operations_v1.transports.rest import OperationsRestTransport
17+
import importlib.util
18+
from typing import Set
19+
20+
_has_async_rest = (
21+
importlib.util.find_spec("google.auth.aio.transport.sessions") is not None
22+
)
23+
24+
# PEP 0810: Explicit Lazy Imports
25+
# Python 3.15+ natively intercepts and defers these imports.
26+
# Developers can disable this behavior and force eager imports.
27+
# For more information, see:
28+
# https://docs.python.org/3.15/library/sys.html#sys.set_lazy_imports_filter
29+
# Older Python versions safely ignore this variable.
30+
__lazy_modules__: Set[str] = {
31+
"google.api_core.operations_v1.abstract_operations_client",
32+
"google.api_core.operations_v1.operations_async_client",
33+
"google.api_core.operations_v1.operations_client",
34+
"google.api_core.operations_v1.transports.rest",
35+
}
2136

2237
__all__ = [
2338
"AbstractOperationsClient",
2439
"OperationsAsyncClient",
2540
"OperationsClient",
26-
"OperationsRestTransport"
41+
"OperationsRestTransport",
2742
]
2843

29-
try:
30-
from google.api_core.operations_v1.transports.rest_asyncio import (
31-
AsyncOperationsRestTransport,
44+
if _has_async_rest:
45+
__lazy_modules__.update(
46+
{
47+
"google.api_core.operations_v1.transports.rest_asyncio",
48+
"google.api_core.operations_v1.operations_rest_client_async",
49+
}
3250
)
33-
from google.api_core.operations_v1.operations_rest_client_async import AsyncOperationsRestClient
34-
35-
__all__ += ["AsyncOperationsRestClient", "AsyncOperationsRestTransport"]
36-
except ImportError:
37-
# This import requires the `async_rest` extra.
38-
# Don't raise an exception if `AsyncOperationsRestTransport` cannot be imported
39-
# as other transports are still available.
40-
pass
51+
52+
from google.api_core.operations_v1.abstract_operations_client import ( # noqa: E402
53+
AbstractOperationsClient,
54+
)
55+
from google.api_core.operations_v1.operations_async_client import ( # noqa: E402
56+
OperationsAsyncClient,
57+
)
58+
from google.api_core.operations_v1.operations_client import ( # noqa: E402
59+
OperationsClient,
60+
)
61+
from google.api_core.operations_v1.transports.rest import ( # noqa: E402
62+
OperationsRestTransport,
63+
)
64+
65+
if _has_async_rest:
66+
try:
67+
from google.api_core.operations_v1.transports.rest_asyncio import ( # noqa: E402, F401
68+
AsyncOperationsRestTransport,
69+
)
70+
from google.api_core.operations_v1.operations_rest_client_async import ( # noqa: E402, F401
71+
AsyncOperationsRestClient,
72+
)
73+
74+
__all__.extend(["AsyncOperationsRestClient", "AsyncOperationsRestTransport"])
75+
except ImportError:
76+
# Fallback for older python/environments when importlib find_spec succeeds but actual import fails
77+
pass

0 commit comments

Comments
 (0)