Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions tests/test_tinydb.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,26 @@ def transform(el):
assert db.count(where('int') == 1) == 2


def test_update_custom_transform_callable(db: TinyDB):
from collections.abc import MutableMapping

from tinydb.operations import delete as delete_field

doc_id = db.insert({'name': 'John', 'temp': True})

def remove_temp(doc: MutableMapping) -> None:
del doc['temp']

db.update(remove_temp, where('name') == 'John')

assert db.get(doc_id=doc_id) == {'name': 'John'}

db.update({'temp': True}, where('name') == 'John')
db.update(delete_field('temp'), where('name') == 'John')

assert db.get(doc_id=doc_id) == {'name': 'John'}


def test_update_ids(db: TinyDB):
db.update({'int': 2}, doc_ids=[1, 2])

Expand Down
8 changes: 4 additions & 4 deletions tinydb/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
data in TinyDB.
"""

from collections.abc import Callable, Iterable, Iterator, Mapping
from collections.abc import Callable, Iterable, Iterator, Mapping, MutableMapping
from typing import (
NoReturn,
Optional,
Expand Down Expand Up @@ -403,7 +403,7 @@ def contains(

def update(
self,
fields: Union[Mapping, Callable[[Mapping], None]],
fields: Union[Mapping, Callable[[MutableMapping], None]],
cond: Optional[QueryLike] = None,
doc_ids: Optional[Iterable[int]] = None,
) -> list[int]:
Expand All @@ -415,7 +415,7 @@ def update(
the IDs that were actually updated.

:param fields: the fields that the matching documents will have
or a method that will update the documents
or a callable that mutates matching documents in place
:param cond: which documents to update
:param doc_ids: a list of document IDs
:returns: a list containing the updated document's ID
Expand Down Expand Up @@ -508,7 +508,7 @@ def updater(table: dict):
def update_multiple(
self,
updates: Iterable[
tuple[Union[Mapping, Callable[[Mapping], None]], QueryLike]
tuple[Union[Mapping, Callable[[MutableMapping], None]], QueryLike]
],
) -> list[int]:
"""
Expand Down