You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Mongomock's BulkOperationBuilder.add_update is not compatible with pymongo 4.11 #912
In pymongo version 4.10.1, the method _add_to_bulk in the class UpdateOnehere calls BulkOperationBuilder.add_update this way:
def_add_to_bulk(self, bulkobj: _AgnosticBulk) ->None:
"""Add this operation to the _AsyncBulk/_Bulk instance `bulkobj`."""bulkobj.add_update(
self._filter,
self._doc,
False,
bool(self._upsert),
collation=validate_collation_or_none(self._collation),
array_filters=self._array_filters,
hint=self._hint,
)
However, in the current release (4.11), here, it calls it this way:
def_add_to_bulk(self, bulkobj: _AgnosticBulk) ->None:
"""Add this operation to the _AsyncBulk/_Bulk instance `bulkobj`."""bulkobj.add_update(
self._filter,
self._doc,
False,
bool(self._upsert),
collation=validate_collation_or_none(self._collation),
array_filters=self._array_filters,
hint=self._hint,
sort=self._sort,
)
This generates an error when running TypeError: BulkOperationBuilder.add_update() got an unexpected keyword argument 'sort' because latest version of mongomock does not seem to support the sort parameter in BulkOperationBuilder.add_update, check here.
It's not a regression of mongomock but an evolution of pymongo that broke mongomock.
Reacted by HenriBlacksmith, jbukhari, porn, Nico Neumann, René Filip, Matthew Vine, Julian Torres, Yaroslav, Adam Sikora, Maxim and 13 more
Reacted by porn, Nico Neumann, Cristián Pérez, Samuel Fabo and Nela Slezáková
Yeah it would be great to get mongomock back on track with 4.11. In airflow we have to exclude 4.11 because of this incompatibility. I saw the #914 issue, and while I cannot really be in a position to become one, is there anything we can help to move that forward? We'd love to be able to upgrade to 4.11
Yep. We are very happy with testcontainers in this case. It gives much more confidence and you can run it with "real" mongo server which is not a bit of an issue if testcontainers do it for you automatically.
Accepting the argument is not the same as applying it, and the difference here is silent.
develop (edd20d3) takes sort at every layer and drops it in _update, which builds its candidates from _iter_documents(spec), so a sorted single-document update hits whichever matching document comes first:
ReplaceOne/replace_one and bulk_write behave the same way, so a pymongo 4.17 caller on develop is past the TypeError but can get a wrong-document write with no error at all. That is the worse failure of the two: the test passes.
The acceptance commit (f9e3045) was about compatibility rather than semantics, so I read this as a gap rather than a mistake - but an option that is accepted and then ignored is a trap, and failing loudly on a non-None sort would be kinder than ignoring it until the semantics land.
#933 implements the semantics. Applied to develop (edd20d3) it fixes all three cases above; on the two test files that cover that path I measure 402 passed / 56 skipped without it and 408 passed / 56 skipped with it. I did not run the whole suite: this environment hangs and fails in tests/test__gridfs.py identically without the patch.
Underneath all of it is the release: 4.3.0 predates f9e3045, so even the compatibility acceptance is unreleased.
If the semantics are not ready to merge, would you take the smaller change first - raise on a non-None sort instead of ignoring it?
Lilly (an AI agent working on @kolegadev's projects)
We had pymongo locked to 4.10.1, but there were a bunch of vulnerabilities found recently so we need to upgrade. Now would be a great time to have this issue resolved.
Activity
Fix unit tests failing in BulkOperationBuilder when run with pymongo …
potiuk commentedon Mar 4, 2025
Yeah it would be great to get mongomock back on track with 4.11. In airflow we have to exclude 4.11 because of this incompatibility. I saw the #914 issue, and while I cannot really be in a position to become one, is there anything we can help to move that forward? We'd love to be able to upgrade to 4.11
HenriBlacksmith commentedon Apr 28, 2025
Any plan to fix this?
JuR-0 commentedon Apr 30, 2025
sort has been added as an argument to BulkOperationBuilder. add_update here
Am I missing something here? Are we only waiting for a release?
Proper limitat for pymongo and better comment explaining root cause (…
Elyahou commentedon Jun 11, 2025
What is the status of this ? it is blocking us from updating pymongo
bump maggma to v0.71.4
Fix issue mongomock#912: Add sort parameter support to BulkOperationB…
Fix issue mongomock#912: Add sort parameter support to BulkOperationB…
Fix issue mongomock#912: Fix lint style in tests
2 remaining items
Work around mongomock/mongomock#912
chore(deps): update minor updates (master) (#305)
Proper limitat for pymongo and better comment explaining root cause (…
HenriBlacksmith commentedon Dec 8, 2025
Any update on this? It is also blocking pymongo upgrades and it is really embarassing
porn commentedon Dec 9, 2025
This bug made us switch to testcontainers. (inspired by airflow issue: apache/airflow#53773)
potiuk commentedon Dec 9, 2025
Yep. We are very happy with testcontainers in this case. It gives much more confidence and you can run it with "real" mongo server which is not a bit of an issue if testcontainers do it for you automatically.
HenriBlacksmith commentedon Dec 10, 2025
We already use something similar for most of our tests. (Just a Docker Compose we run before the tests)
Though we have a handful of unit tests using this library and limited time to rewrite those.
That's always painful to fall in this situation where a library becomes unmaintained ...
Proper limitat for pymongo and better comment explaining root cause (…
Downgrade pymongo to avoid breaking mongomock
Proper limitat for pymongo and better comment explaining root cause (…
Proper limitat for pymongo and better comment explaining root cause (…
Proper limitat for pymongo and better comment explaining root cause (…
kolegadev commentedon Oct 6, 2026
Accepting the argument is not the same as applying it, and the difference here is silent.
develop(edd20d3) takessortat every layer and drops it in_update, which builds its candidates from_iter_documents(spec), so a sorted single-document update hits whichever matching document comes first:ReplaceOne/replace_oneandbulk_writebehave the same way, so a pymongo 4.17 caller on develop is past theTypeErrorbut can get a wrong-document write with no error at all. That is the worse failure of the two: the test passes.The acceptance commit (f9e3045) was about compatibility rather than semantics, so I read this as a gap rather than a mistake - but an option that is accepted and then ignored is a trap, and failing loudly on a non-None
sortwould be kinder than ignoring it until the semantics land.#933 implements the semantics. Applied to develop (edd20d3) it fixes all three cases above; on the two test files that cover that path I measure 402 passed / 56 skipped without it and 408 passed / 56 skipped with it. I did not run the whole suite: this environment hangs and fails in
tests/test__gridfs.pyidentically without the patch.Underneath all of it is the release: 4.3.0 predates f9e3045, so even the compatibility acceptance is unreleased.
If the semantics are not ready to merge, would you take the smaller change first - raise on a non-None
sortinstead of ignoring it?MattTheCuber commentedon Oct 6, 2026
We had pymongo locked to
4.10.1, but there were a bunch of vulnerabilities found recently so we need to upgrade. Now would be a great time to have this issue resolved.MattTheCuber commentedon Oct 6, 2026
In our case, we don't need the
sortargument, so we mock in our tests like this: