Skip to content

PYTHON-5981 Reject aggregate/pipeline as aggregation options - #2998

Merged
aclark4life merged 9 commits into
mongodb:mainfrom
aclark4life:PYTHON-5981
Aug 20, 2026
Merged

PYTHON-5981 Reject aggregate/pipeline as aggregation options#2998
aclark4life merged 9 commits into
mongodb:mainfrom
aclark4life:PYTHON-5981

Conversation

@aclark4life

@aclark4life aclark4life commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

PYTHON-5981

Changes in this PR

_AggregationCommand.__init__ now raises ConfigurationError if aggregate or pipeline is passed as a keyword option.

Both names are fields of the aggregate command itself, so previously passing them as options silently replaced the generated command's target namespace or its pipeline rather than adding an option. They now join explain as rejected option names, with the message The <name> option cannot be specified as a keyword argument.

This affects the helpers that build an aggregate command:

  • AsyncCollection.aggregate / Collection.aggregate
  • AsyncCollection.aggregate_raw_batches / Collection.aggregate_raw_batches
  • AsyncDatabase.aggregate / Database.aggregate
  • AsyncCollection.list_search_indexes / Collection.list_search_indexes

Test Plan

Added test_aggregate_reserved_options to test/asynchronous/test_collection.py covering aggregate= on all four helpers plus pipeline= on list_search_indexes, asserting ConfigurationError in each case.

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?

@aclark4life
aclark4life marked this pull request as ready for review August 18, 2026 20:36
@aclark4life
aclark4life requested a review from a team as a code owner August 18, 2026 20:36
@aclark4life
aclark4life requested review from blink1073 and a lite review from Copilot August 18, 2026 20:36
aclark4life and others added 8 commits August 18, 2026 16:38
The reserved-option tests only asserted that ConfigurationError is raised,
so one of them passed for the wrong reason:
list_search_indexes(aggregate=...) raises OperationFailure on a non-Atlas
server before the option is applied.

Assert the behavior instead. Command monitoring shows the supplied
namespace never reaches the wire, which holds on any topology, and
separate tests show a supplied pipeline can neither read ($unionWith)
nor overwrite ($out) another collection.
Both tests are blocked by the same ConfigurationError as
test_aggregate_reserved_options, so they cannot fail independently.
Co-authored-by: Noah Stapp <noah.stapp@mongodb.com>

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 hardens aggregation command construction by rejecting reserved aggregate-command fields (aggregate, pipeline) when supplied via keyword options, preventing user kwargs from silently overriding the generated command’s target namespace or pipeline.

Changes:

  • Added reserved-option validation in _AggregationCommand.__init__ for both async and generated sync implementations.
  • Added regression tests covering aggregate= across aggregation helpers and pipeline= for list_search_indexes (async + generated sync tests).
  • Documented the behavior change in the 4.18.0 changelog.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
test/test_collection.py Adds (generated sync) regression test for rejecting reserved aggregation options.
test/asynchronous/test_collection.py Adds source async regression test for rejecting reserved aggregation options.
pymongo/synchronous/aggregation.py Rejects aggregate/pipeline in aggregation options (generated sync).
pymongo/asynchronous/aggregation.py Rejects aggregate/pipeline in aggregation options (source async).
doc/changelog.rst Adds changelog entry for the new ConfigurationError behavior (note: title formatting needs correction per review comment).
Suppressed comments (1)

doc/changelog.rst:3

  • The top-level RST title underline is too short ("=="), and the blank line after the title was removed. This is likely to trigger a Sphinx/docutils error like "Title underline too short" and break the docs build. Restore the proper underline length and the blank line after the title.
Changelog
=========


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

@codecov

codecov Bot commented Aug 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Comment thread test/asynchronous/test_collection.py Outdated
@aclark4life
aclark4life requested a review from blink1073 August 20, 2026 00:46

@blink1073 blink1073 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@aclark4life
aclark4life merged commit fccedb2 into mongodb:main Aug 20, 2026
86 of 90 checks passed
@aclark4life
aclark4life deleted the PYTHON-5981 branch August 20, 2026 01:37
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