Skip to content

PYTHON-5856 Move cursor execution logic into cursor class#2894

Draft
aclark4life wants to merge 1 commit into
mongodb:masterfrom
aclark4life:PYTHON-5856
Draft

PYTHON-5856 Move cursor execution logic into cursor class#2894
aclark4life wants to merge 1 commit into
mongodb:masterfrom
aclark4life:PYTHON-5856

Conversation

@aclark4life

Copy link
Copy Markdown
Contributor

PYTHON-5856

Changes in this PR

Moves the command preparation and response construction that lived in Server.run_operation into _AsyncCursorBase._run_with_conn, completing the consolidation started in PYTHON-5676. Cursors now 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 — new home for all of Server.run_operation's logic
  • client._run_operation now accepts execute_fn(conn, op, read_preference) -> Response instead of routing through server.run_operation
  • Remove Server.run_operation, Server.operation_to_command, and Server._split_message entirely

Note: _run_with_conn still passes address=conn.address and start=datetime.datetime.now() to run_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_getmore to use the new execute_fn parameter name.

Checklist

Checklist for Author

  • Did you update the changelog (if necessary)? — Not necessary; internal refactor with no user-facing behavior change.
  • Is there test coverage? — Covered by existing spec suites.
  • Is any followup work tracked in a JIRA ticket? If so, add link(s). — PYTHON-5745 (PYTHON-5745 Consolidate command telemetry #2891) will clean up the address/start args once that PR merges.

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?

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

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 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/async server.py.
  • Introduced cursor-base helpers (_CURSOR_DOC_FIELDS, _split_message, _operation_to_command) and added _run_with_conn as the new execution path for cursor operations.
  • Updated MongoClient._run_operation (sync/async) to accept an execute_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-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.23404% with 12 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pymongo/asynchronous/cursor_base.py 86.36% 3 Missing and 3 partials ⚠️
pymongo/synchronous/cursor_base.py 86.36% 3 Missing and 3 partials ⚠️

📢 Thoughts on this report? Let us know!

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