From cffe30fd2fafa59db38989482097196398375786 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 20 Jul 2026 14:57:53 +0000 Subject: [PATCH] Fix ValueTracker skipping type evaluation for empty strings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `ValueTracker.evaluate()` used `if not value` to skip NULL values, but this also skipped empty strings (`""`), `0`, and other falsy values. A CSV column containing only empty strings was incorrectly inferred as INTEGER (the initial default) instead of TEXT. Replace the falsy check with an explicit `if value is None` so that only NULL is skipped and all other values — including `""` — are evaluated against the type tests. --- sqlite_utils/utils.py | 2 +- tests/test_utils.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/sqlite_utils/utils.py b/sqlite_utils/utils.py index b39b11764..a50dddd2c 100644 --- a/sqlite_utils/utils.py +++ b/sqlite_utils/utils.py @@ -490,7 +490,7 @@ def guessed_type(self) -> str: return "text" def evaluate(self, value: object) -> None: - if not value or not self.couldbe: + if value is None or not self.couldbe: return not_these: List[str] = [] for name, test in self.couldbe.items(): diff --git a/tests/test_utils.py b/tests/test_utils.py index 3de5e94a8..b6635279c 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,4 +1,5 @@ from sqlite_utils import utils +from sqlite_utils.utils import TypeTracker import csv import io import pytest @@ -73,6 +74,23 @@ def test_maximize_csv_field_size_limit(): assert rows_list2[0]["text"] == long_value +@pytest.mark.parametrize( + "rows,expected_types", + [ + ([{"a": ""}], {"a": "text"}), + ([{"a": ""}, {"a": ""}], {"a": "text"}), + ([{"a": "1"}, {"a": ""}], {"a": "text"}), + ([{"a": "0"}], {"a": "integer"}), + ([{"a": "1"}], {"a": "integer"}), + ([{"a": None}], {"a": "integer"}), + ], +) +def test_type_tracker_empty_strings(rows, expected_types): + tracker = TypeTracker() + list(tracker.wrap(iter(rows))) + assert tracker.types == expected_types + + @pytest.mark.parametrize( "input,expected", (