Skip to content

Commit 63caee8

Browse files
committed
feat: lazy imports gapic v1 (#17673)
## Description This PR initiates the rollout of our PEP 0810 lazy-loading architecture to google-api-core, starting with the `gapic_v1` module. By applying Python 3.15's native explicit lazy imports, we defer the eager execution of inner modules (like `method.py` and `config.py`) which recursively pull in massive third-party C-extensions like `grpcio`. This acts as the first step in addressing the high initialization latency and peak memory footprints we're seeing on Serverless cold-starts. Older Python runtimes (3.14 and below) safely ignore the `__lazy_modules__` set and fall back to standard eager execution, meaning this introduces zero backwards compatibility risk. *(Note: We use fully-qualified, hardcoded module strings rather than dynamic `__name__` injection, and provide explicit type annotations (`Set[str]`), to ensure static analysis tools and type checkers can safely infer the proxies without complaints).* ## Related Documents * **Design Doc:** go/sdk:api-core-lazy-loading * **Original GAPIC Generator Implementation PR:** #17591
1 parent 3f5ad03 commit 63caee8

1 file changed

Lines changed: 40 additions & 14 deletions

File tree

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

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

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,44 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from google.api_core.gapic_v1 import client_info
16-
from google.api_core.gapic_v1 import config
17-
from google.api_core.gapic_v1 import config_async
18-
from google.api_core.gapic_v1 import method
19-
from google.api_core.gapic_v1 import method_async
20-
from google.api_core.gapic_v1 import routing_header
15+
import importlib.util
16+
from typing import Set
2117

22-
__all__ = [
23-
"client_info",
24-
"config",
25-
"config_async",
26-
"method",
27-
"method_async",
28-
"routing_header",
29-
]
18+
_has_grpc = importlib.util.find_spec("grpc") is not None
19+
20+
# PEP 0810: Explicit Lazy Imports
21+
# Python 3.15+ natively intercepts and defers these imports.
22+
# Developers can disable this behavior and force eager imports.
23+
# For more information, see:
24+
# https://docs.python.org/3.15/library/sys.html#sys.set_lazy_imports_filter
25+
# Older Python versions safely ignore this variable.
26+
__lazy_modules__: Set[str] = {
27+
"google.api_core.gapic_v1.client_info",
28+
"google.api_core.gapic_v1.routing_header",
29+
}
30+
__all__ = ["client_info", "routing_header"]
31+
32+
if _has_grpc:
33+
__lazy_modules__.update(
34+
{
35+
"google.api_core.gapic_v1.config",
36+
"google.api_core.gapic_v1.config_async",
37+
"google.api_core.gapic_v1.method",
38+
"google.api_core.gapic_v1.method_async",
39+
}
40+
)
41+
42+
from google.api_core.gapic_v1 import ( # noqa: E402
43+
client_info,
44+
routing_header,
45+
)
46+
47+
if _has_grpc:
48+
from google.api_core.gapic_v1 import ( # noqa: F401
49+
config,
50+
config_async,
51+
method,
52+
method_async,
53+
)
54+
55+
__all__.extend(["config", "config_async", "method", "method_async"])

0 commit comments

Comments
 (0)