Skip to content

PYTHON-6244 Enable ruff TRY rules - #3204

Merged
NoahStapp merged 4 commits into
mongodb:mainfrom
NoahStapp:PYTHON-6244
Nov 11, 2026
Merged

NoahStapp merged 4 commits into
mongodb:mainfrom
NoahStapp:PYTHON-6244

Conversation

@NoahStapp

@NoahStapp NoahStapp commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

PYTHON-6244

Changes in this PR

Enable ruff's TRY linting rules and make the necessary code changes to have them pass.

Test Plan

Existing test suite. Passing run of affected OIDC tests.

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?

@NoahStapp
NoahStapp requested a review from a team as a code owner November 11, 2026 16:38
@NoahStapp
NoahStapp requested review from sleepyStick and a balanced review from Copilot November 11, 2026 16:38

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

Exception changes from ValueError to TypeError leave existing session, server-selector, and OIDC test assertions incompatible; update the asynchronous assertions and regenerate their synchronous counterparts.

3 open issues
What changed in this PR

Enables Ruff’s TRY rules and updates exception handling across the driver, tests, and development tools.

Changes:

  • Removes the blanket TRY exclusion and adds targeted suppressions.
  • Uses more specific exception types for validation and failure cases.
  • Replaces explicit exception re-raises with bare raise statements.
File Description
tools/​synchro.py Uses a bare re-raise for missing sync markers.
tools/​fail_if_no_c.py Adds targeted exception-logging suppressions.
test/​utils.py Mirrors retry re-raise cleanup.
test/​unified_format_shared.py Uses ValueError for unsupported events.
test/​test_sfp.py Mirrors environment-validation exception changes.
test/​test_load_balancer.py Mirrors timeout exception changes.
test/​test_examples.py Mirrors the transaction-example suppression.
test/​test_client.py Mirrors the connection-failure exception change.
test/​helpers.py Mirrors the helper’s RuntimeError change.
test/​atlas/​test_connection.py Uses ValueError for missing configuration.
test/​asynchronous/​utils.py Uses a bare re-raise after exhausted retries.
test/​asynchronous/​test_sfp.py Uses ValueError for missing environment variables.
test/​asynchronous/​test_load_balancer.py Uses TimeoutError for unlock timeouts.
test/​asynchronous/​test_examples.py Preserves illustrative error handling with a suppression.
test/​asynchronous/​test_client.py Uses ConnectionFailure in the connection stub.
test/​asynchronous/​helpers.py Uses RuntimeError for helper failures.
pyproject.toml Enables Ruff’s TRY rules.
pymongo/​synchronous/​monitor.py Mirrors monitor exception changes and suppression.
pymongo/​synchronous/​collection.py Mirrors return-document validation changes.
pymongo/​synchronous/​auth_oidc.py Mirrors callback-result validation changes.
pymongo/​pyopenssl_context.py Uses a bare re-raise for nonblocking I/O errors.
pymongo/​common.py Uses TypeError for invalid argument types.
pymongo/​client_session_shared.py Uses TypeError for invalid cluster time.
pymongo/​asynchronous/​monitor.py Adds a specific SRV error and targeted suppression.
pymongo/​asynchronous/​collection.py Uses TypeError for invalid return-document types.
pymongo/​asynchronous/​auth_oidc.py Uses TypeError for invalid callback results.
bson/​codec_options.py Uses TypeError for invalid decode-handler types.

Review effort: Balanced

Comment thread pymongo/asynchronous/auth_oidc.py Resolved
Comment thread pymongo/client_session_shared.py Resolved
Comment thread pymongo/common.py Resolved
@NoahStapp
NoahStapp marked this pull request as draft November 11, 2026 16:57
@NoahStapp
NoahStapp marked this pull request as ready for review November 11, 2026 19:25
@NoahStapp
NoahStapp merged commit 131f4d7 into mongodb:main Nov 11, 2026
76 of 78 checks passed
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.

3 participants