Repository navigation
PYTHON-6274 Enable ruff rule PLR2004 - #3225
Open
aclark4life wants to merge 2 commits into
Open
aclark4life wants to merge 2 commits into
aclark4life wants to merge 2 commits into
Conversation
aclark4life
marked this pull request as ready for review
November 13, 2026 21:31
aclark4life
requested review from
NoahStapp
and
a balanced review from Copilot
November 13, 2026 21:31
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The timezone-offset length constants misleadingly describe the supported formats and need names and references that accurately distinguish ±HH:MM from ±HHMM.
1 open issue
What changed in this PR
This PR enables Ruff’s PLR2004 rule and replaces numeric comparison literals with named constants across BSON, the driver, and supporting scripts, without intending to change runtime behavior.
Changes:
- Enables the rule while exempting files under
test/. - Names protocol sizes, validation bounds, server feature thresholds, and error codes, including generated synchronous counterparts.
- Preserves literal tuple-length checks needed for type narrowing through targeted suppressions.
| File | Description |
|---|---|
tools/compare_import_time.py |
Names the import-time regression threshold. |
pyproject.toml |
Enables PLR2004 and adds test exemptions. |
pymongo/uri_parser_shared.py |
Names the maximum port value. |
pymongo/topology_description.py |
Uses the MongoDB 6.0 wire-version constant. |
pymongo/synchronous/srv_resolver.py |
Mirrors DNS label-count constants. |
pymongo/synchronous/encryption.py |
Mirrors the encrypted subtype constant. |
pymongo/synchronous/collection.py |
Mirrors feature-gate and error-code constants. |
pymongo/synchronous/client_bulk.py |
Mirrors the bulk-write wire-version constant. |
pymongo/synchronous/bulk.py |
Mirrors the update-sort wire-version constant. |
pymongo/synchronous/auth.py |
Mirrors the minimum SCRAM iteration constant. |
pymongo/server_description.py |
Names the retryable-read wire-version threshold. |
pymongo/read_preferences.py |
Names the secondary aggregation-write threshold. |
pymongo/ocsp_support.py |
Names the successful HTTP status code. |
pymongo/network_layer.py |
Names message-header sizes and compression opcode. |
pymongo/mongo_client_shared.py |
Reuses the MMAPv1 transaction error constant. |
pymongo/helpers_shared.py |
Names command and write error codes. |
pymongo/errors.py |
Defines shared server error-code constants. |
pymongo/cursor_shared.py |
Exempts a tuple-length check for type narrowing. |
pymongo/compression_support.py |
Names the maximum zlib compression level. |
pymongo/common.py |
Adds wire-version and validation constants. |
pymongo/client_session_shared.py |
Uses timeout and snapshot-read constants. |
pymongo/bulk_shared.py |
Reuses the MMAPv1 transaction error constant. |
pymongo/asynchronous/srv_resolver.py |
Names DNS label-count thresholds. |
pymongo/asynchronous/encryption.py |
Names the encrypted BSON subtype. |
pymongo/asynchronous/collection.py |
Uses feature-gate and namespace-error constants. |
pymongo/asynchronous/client_bulk.py |
Uses the bulk-write wire-version constant. |
pymongo/asynchronous/bulk.py |
Uses the update-sort wire-version constant. |
pymongo/asynchronous/auth.py |
Names the minimum SCRAM iteration count. |
pymongo/_kms_connect_shared.py |
Names HTTP validation and relay-completion constants. |
pymongo/_gcp_helpers.py |
Names the successful HTTP status code. |
pymongo/_client_bulk_shared.py |
Reuses the MMAPv1 transaction error constant. |
pymongo/_azure_helpers.py |
Names the successful HTTP status code. |
bson/objectid.py |
Names binary and hexadecimal ObjectId lengths. |
bson/json_util.py |
Names Extended JSON format lengths and counts. |
bson/decimal128.py |
Names byte length and preserves tuple narrowing. |
bson/binary.py |
Names subtype, UUID, vector, and padding bounds. |
bson/__init__.py |
Names integer/document bounds and reuses a subtype constant. |
.evergreen/scripts/test_generate_config.py |
Names task-count expectations. |
.evergreen/scripts/setup_tests.py |
Names the successful download status code. |
.evergreen/scripts/mod_wsgi_matrix.py |
Names the expected command-line argument count. |
Review effort: Balanced
This branch has not been deployed
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-6274
Changes in this PR
Enable ruff
PLR2004and replace magic numbers in comparisons with named constants inbson/,pymongo/,.evergreen/scripts/, andtools/.Tests keep the rule disabled through per-file ignores, because expected literals in tests are the specification.
Test Plan
ruff check .andruff format --check .just typingChecklist
Checklist for Author
Checklist for Reviewer