-
Notifications
You must be signed in to change notification settings - Fork 459
Dramatically improve performance by lazily loading most imports #1645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+29,975
−10,773
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
helenye-stripe
approved these changes
Oct 16, 2025
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.
Why?
In #1427, users flagged that importing stripe is really slow. We dug into some causes in this comment. This PR dramatically improves import speeds and memory usage, ensuring users only "pay" for what they actually use.
As a result, the initial import is very fast. Subsequent operations will incur slight performance hits since we're doing imports as-needed, but the overall effect is still a dramatic increase in performance.
Stats
tldr: roughly a 4x speed improvement across the board
First, a pure import of the module is much faster:
Everything else also got faster as a result. For example, this script (which includes a round trip to the server):
Has improved runtime:
Early user feedback has been very good and we've got good test coverage here, so I'm feeling pretty good.
What?
Fixes fall into a few big categories:
if TYPE_CHECKINGblocks and quote them when used_service,__init__, ¶mfiles, move all imports into a typechecking block and use a module-level__getattr__(docs) to dynamically resolve imports. This means we're never recursively importing everything anymoreStripeClient, lean heavily on type-only imports_v1_services.pyand_v2_services.py, move all types to annotations and lazily instantiate subservices as needed, instead of all at onceIn many of these cases, we generate a dict that has everything we need to import a module, then write the
__getattr__to try and do the import when prompted. We raiseAttributeErrors when the import fails, which Python interprets on our behalf.We use
importlib.import_module, which is a high-level API for programmatically importing things. It caches modules just like static imports do, so subsequent dynamic imports are much faster.See Also
Changelog
if TYPE_CHECKINGblock__getattr__functions to most__init__.pyfiles