PYTHON-6013 Consolidate session/transaction helpers into a shared file - #3019
Merged
Conversation
Contributor
There was a problem hiding this comment.
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.pyto 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
_TxnStateand 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.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
sleepyStick
approved these changes
Aug 31, 2026
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-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 any followup work tracked in a JIRA ticket? If so, add link(s).Checklist for Reviewer