Skip to content

PYTHON-6274 Enable ruff rule PLR2004 - #3225

Open
aclark4life wants to merge 2 commits into
mongodb:mainfrom
aclark4life:PYTHON-6274
Open

aclark4life wants to merge 2 commits into
mongodb:mainfrom
aclark4life:PYTHON-6274

Conversation

@aclark4life

Copy link
Copy Markdown
Contributor

PYTHON-6274

Changes in this PR

Enable ruff PLR2004 and replace magic numbers in comparisons with named constants in bson/, pymongo/, .evergreen/scripts/, and tools/.

Tests keep the rule disabled through per-file ignores, because expected literals in tests are the specification.

Test Plan

  • ruff check . and ruff format --check .
  • just typing
  • Targeted pytest runs: bson, binary, decimal128, objectid, json_util, uri_parser, network layer, compression, sessions, and SRV polling.

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

  • Does the title of the PR reference a JIRA Ticket?
  • Do you fully understand the implementation? (Would you be comfortable explaining how this code works to someone else?)
  • Is all relevant documentation (README or docstring) updated?

@aclark4life
aclark4life marked this pull request as ready for review November 13, 2026 21:31
@aclark4life
aclark4life requested a review from a team as a code owner November 13, 2026 21:31
@aclark4life
aclark4life requested review from NoahStapp and a balanced review from Copilot November 13, 2026 21:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 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

Comment thread bson/json_util.py Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are recorded for the PLR2004 and named-constant changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

This branch has not been deployed

No deployments
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