Skip to content

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

Merged
merged 23 commits into from
Mar 25, 2024

Conversation

blink1073
Copy link
Member

@blink1073 blink1073 commented Mar 13, 2024

pip install -e ".[aws,oscp,encryption,snappy,gssapi,zstd]"
pip install tuna
python -X importtime -c "import pymongo" 2> pymongo.log
tuna pymongo.log

0.209 s without this PR
0.063 s with this PR (about 3X faster).

@blink1073 blink1073 requested a review from ShaneHarvey March 13, 2024 13:38
@blink1073
Copy link
Member Author

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

spec = importlib.util.find_spec(name)
if spec is None:
# Import the module to trigger an import error.
importlib.import_module(name)
Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done


From https://docs.python.org/3/library/importlib.html#implementing-lazy-imports
"""
spec = importlib.util.find_spec(name)
Copy link
Member

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

Copy link
Member Author

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)
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

from pymongo.monitoring import _SENSITIVE_COMMANDS


def lazy_import(name: str) -> ModuleType:
Copy link
Member

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?

Copy link
Member Author

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.

@ShaneHarvey
Copy link
Member

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.

Sounds good to me.

@blink1073
Copy link
Member Author

evergreen retry

@blink1073 blink1073 closed this Mar 20, 2024
@blink1073 blink1073 reopened this Mar 20, 2024
@blink1073 blink1073 requested a review from ShaneHarvey March 21, 2024 11:19
@blink1073 blink1073 marked this pull request as ready for review March 21, 2024 18:57
fi

# Skip the report of it isn't a PR run.
if [ "$BASE_SHA" == "$HEAD_SHA" ]; then
Copy link
Member

@ShaneHarvey ShaneHarvey Mar 21, 2024

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.

Copy link
Member Author

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
Copy link
Member

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.

Copy link
Member Author

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.
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good.

@blink1073 blink1073 requested a review from ShaneHarvey March 22, 2024 01:22
display_name: "${green-framework} ${python-version} ${platform} ${auth-ssl}"
tasks: *all-server-versions
tasks: *encryption-server-versions
Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed

@@ -0,0 +1,38 @@
# Copyright 2011-present MongoDB, Inc.
Copy link
Member

Choose a reason for hiding this comment

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

2024

Copy link
Member Author

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.
Copy link
Member

Choose a reason for hiding this comment

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

Sounds good.

@blink1073 blink1073 requested a review from ShaneHarvey March 25, 2024 11:46
@@ -45,7 +45,7 @@ dependencies = [

[project.optional-dependencies]
aws = [
"pymongo-auth-aws<2.0.0",
"pymongo-auth-aws>=1.1.0,<2.0.0",
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@blink1073 blink1073 requested a review from ShaneHarvey March 25, 2024 16:55
@blink1073 blink1073 merged commit 42a08c4 into mongodb:master Mar 25, 2024
@blink1073 blink1073 deleted the PYTHON-4260-2 branch March 25, 2024 17:55
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