Skip to content

PYTHON-5676 Consolidate Command Execution Logic#2789

Draft
aclark4life wants to merge 4 commits intomongodb:masterfrom
aclark4life:PYTHON-5676
Draft

PYTHON-5676 Consolidate Command Execution Logic#2789
aclark4life wants to merge 4 commits intomongodb:masterfrom
aclark4life:PYTHON-5676

Conversation

@aclark4life
Copy link
Copy Markdown
Contributor

PYTHON-5676

Changes in this PR

Test Plan

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?

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 5, 2026

Codecov Report

❌ Patch coverage is 96.55172% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.13%. Comparing base (575d75f) to head (310caa4).

Files with missing lines Patch % Lines
pymongo/asynchronous/bulk.py 85.71% 1 Missing ⚠️
pymongo/asynchronous/client_bulk.py 85.71% 1 Missing ⚠️
pymongo/asynchronous/cursor.py 92.30% 0 Missing and 1 partial ⚠️
pymongo/asynchronous/network.py 98.41% 0 Missing and 1 partial ⚠️
pymongo/synchronous/bulk.py 85.71% 1 Missing ⚠️
pymongo/synchronous/client_bulk.py 85.71% 1 Missing ⚠️
pymongo/synchronous/cursor.py 92.30% 0 Missing and 1 partial ⚠️
pymongo/synchronous/network.py 98.41% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2789      +/-   ##
==========================================
+ Coverage   82.43%   88.13%   +5.70%     
==========================================
  Files         140      142       +2     
  Lines       24175    24290     +115     
  Branches     4131     4119      -12     
==========================================
+ Hits        19928    21408    +1480     
+ Misses       3369     2009    -1360     
+ Partials      878      873       -5     
Flag Coverage Δ
auth-aws-rhel8-test-auth-aws-rapid-web-identity-python3.14-cov 35.18% <31.89%> (+0.17%) ⬆️
auth-aws-win64-test-auth-aws-rapid-web-identity-python3.14-cov 35.18% <31.89%> (+0.17%) ⬆️
auth-enterprise-macos-test-standard-auth-latest-python3.11-auth-ssl-sharded-cluster-cov 44.00% <65.51%> (?)
auth-enterprise-rhel8-test-standard-auth-latest-python3.11-auth-ssl-sharded-cluster-cov 44.00% <65.51%> (+0.25%) ⬆️
auth-enterprise-win64-test-standard-auth-latest-python3.11-auth-ssl-sharded-cluster-cov 44.03% <65.51%> (+0.22%) ⬆️
auth-oidc-local-ubuntu-22-test-auth-oidc-default 48.77% <68.96%> (?)
compression-snappy-rhel8-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.55% <64.65%> (+0.23%) ⬆️
compression-snappy-rhel8-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.73% <65.51%> (+0.24%) ⬆️
compression-snappy-rhel8-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.33% <62.50%> (+0.25%) ⬆️
compression-snappy-rhel8-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.20% <65.08%> (+0.22%) ⬆️
compression-zlib-rhel8-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.56% <64.65%> (+0.21%) ⬆️
compression-zlib-rhel8-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.82% <65.51%> (+0.37%) ⬆️
compression-zlib-rhel8-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.33% <62.50%> (+0.26%) ⬆️
compression-zlib-rhel8-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.20% <65.08%> (+0.20%) ⬆️
compression-zstd-rhel8-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.55% <64.65%> (+0.22%) ⬆️
compression-zstd-rhel8-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.76% <65.51%> (+0.31%) ⬆️
compression-zstd-rhel8-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.34% <62.50%> (+0.27%) ⬆️
compression-zstd-rhel8-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.22% <65.08%> (+0.25%) ⬆️
compression-zstd-ubuntu-22-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.18% <65.08%> (+0.22%) ⬆️
coverage-report-coverage-report 88.13% <96.55%> (+5.74%) ⬆️
disable-test-commands-rhel8-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.56% <64.65%> (+0.23%) ⬆️
disable-test-commands-rhel8-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.77% <65.51%> (+0.28%) ⬆️
disable-test-commands-rhel8-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.33% <62.50%> (+0.26%) ⬆️
disable-test-commands-rhel8-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.22% <65.08%> (+0.22%) ⬆️
encryption-crypt_shared-macos-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 52.88% <76.72%> (?)
encryption-crypt_shared-macos-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 54.65% <78.44%> (?)
encryption-crypt_shared-macos-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 54.50% <78.44%> (?)
encryption-crypt_shared-rhel8-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 52.89% <76.72%> (?)
encryption-crypt_shared-rhel8-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 54.58% <78.44%> (?)
encryption-crypt_shared-rhel8-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 54.50% <78.44%> (?)
encryption-crypt_shared-win64-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 52.82% <76.72%> (?)
encryption-crypt_shared-win64-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 54.62% <78.44%> (?)
encryption-crypt_shared-win64-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 54.56% <78.44%> (?)
encryption-macos-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 52.89% <76.72%> (?)
encryption-macos-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 54.70% <78.44%> (?)
encryption-macos-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 54.50% <78.44%> (?)
encryption-pyopenssl-rhel8-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 53.55% <76.72%> (?)
encryption-pyopenssl-rhel8-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 55.26% <78.44%> (?)
encryption-pyopenssl-rhel8-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 55.19% <78.44%> (?)
encryption-rhel8-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 52.88% <76.72%> (?)
encryption-rhel8-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 54.58% <78.44%> (?)
encryption-rhel8-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 54.48% <78.44%> (?)
encryption-win64-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 52.76% <76.72%> (?)
encryption-win64-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 54.70% <78.44%> (?)
encryption-win64-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 54.57% <78.44%> (?)
load-balancer-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 48.53% <71.55%> (?)
mongodb-latest-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 61.71% <62.50%> (+0.27%) ⬆️
mongodb-latest-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 59.55% <64.65%> (+0.19%) ⬆️
mongodb-latest-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 59.68% <49.56%> (+0.19%) ⬆️
mongodb-latest-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 59.79% <52.58%> (+0.21%) ⬆️
mongodb-latest-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 61.66% <65.51%> (+0.27%) ⬆️
mongodb-rapid-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 61.71% <62.50%> (+0.26%) ⬆️
mongodb-rapid-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 59.54% <64.65%> (+0.23%) ⬆️
mongodb-rapid-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 59.67% <49.56%> (+0.19%) ⬆️
mongodb-rapid-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 59.79% <52.58%> (+0.21%) ⬆️
mongodb-rapid-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 61.66% <65.51%> (+0.27%) ⬆️
mongodb-v4.2-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 57.41% <54.74%> (+0.26%) ⬆️
mongodb-v4.2-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 55.80% <57.32%> (+0.19%) ⬆️
mongodb-v4.2-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 57.20% <47.41%> (+0.16%) ⬆️
mongodb-v4.2-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 57.33% <50.43%> (+0.17%) ⬆️
mongodb-v4.2-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 57.55% <57.75%> (+0.21%) ⬆️
mongodb-v4.4-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 59.76% <60.34%> (+0.23%) ⬆️
mongodb-v4.4-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 57.77% <62.50%> (+0.21%) ⬆️
mongodb-v4.4-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 57.71% <47.41%> (+0.16%) ⬆️
mongodb-v4.4-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 57.81% <50.43%> (+0.18%) ⬆️
mongodb-v4.4-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 59.58% <63.36%> (+0.24%) ⬆️
mongodb-v5.0-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 59.96% <60.34%> (+0.23%) ⬆️
mongodb-v5.0-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 57.93% <62.50%> (+0.20%) ⬆️
mongodb-v5.0-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 57.91% <47.41%> (+0.16%) ⬆️
mongodb-v5.0-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 58.05% <50.43%> (+0.18%) ⬆️
mongodb-v5.0-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 59.82% <63.36%> (+0.25%) ⬆️
mongodb-v6.0-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 59.99% <60.34%> (+0.24%) ⬆️
mongodb-v6.0-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 57.95% <62.50%> (+0.21%) ⬆️
mongodb-v6.0-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 57.93% <47.41%> (+0.16%) ⬆️
mongodb-v6.0-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 58.10% <50.43%> (+0.18%) ⬆️
mongodb-v6.0-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 59.95% <63.36%> (+0.23%) ⬆️
mongodb-v7.0-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 60.00% <60.34%> (+0.23%) ⬆️
mongodb-v7.0-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 57.92% <62.50%> (+0.20%) ⬆️
mongodb-v7.0-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 57.98% <47.41%> (+0.17%) ⬆️
mongodb-v7.0-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 58.11% <50.43%> (+0.19%) ⬆️
mongodb-v7.0-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 59.98% <63.36%> (+0.27%) ⬆️
mongodb-v8.0-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 61.70% <62.50%> (+0.25%) ⬆️
mongodb-v8.0-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 59.54% <64.65%> (+0.22%) ⬆️
mongodb-v8.0-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 59.67% <49.56%> (+0.19%) ⬆️
mongodb-v8.0-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 59.79% <52.58%> (+0.21%) ⬆️
mongodb-v8.0-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 61.66% <65.51%> (+0.27%) ⬆️
no-c-ext-rhel8-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 60.77% <64.65%> (+0.25%) ⬆️
no-c-ext-rhel8-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 62.98% <65.51%> (+0.28%) ⬆️
no-c-ext-rhel8-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 62.55% <62.50%> (+0.27%) ⬆️
no-c-ext-rhel8-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 60.41% <65.08%> (+0.23%) ⬆️
ocsp-rhel8-test-ocsp-ecdsa-valid-cert-server-staples-latest-python3.14-cov 34.43% <22.84%> (?)
ocsp-rhel8-test-ocsp-rsa-valid-cert-server-staples-latest-python3.14-cov 34.43% <22.84%> (?)
pyopenssl-macos-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 62.06% <65.51%> (?)
pyopenssl-rhel8-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 62.03% <65.51%> (?)
pyopenssl-win64-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.93% <65.51%> (?)
stable-api-accept-v2-rhel8-auth-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.55% <64.65%> (+0.21%) ⬆️
stable-api-accept-v2-rhel8-auth-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.22% <65.08%> (+0.24%) ⬆️
stable-api-require-v1-rhel8-auth-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.54% <64.22%> (+0.24%) ⬆️
stable-api-require-v1-rhel8-auth-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.18% <62.06%> (+0.26%) ⬆️
stable-api-require-v1-rhel8-auth-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.17% <64.65%> (+0.21%) ⬆️
storage-inmemory-rhel8-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.55% <64.65%> (+0.19%) ⬆️
storage-inmemory-rhel8-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.22% <65.08%> (+0.22%) ⬆️
test-macos-arm64-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.53% <64.65%> (+0.21%) ⬆️
test-macos-arm64-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.75% <65.51%> (+0.24%) ⬆️
test-macos-arm64-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.32% <62.50%> (+0.26%) ⬆️
test-macos-arm64-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.18% <65.08%> (+0.21%) ⬆️
test-macos-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.55% <64.65%> (?)
test-macos-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.71% <65.51%> (?)
test-macos-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.32% <62.50%> (+0.26%) ⬆️
test-macos-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.19% <65.08%> (?)
test-numpy-macos-arm64-test-numpy-python3.14-python3.14-cov 32.78% <10.34%> (+0.15%) ⬆️
test-numpy-macos-test-numpy-python3.14-python3.14-cov 32.78% <10.34%> (?)
test-numpy-rhel8-test-numpy-python3.14-python3.14-cov 32.78% <10.34%> (+0.17%) ⬆️
test-numpy-win32-test-numpy-python3.14-python3.14-cov 32.76% <10.34%> (+0.17%) ⬆️
test-numpy-win64-test-numpy-python3.14-python3.14-cov 32.76% <10.34%> (+0.16%) ⬆️
test-win32-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.41% <64.65%> (+0.22%) ⬆️
test-win32-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.70% <65.51%> (+0.27%) ⬆️
test-win32-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.26% <62.50%> (+0.22%) ⬆️
test-win32-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.07% <65.08%> (+0.22%) ⬆️
test-win64-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.41% <64.65%> (+0.22%) ⬆️
test-win64-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.69% <65.51%> (+0.27%) ⬆️
test-win64-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.29% <62.50%> (+0.24%) ⬆️
test-win64-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.07% <65.08%> (+0.23%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

…ion()

Since MIN_SUPPORTED_WIRE_VERSION=8 (MongoDB 4.2+), use_command() always
returns True and OP_MSG is always used. Remove the unreachable OP_QUERY
branches from Server.run_operation(), along with the dead else-branches
in Cursor and CommandCursor that referenced _OpReply.

- Hard-code use_cmd=True / apply_timeout=True in run_operation()
- Always use user_fields=_CURSOR_DOC_FIELDS and legacy_response=False
- Remove isinstance(reply, _OpMsg) exhaust check (reply is always _OpMsg)
- Hard-code from_command=True in Response/PinnedResponse construction
- Remove dead else-branches with assert isinstance(response.data, _OpReply)
  in Cursor._send_message and CommandCursor._send_message
- Add scope document PYTHON-5676-scope.md outlining all four phases
Eliminate the ~13-line _COMMAND_LOGGER/_debug_log boilerplate that was
copy-pasted identically across five command execution paths. Add three
helpers to pymongo/command_helpers.py and call them from all five sites.

- New pymongo/command_helpers.py: _log_command_started, _log_command_succeeded,
  _log_command_failed — pure functions, no I/O, no async/sync split needed
- network.py, server.py, bulk.py, client_bulk.py: replace inline _debug_log
  blocks with helper calls; remove _COMMAND_LOGGER/_CommandStatusMessage
  imports where no longer needed
Extract _network_command_core() from network.command() to handle the
common send/receive/APM/decrypt pipeline. Both the standard command
path and the cursor (find/getMore) path now use the same core function.

- New _network_command_core() in network.py: takes a pre-built message
  and handles APM, send_message, receive_message, unpack, process,
  check, decrypt — returns (docs, reply, duration)
- command() is now a thin wrapper: encode → _network_command_core → return docs[0]
- run_operation() is now a thin wrapper: build message → _network_command_core
  → wrap in Response/PinnedResponse
- _CURSOR_DOC_FIELDS moved from server.py to network.py (only used in core)
- server.py drops _decode_all_selective, _check_command_response,
  _convert_exception, NotPrimaryError, OperationFailure imports
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.

2 participants