|
14 | 14 |
|
15 | 15 | """Package for interacting with the google.longrunning.operations meta-API.""" |
16 | 16 |
|
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 | +try: |
| 21 | + _has_async_rest = ( |
| 22 | + importlib.util.find_spec("google.auth.aio.transport.sessions") is not None |
| 23 | + ) |
| 24 | +except ModuleNotFoundError: |
| 25 | + _has_async_rest = False |
| 26 | + |
| 27 | +# PEP 0810: Explicit Lazy Imports |
| 28 | +# Python 3.15+ natively intercepts and defers these imports. |
| 29 | +# Developers can disable this behavior and force eager imports. |
| 30 | +# For more information, see: |
| 31 | +# https://docs.python.org/3.15/library/sys.html#sys.set_lazy_imports_filter |
| 32 | +# Older Python versions safely ignore this variable. |
| 33 | +# NOTE: We statically define all modules here (including async ones) to ensure |
| 34 | +# static analysis tools (mypy, pyright, Ruff) can easily parse them. If async |
| 35 | +# support is not present, the imports are ignored, making their presence safe. |
| 36 | +__lazy_modules__: Set[str] = { |
| 37 | + "google.api_core.operations_v1.abstract_operations_client", |
| 38 | + "google.api_core.operations_v1.operations_async_client", |
| 39 | + "google.api_core.operations_v1.operations_client", |
| 40 | + "google.api_core.operations_v1.transports.rest", |
| 41 | + "google.api_core.operations_v1.transports.rest_asyncio", |
| 42 | + "google.api_core.operations_v1.operations_rest_client_async", |
| 43 | +} |
21 | 44 |
|
22 | 45 | __all__ = [ |
23 | 46 | "AbstractOperationsClient", |
24 | 47 | "OperationsAsyncClient", |
25 | 48 | "OperationsClient", |
26 | | - "OperationsRestTransport" |
| 49 | + "OperationsRestTransport", |
27 | 50 | ] |
28 | 51 |
|
29 | | -try: |
30 | | - from google.api_core.operations_v1.transports.rest_asyncio import ( |
31 | | - AsyncOperationsRestTransport, |
32 | | - ) |
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 |
| 52 | + |
| 53 | +from google.api_core.operations_v1.abstract_operations_client import ( # noqa: E402 |
| 54 | + AbstractOperationsClient, |
| 55 | +) |
| 56 | +from google.api_core.operations_v1.operations_async_client import ( # noqa: E402 |
| 57 | + OperationsAsyncClient, |
| 58 | +) |
| 59 | +from google.api_core.operations_v1.operations_client import ( # noqa: E402 |
| 60 | + OperationsClient, |
| 61 | +) |
| 62 | +from google.api_core.operations_v1.transports.rest import ( # noqa: E402 |
| 63 | + OperationsRestTransport, |
| 64 | +) |
| 65 | + |
| 66 | +if _has_async_rest: |
| 67 | + try: |
| 68 | + # On Python 3.15+, PEP 0810 lazy loading means these imports will succeed |
| 69 | + # instantly (returning a lazy proxy). Any actual ImportErrors (e.g., due to |
| 70 | + # missing aiohttp/auth dependencies) are deferred until the proxies are accessed. |
| 71 | + from google.api_core.operations_v1.transports.rest_asyncio import ( # noqa: E402, F401 |
| 72 | + AsyncOperationsRestTransport, |
| 73 | + ) |
| 74 | + from google.api_core.operations_v1.operations_rest_client_async import ( # noqa: E402, F401 |
| 75 | + AsyncOperationsRestClient, |
| 76 | + ) |
| 77 | + |
| 78 | + __all__.extend(["AsyncOperationsRestClient", "AsyncOperationsRestTransport"]) |
| 79 | + except ImportError: |
| 80 | + # Fallback for older python/environments when importlib find_spec succeeds but actual import fails |
| 81 | + pass |
0 commit comments