This commit is contained in:
ZIHAN DAI 2026-10-20 15:42:51 +11:00 • committed by GitHub
commit 3775d98747
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 23 additions and 1 deletions

View file

@ -4235,7 +4235,10 @@ class Table(Queryable):
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(

View file

@ -1,3 +1,5 @@
import functools
import pytest
from sqlite_utils.db import BadMultiValues
@ -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?",
}