feat(core): implement PEP 0810 explicit lazy imports in google-cloud-core - #18052
feat(core): implement PEP 0810 explicit lazy imports in google-cloud-core#18052hebaalazzeh wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request implements PEP 0810 explicit lazy imports (lazy_modules) across several modules in google-cloud-core to optimize cold starts and memory footprints in Python 3.15+. The review feedback correctly identifies that using importlib.util.find_spec on submodules (such as google.auth.api_key) eagerly imports their parent packages, which defeats the purpose of lazy loading. The reviewer suggests using module-level getattr to defer the resolution of HAS_GOOGLE_AUTH_API_KEY until it is actually accessed.
3bd0d3d to
88be17b
Compare
…core This PR implements PEP 0810 explicit lazy imports in google-cloud-core. On Python 3.15+, this defers loading of heavy inner modules and third-party dependencies (grpcio, cryptography, requests, and protobuf descriptor pools) to reduce serverless cold starts and memory footprints. On Python 3.14 and below, this falls back safely to eager execution with zero backwards-compatibility risk. ### Related Links - GAPIC Implementation PR: #17591 - google-api-core gapic_v1 PR: #17673 - google-api-core operations_v1 PR: #17724 - google-auth transport PR: #17679
88be17b to
8f73f3b
Compare
| import grpc # noqa: E402 | ||
| import google.auth.transport.grpc # noqa: E402 |
There was a problem hiding this comment.
Here and in other places, why do we need these # noqa: E402? Given that things were working before.
There was a problem hiding this comment.
In order for PEP 0810 explicit lazy imports to work, the __lazy_modules__ set must be defined before the imports we want Python 3.15 to intercept.
Because a variable definition now precedes these import statements, linters (such as flake8 and ruff) flag them with E402: Module level import not at top of file. The # noqa: E402 comments suppress that warning, following the exact same pattern used in #17673 (gapic_v1) and #17724 (operations_v1).
This PR implements PEP 0810 explicit lazy imports in google-cloud-core.
On Python 3.15+, this defers loading of heavy inner modules and third-party dependencies (grpcio, cryptography, requests, and protobuf descriptor pools) to reduce serverless cold starts and memory footprints.
On Python 3.14 and below, this falls back safely to eager execution with zero backwards-compatibility risk.
Related Links
Design doc: go/sdk:python-lazy-loading