Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Implement lazy loading to speed up import times for packages that use this library
Closes #544
Title
Improve import time by lazy-loading cloud provider modules
Description
Problem
Importing
cloudpathlibcurrently takes ~435ms because it eagerly loads all cloud provider SDKs (google-cloud-storage,boto3,azure-storage-blob) at import time, even if the user only needs one provider.This is problematic for:
Benchmarks
Measured with Python 3.12 (5 runs, mean ± std):
import cloudpathlibfrom cloudpathlib import S3Pathfrom cloudpathlib import GSPathfrom cloudpathlib import AzureBlobPathAdditionally, the current implementation loads all three SDKs even when importing just one path class. With lazy loading, only the SDK for the provider being used gets loaded.
Proposed Solution
Implement lazy loading via
__getattr__in the__init__.pyfiles:cloudpathlib/__init__.pycloudpathlib/s3/__init__.pycloudpathlib/gs/__init__.pycloudpathlib/azure/__init__.pyThis defers loading cloud SDKs until they are actually accessed, while preserving static type hints via
TYPE_CHECKINGblocks.Example
Before:
import cloudpathlib # Takes ~435ms, loads all SDKsAfter:
import cloudpathlib # Takes ~22ms, no SDKs loaded
from cloudpathlib import S3Path # Only loads boto3 when S3Path is accessed
Implementation Notes
__getattr__withTYPE_CHECKINGblocks for static type hints (IDE support preserved)cloudpath.py(from cloudpathlib.enums→from .enums)anypathimport to function-local incloudpath.pyto avoid circular importsContributor checklist:
CONTRIBUTING.mdCloses #issueappears in the PR summary (e.g.,Closes #123).HISTORY.mdwith the issue that is addressed and the PR you are submitting. If the top section is not `## UNRELEASED``, then you need to add a new section to the top of the document for your change.