Skip to content

PYTHON-6013 Consolidate session/transaction helpers into a shared file - #3019

Merged
NoahStapp merged 3 commits into
mongodb:mainfrom
NoahStapp:PYTHON-6013
Aug 31, 2026
Merged

PYTHON-6013 Consolidate session/transaction helpers into a shared file#3019
NoahStapp merged 3 commits into
mongodb:mainfrom
NoahStapp:PYTHON-6013

Conversation

@NoahStapp

Copy link
Copy Markdown
Contributor

PYTHON-6013

Changes in this PR

Consolidate code that does not need to be synchronized into a new pymongo/client_session_shared.py.

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

  • 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?

Copilot AI lite review requested due to automatic review settings August 31, 2026 18:12
@NoahStapp
NoahStapp requested a review from a team as a code owner August 31, 2026 18:12
@NoahStapp
NoahStapp requested a review from sleepyStick August 31, 2026 18:12

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.

Pull request overview

This PR refactors PyMongo’s session/transaction implementation by extracting logic that is identical across the synchronous and asynchronous stacks into a new shared module (pymongo/client_session_shared.py), then updates call sites and tests to import from the shared location.

Changes:

  • Added pymongo/client_session_shared.py to host shared session/transaction helpers (e.g., _TxnState, server session pool types, retry timing helpers, write-concern validation, and option classes).
  • Updated sync/async runtime modules (client_session, topology, pool, bulk/client_bulk, mongo_client) to import the moved helpers from the shared module.
  • Updated unified format and transaction tests (sync + async) to reference _TxnState and retry timeout constants from the new shared module.

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
pymongo/client_session_shared.py New shared module containing session/transaction helpers used by both sync and async code paths.
pymongo/synchronous/client_session.py Replaced locally-defined helpers/options with imports from client_session_shared and re-exported options for docs.
pymongo/asynchronous/client_session.py Same consolidation as sync: imports from client_session_shared and re-exported options for docs.
pymongo/synchronous/topology.py Switched server session pool imports to the shared module.
pymongo/asynchronous/topology.py Switched server session pool imports to the shared module.
pymongo/synchronous/pool.py Switched _validate_session_write_concern import to the shared module.
pymongo/asynchronous/pool.py Switched _validate_session_write_concern import to the shared module.
pymongo/synchronous/bulk.py Switched _validate_session_write_concern import to the shared module.
pymongo/asynchronous/bulk.py Switched _validate_session_write_concern import to the shared module.
pymongo/synchronous/client_bulk.py Switched _validate_session_write_concern import to the shared module.
pymongo/asynchronous/client_bulk.py Switched _validate_session_write_concern import to the shared module.
pymongo/synchronous/mongo_client.py Switched _EmptyServerSession (and typing-only _ServerSession) imports to the shared module.
pymongo/asynchronous/mongo_client.py Switched _EmptyServerSession (and typing-only _ServerSession) imports to the shared module.
test/unified_format.py Updated _TxnState import to come from client_session_shared.
test/asynchronous/unified_format.py Updated _TxnState import to come from client_session_shared.
test/test_transactions.py Updated retry-timeout patching to target client_session_shared._WITH_TRANSACTION_RETRY_TIME_LIMIT.
test/asynchronous/test_transactions.py Updated retry-timeout patching to target client_session_shared._WITH_TRANSACTION_RETRY_TIME_LIMIT.
Suppressed comments (1)

pymongo/client_session_shared.py:124

  • The TransactionOptions docstring says that when write_concern is None the client's read_preference is used, but the default is the client's write_concern; this is misleading in the rendered docs.
    :param write_concern: The
        :class:`~pymongo.write_concern.WriteConcern` to use for this
        transaction. If ``None`` (the default) the :attr:`read_preference` of
        the client is used.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread pymongo/client_session_shared.py
Comment thread pymongo/client_session_shared.py
@codecov

codecov Bot commented Aug 31, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.17989% with 11 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pymongo/client_session_shared.py 93.49% 5 Missing and 6 partials ⚠️

📢 Thoughts on this report? Let us know!

@NoahStapp
NoahStapp merged commit 09aa41b into mongodb:main Aug 31, 2026
90 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