PYTHON-5856 Move cursor execution logic into cursor class#2894
Draft
aclark4life wants to merge 1 commit into
Draft
PYTHON-5856 Move cursor execution logic into cursor class#2894aclark4life wants to merge 1 commit into
aclark4life wants to merge 1 commit into
Conversation
Move the command preparation and response construction that lived in Server.run_operation into _AsyncCursorBase._run_with_conn, so cursors call run_cursor_command directly without routing through Server. - Extract _CURSOR_DOC_FIELDS, _split_message, and _operation_to_command as module-level helpers in cursor_base.py - Add abstract _unpack_response to _AsyncCursorBase to make the interface explicit - Add _run_with_conn (carrying the @_handle_reauth decorator) to _AsyncCursorBase; this is the new home for all of run_operation's logic - client._run_operation now accepts execute_fn(conn, op, rp) -> Response instead of routing through server.run_operation - Remove Server.run_operation, Server.operation_to_command, and Server._split_message entirely
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors cursor execution by moving the _Query/_GetMore command preparation + response construction logic out of Server.run_operation and into the cursor base classes (_AsyncCursorBase / sync equivalent). This continues the consolidation work so cursors execute via a cursor-owned execute_fn rather than routing through Server.
Changes:
- Removed
Server.run_operation(and related helpers) in both sync/asyncserver.py. - Introduced cursor-base helpers (
_CURSOR_DOC_FIELDS,_split_message,_operation_to_command) and added_run_with_connas the new execution path for cursor operations. - Updated
MongoClient._run_operation(sync/async) to accept anexecute_fn(conn, op, read_preference) -> Response, and updated cursor call sites + the stale getMore test accordingly.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| test/test_client.py | Updates stale getMore test to pass the new execute_fn callable (sync mirror). |
| test/asynchronous/test_client.py | Updates stale getMore test to pass the new execute_fn callable. |
| pymongo/synchronous/server.py | Removes cursor operation execution logic from Server. |
| pymongo/synchronous/mongo_client.py | Changes _run_operation to accept and invoke execute_fn instead of server.run_operation. |
| pymongo/synchronous/cursor.py | Switches cursor _send_message to route through _run_with_conn. |
| pymongo/synchronous/cursor_base.py | Adds _run_with_conn, module-level helpers, and an explicit abstract _unpack_response. |
| pymongo/synchronous/command_cursor.py | Switches command cursor _send_message to route through _run_with_conn. |
| pymongo/asynchronous/server.py | Removes cursor operation execution logic from Server. |
| pymongo/asynchronous/mongo_client.py | Changes _run_operation to accept and invoke execute_fn instead of server.run_operation. |
| pymongo/asynchronous/cursor.py | Switches cursor _send_message to route through _run_with_conn. |
| pymongo/asynchronous/cursor_base.py | Adds async _run_with_conn, module-level helpers, and an explicit abstract _unpack_response. |
| pymongo/asynchronous/command_cursor.py | Switches command cursor _send_message to route through _run_with_conn. |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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-5856
Changes in this PR
Moves the command preparation and response construction that lived in
Server.run_operationinto_AsyncCursorBase._run_with_conn, completing the consolidation started in PYTHON-5676. Cursors now callrun_cursor_commanddirectly without routing throughServer._CURSOR_DOC_FIELDS,_split_message, and_operation_to_commandas module-level helpers incursor_base.py_unpack_responseto_AsyncCursorBaseto make the interface explicit_run_with_conn(carrying the@_handle_reauthdecorator) to_AsyncCursorBase— new home for all ofServer.run_operation's logicclient._run_operationnow acceptsexecute_fn(conn, op, read_preference) -> Responseinstead of routing throughserver.run_operationServer.run_operation,Server.operation_to_command, andServer._split_messageentirelyNote:
_run_with_connstill passesaddress=conn.addressandstart=datetime.datetime.now()torun_cursor_command; these will be dropped as a trivial follow-up when PYTHON-5745 (#2891) merges.Test Plan
No new tests — this is a behavior-preserving refactor. The existing spec suites (APM/command-monitoring, auth, cursor) assert the exact behavior that any regression would change. Updated
test_stale_getmoreto use the newexecute_fnparameter name.Checklist
Checklist for Author
address/startargs once that PR merges.Checklist for Reviewer