Repository navigation
PYTHON-6275 Enable all ruff PERF rules - #3224
Merged
Merged
Conversation
NoahStapp
requested review from
blink1073
and
a balanced review from Copilot
November 13, 2026 15:36
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Bounded to_list(length) calls create an unnecessary temporary list, adding auxiliary memory proportional to the number of documents transferred.
1 open issue
What changed in this PR
This PR enables Ruff’s PERF rules across PyMongo and updates collection-building patterns in the driver, tests, and tooling to comply.
Changes:
- Enable the
PERFrule family in Ruff configuration. - Replace append and assignment loops with comprehensions,
extend, andupdate. - Apply corresponding changes to asynchronous code and generated synchronous counterparts.
| File | Description |
|---|---|
| tools/generate_sbom_requirements.py | Collect requirement files with a comprehension. |
| test/unified_format.py | Simplify command-event filtering. |
| test/test_discovery_and_monitoring.py | Simplify concurrent runner creation. |
| test/test_custom_types.py | Build decimal test documents with a comprehension. |
| test/test_client_bulk_write.py | Simplify write-model creation and event filtering. |
| test/test_change_stream.py | Generate collection names with a comprehension. |
| test/test_bulk.py | Extend bulk documents from a generator. |
| test/test_auth.py | Simplify authentication thread creation. |
| test/test_auth_oidc.py | Simplify concurrent runner creation. |
| test/asynchronous/unified_format.py | Simplify command-event filtering. |
| test/asynchronous/test_discovery_and_monitoring.py | Simplify concurrent runner creation. |
| test/asynchronous/test_custom_types.py | Build decimal test documents with a comprehension. |
| test/asynchronous/test_concurrency.py | Build task coroutines with a comprehension. |
| test/asynchronous/test_client_bulk_write.py | Simplify write-model creation and event filtering. |
| test/asynchronous/test_change_stream.py | Generate collection names with a comprehension. |
| test/asynchronous/test_bulk.py | Extend bulk documents from a generator. |
| test/asynchronous/test_auth.py | Simplify authentication thread creation. |
| test/asynchronous/test_auth_oidc.py | Simplify concurrent runner creation. |
| pyproject.toml | Enable Ruff’s PERF rules. |
| pymongo/uri_parser_shared.py | Merge remaining options with dict.update. |
| pymongo/synchronous/cursor.py | Extend bounded batches using a comprehension. |
| pymongo/synchronous/command_cursor.py | Extend bounded command batches using a comprehension. |
| pymongo/encryption_options.py | Build option documents with dictionary comprehensions. |
| pymongo/common.py | Extend version padding from a generator. |
| pymongo/client_options.py | Simplify TLS option collection. |
| pymongo/asynchronous/cursor.py | Extend bounded batches using a comprehension. |
| pymongo/asynchronous/command_cursor.py | Extend bounded command batches using a comprehension. |
| gridfs/synchronous/grid_file.py | Build bounded cursor results with a comprehension. |
| gridfs/asynchronous/grid_file.py | Build bounded cursor results with an awaited comprehension. |
| bson/son.py | Build representation fragments with a comprehension. |
| .evergreen/scripts/generate_config.py | Extend CI tasks from a generator. |
Review effort: Balanced
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.

PYTHON-6275
Changes in this PR
Enable all ruff
PERFrules and make the code changes required to pass them. All of these required changes are replacing loops with list/dict comprehensions.Test Plan
Existing test suite.
Checklist
Checklist for Author
[ ] Did you update the changelog (if necessary)?[ ] Is there test coverage?[ ] Is any followup work tracked in a JIRA ticket? If so, add link(s).Checklist for Reviewer