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
5 changes: 4 additions & 1 deletion sqlite_utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -4235,7 +4235,10 @@ def convert_value(v):
fn_name = getattr(fn, "__name__", "fn")
if fn_name == "<lambda>":
fn_name = f"lambda_{abs(hash(fn))}"
self.db.register_function(convert_value, name=fn_name)
# replace=True: a previous .convert() may have registered a
# different callable under the same name, e.g. two bound methods
# called "get" or two functools.partial objects
self.db.register_function(convert_value, name=fn_name, replace=True)
sql = "update {} set {sets}{where};".format(
quote_identifier(self.name),
sets=", ".join(
Expand Down
19 changes: 19 additions & 0 deletions tests/test_convert.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import functools

import pytest

from sqlite_utils.db import BadMultiValues
Expand Down Expand Up @@ -158,3 +160,20 @@ def test_convert_repeated(fresh_db):
table.convert(col, lambda x: x * 2)
table.convert(col, lambda _x: 0)
assert table.get(1) == {col: 0}


def test_convert_repeated_callables_with_same_name(fresh_db):
# Different callables can share a name: bound methods such as dict.get,
# or functools.partial objects, which have no __name__ at all
table = fresh_db.table("places")
table.insert({"id": 1, "state": "CA", "country": "US"}, pk="id")
table.convert("state", {"CA": "California"}.get)
table.convert("country", {"US": "United States"}.get)
assert table.get(1) == {"id": 1, "state": "California", "country": "United States"}
table.convert("state", functools.partial("{}!".format))
table.convert("country", functools.partial("{}?".format))
assert table.get(1) == {
"id": 1,
"state": "California!",
"country": "United States?",
}
Loading