Test lazy imports in __init__ - #6
Closed
keithpeck wants to merge 4 commits into
Closed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Importing
earthdaily.earthone(or any of its eagerly-loaded sub-packages such asauth) triggered the immediate loading of all sub-packages. Thevectorsub-package transitively importsgeopandas, pandas, and shapelyat module level — all of which are expensive to initialise. This caused significant cold-start latency in environments that only need a lightweight sub-package (e.g. a Lambda function using only auth for token management).Change
All sub-packages except config are now imported lazily using the PEP 562 module-level
__getattr__mechanism. They are loaded on first access rather than at package initialisation time.config remains eagerly imported as it is required at module load time to assign the top-level select_env and get_settings helpers.
A module-level
__dir__has also been added, returning__all__,so that IDE tab-completion anddir()introspection continue to show lazy sub-packages even before they have been accessed.Options considered
I considered adding a
preload()function to allow opt-in eager loading but doesn't seem to be a common practice. users can achieve the same result with standard Python:Adding a named function would expand the public API surface for no meaningful benefit.