Skip to content

feat(core): implement PEP 0810 explicit lazy imports in google-cloud-core - #18052

Open
hebaalazzeh wants to merge 1 commit into
mainfrom
feature/lazy-imports-google-cloud-core
Open

feat(core): implement PEP 0810 explicit lazy imports in google-cloud-core#18052
hebaalazzeh wants to merge 1 commit into
mainfrom
feature/lazy-imports-google-cloud-core

Conversation

@hebaalazzeh

Copy link
Copy Markdown
Contributor

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

@hebaalazzeh hebaalazzeh self-assigned this Aug 10, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread packages/google-cloud-core/google/cloud/client/__init__.py Outdated
Comment thread packages/google-cloud-core/google/cloud/client/__init__.py Outdated
Comment thread packages/google-cloud-core/google/cloud/client/__init__.py Outdated
@hebaalazzeh
hebaalazzeh force-pushed the feature/lazy-imports-google-cloud-core branch 2 times, most recently from 3bd0d3d to 88be17b Compare August 10, 2026 20:32
…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
@hebaalazzeh
hebaalazzeh force-pushed the feature/lazy-imports-google-cloud-core branch from 88be17b to 8f73f3b Compare August 10, 2026 21:18
@hebaalazzeh
hebaalazzeh marked this pull request as ready for review August 10, 2026 21:23
@hebaalazzeh
hebaalazzeh requested a review from a team as a code owner August 10, 2026 21:23
Comment on lines +53 to +54
import grpc # noqa: E402
import google.auth.transport.grpc # noqa: E402

@ohmayr ohmayr Aug 10, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and in other places, why do we need these # noqa: E402? Given that things were working before.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants