Skip missing doc_ids in Table.update and Table.remove (fixes #591)#616
Merged
msiemens merged 1 commit intoMay 21, 2026
Merged
Conversation
`Table.get(doc_id=N)` and `Table.get(doc_ids=[N])` already return silently when the requested document does not exist, but `Table.update(doc_ids=[N])` and `Table.remove(doc_ids=[N])` raise `KeyError`. The four code paths are documented and named symmetrically, so the asymmetric behaviour is surprising and was raised as a bug in issue msiemens#591. Worse, when a mix of existing and missing IDs is passed to `update`, the first existing documents are mutated in place before the `KeyError` is raised. The storage write is then skipped by `_update_table`, but documents are shared by reference between the in-memory copy and the table dict, so subsequent reads still see the half-applied update. The operation is neither atomic nor recoverable. Filter the requested IDs against the live table *inside* the updater, before performing any side effects, and return only the IDs that were actually updated/removed. This: * makes the four `get`/`update`/`remove` paths consistent; * makes both operations atomic - either every existing target is affected, or none is; * lets `upsert` drop its now-unreachable `except KeyError` branch (`update` simply returns `[]` when nothing matched, which falls through the existing `if updated_docs:` guard into the insert path). Regression coverage is added in `tests/test_tinydb.py` for the missing, mixed and cross-method-consistency cases. Fixes msiemens#591
Owner
|
Thanks for implementing this! |
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.
Fixes #591.
What this changes
Table.get(doc_id=N)andTable.get(doc_ids=[N])already return silently when the requested document doesn't exist (returningNoneand[]respectively), butTable.update(doc_ids=[N])andTable.remove(doc_ids=[N])raiseKeyError. The four code paths are documented and named symmetrically, so the asymmetric behaviour is surprising — that's the inconsistency #591 reported.There's also a subtler correctness issue: when a mix of existing and missing IDs is passed to
update, the first existing documents get mutated in place before theKeyErroris raised._update_tableskips the storage write on the exception path, but the document dicts inside the table are shared by reference with the in-memory mapping, so subsequent reads still observe the half-applied update. Repro onmaster:Approach
Filter the requested IDs against the live table inside the updater, before performing any side effects, then return only the IDs that were actually affected. This:
get/update/removepaths consistent — all four silently skip missing IDs.upsertdrop its now-unreachableexcept KeyErrorbranch —updatesimply returns[]when nothing matched, which already falls through the existingif updated_docs:guard into the insert path.I considered the alternative of making
get(doc_id=N)raise too. Rejected becauseget's docstring explicitly promisesReturns None if the document doesn't exist, so changing it would be a real breaking change to a documented contract. The behaviour added here matches the existingget(doc_ids=...)filtering convention.Behaviour after this PR
Compatibility
User code that catches
KeyErroraroundupdate(doc_ids=...)/remove(doc_ids=...)to detect missing IDs will need to switch to inspecting the returned list (e.g.if len(db.update(..., doc_ids=ids)) != len(ids): ...). That seems strictly better — the returned list is the documented and forward-compatible signal — and it lines up with howget(doc_ids=...)already works.Tests
Added regression tests in
tests/test_tinydb.py(each runs against bothMemoryStorageandJSONStoragevia the existingdbfixture):test_remove_ids_missing/test_update_ids_missing— single missing ID returns[], no exception, no state change.test_remove_ids_mixed/test_update_ids_mixed— mix of existing and missing IDs only affects the existing ones (atomic).test_doc_id_missing_consistency—get,updateandremoveall return without raising for a missing ID.Full suite:
pytest tests/→ 215 passed (was 205). Type check:pytest --mypy -m mypy tinydb tests→ clean.