From cab088f7659ecbc0a8602e152895e7c288faf844 Mon Sep 17 00:00:00 2001 From: Zihan Dai Date: Tue, 20 Oct 2026 15:42:30 +1100 Subject: [PATCH] Replace previously registered convert functions with the same name --- sqlite_utils/db.py | 5 ++++- tests/test_convert.py | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index c011d9bb0..f5e9ac263 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -4235,7 +4235,10 @@ def convert_value(v): fn_name = getattr(fn, "__name__", "fn") if fn_name == "": 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( diff --git a/tests/test_convert.py b/tests/test_convert.py index 1f9e9ed15..fff1faf21 100644 --- a/tests/test_convert.py +++ b/tests/test_convert.py @@ -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?", + }