Repository navigation
PYTHON-6274 Enable ruff rule PLR2004 - #3225
aclark4life wants to merge 3 commits into
Conversation
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
|
Test failures, please fix before I review. |
| _INT8_MIN = -128 | ||
| _INT8_MAX = 127 | ||
| # Packed-bit vectors are restricted to the signed int8 range when built from numpy arrays. | ||
| _PACKED_BIT_MAX = 127 |
There was a problem hiding this comment.
We don't need two different constants in one file with the same value.
| vector = vector.astype(np.dtype("float32"), copy=False) | ||
| elif dtype == BinaryVectorDtype.INT8: | ||
| if vector.min() >= -128 and vector.max() <= 127: | ||
| if vector.min() >= _INT8_MIN and vector.max() <= _INT8_MAX: |
There was a problem hiding this comment.
This is a behavior change that should be done in a separate ticket.
| from typing import Any | ||
| from urllib.parse import quote | ||
|
|
||
| _HTTP_OK = 200 |
There was a problem hiding this comment.
This same constant is defined 5 times. We should define it once and re-use the same definition across all its usages.
| _NEAREST = 4 | ||
|
|
||
| # MongoDB 5.0, the first server that supports agg $out/$merge with secondary read preference. | ||
| _MIN_AGG_WRITE_WIRE_VERSION = 13 |
There was a problem hiding this comment.
This duplicates the clearer WIRE_VERSION_5_0 = 13.
| BSONMAX = b"\x7f" # Max key | ||
|
|
||
| _INT32_MIN = -(2**31) | ||
| _INT32_MAX = 2**31 - 1 |
There was a problem hiding this comment.
This differs from json_util.py's _INT32_MAX = 2**31. There should be one definition shared across all usages.
| ] | ||
| ) | ||
|
|
||
| # Server code raised for findAndModify with upsert and other duplicate key errors. |
There was a problem hiding this comment.
This should be above the definition of _DUPLICATE_KEY_CODES and not _DUPLICATE_KEY_CODE.
|
|
||
| # Server error codes. | ||
| _MAX_TIME_MS_EXPIRED_CODE = 50 | ||
| _MMAPV1_TRANSACTION_NUMBER_CODE = 20 |
There was a problem hiding this comment.
| _MMAPV1_TRANSACTION_NUMBER_CODE = 20 | |
| _ILLEGAL_OPERATION_CODE = 20 |
Error code 20 is IllegalOperation.
| _BINARY_COMPONENT_COUNT = 2 | ||
| _REGEX_COMPONENT_COUNT = 2 | ||
| _DBREF_COMPONENT_COUNT = 2 | ||
| _SINGLE_FIELD_COUNT = 1 |
There was a problem hiding this comment.
We don't need this, the linting rule ignores -1, 0, and 1.
| _MAX_SUBTYPE_HEX_LENGTH = 2 | ||
| _BINARY_COMPONENT_COUNT = 2 | ||
| _REGEX_COMPONENT_COUNT = 2 | ||
| _DBREF_COMPONENT_COUNT = 2 |
There was a problem hiding this comment.
Using # noqa: PLR2004 on the lines that use these is probably more readable since these are all single-use.
| "RET", "ARG", "F405", "B028", "PGH001", "B018", "F403", "RUF015", "E731", "B007", | ||
| "UP031", "F401", "B023", "F811", "DTZ002", "LOG"] | ||
| "UP031", "F401", "B023", "F811", "DTZ002", "LOG", "PLR2004"] | ||
| "test/**/*.py" = ["PLR2004"] |
There was a problem hiding this comment.
We don't need to explicitly enable a rule in test if it's already enabled.

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