-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-4260 Lazily load optional imports #1550
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
Conversation
@ShaneHarvey I looked at adding this to existing benchmarks, but those run without optional dependencies. We could instead use something like our mongo-arrow checks, adding a pass/fail benchmark on the PR if there is a significant increase in import time. |
pymongo/common.py
Outdated
spec = importlib.util.find_spec(name) | ||
if spec is None: | ||
# Import the module to trigger an import error. | ||
importlib.import_module(name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we raise ModuleNotFound directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
pymongo/common.py
Outdated
|
||
From https://docs.python.org/3/library/importlib.html#implementing-lazy-imports | ||
""" | ||
spec = importlib.util.find_spec(name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to guard against this caveat?:
If the module is in sys.modules, then sys.modules[name].spec is returned (unless the spec would be None or is not set, in which case ValueError is raised).
https://docs.python.org/3/library/importlib.html#importlib.util.find_spec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
# Clear the cached credentials if we hit a failure in auth. | ||
set_cached_credentials(None) | ||
pymongo_auth_aws.set_cached_credentials(None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This behavior is subtly different. set_cached_credentials is now required when before it was optional. Let's raise the min version of pymongo_auth_aws that can be used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
pymongo/compression_support.py
Outdated
from pymongo.monitoring import _SENSITIVE_COMMANDS | ||
|
||
|
||
def lazy_import(name: str) -> ModuleType: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we have two copies of lazy_import?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hoisted the function to a new private module to avoid import cycles.
Sounds good to me. |
evergreen retry |
fi | ||
|
||
# Skip the report of it isn't a PR run. | ||
if [ "$BASE_SHA" == "$HEAD_SHA" ]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we still run this on mainline? The mainline test can check the previous commit (HEAD~
) vs the current.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@@ -74,6 +74,10 @@ Unavoidable breaking changes | |||
>>> dict_to_SON(data_as_dict) | |||
SON([('driver', SON([('name', 'PyMongo'), ('version', '4.7.0.dev0')])), ('os', SON([('type', 'Darwin'), ('name', 'Darwin'), ('architecture', 'arm64'), ('version', '14.3')])), ('platform', 'CPython 3.11.6.final.0')]) | |||
|
|||
- PyMongo now uses `lazy imports <https://docs.python.org/3/library/importlib.html#implementing-lazy-imports>`_ for external dependencies. | |||
If you are relying on any kind of monkey-patching of the standard library, you may need to explicitly import those external libraries in addition |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add a note that gevent+eventlet style patching still works as expected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@@ -0,0 +1,37 @@ | |||
# Copyright 2024-Present MongoDB, Inc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this tool live in the test/ dir instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so, since we won't run it as part of pytest
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good.
.evergreen/config.yml
Outdated
display_name: "${green-framework} ${python-version} ${platform} ${auth-ssl}" | ||
tasks: *all-server-versions | ||
tasks: *encryption-server-versions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't actually test with encryption since it's missing the encryption tag. How about we open a new ticket to add this test coverage rather than add it here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
pymongo/_lazy_import.py
Outdated
@@ -0,0 +1,38 @@ | |||
# Copyright 2011-present MongoDB, Inc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
@@ -0,0 +1,37 @@ | |||
# Copyright 2024-Present MongoDB, Inc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good.
@@ -45,7 +45,7 @@ dependencies = [ | |||
|
|||
[project.optional-dependencies] | |||
aws = [ | |||
"pymongo-auth-aws<2.0.0", | |||
"pymongo-auth-aws>=1.1.0,<2.0.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also document this in the changelog.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
0.209 s without this PR
0.063 s with this PR (about 3X faster).