fix: auto-detect single-column CSV files

Format detection passed the first bytes to csv.Sniffer().sniff(), which
raises csv.Error('Could not determine delimiter') for files with no
delimiter at all, so a bare single-column list like 'id\n1\n2' could
never be loaded through rows_from_file. Fall back to the default CSV
dialect when sniffing fails so DictReader treats the file as a single
column.
This commit is contained in:
Harsh Thakkar 2026-09-24 05:54:53 +05:30
commit 04c67240ac
2 changed files with 29 additions and 4 deletions

View file

@ -55,6 +55,21 @@ def test_hash_record():
)
@pytest.mark.parametrize(
"content,expected",
(
(b"apple\nbanana\ncherry\n", [{"apple": "banana"}, {"apple": "cherry"}]),
(b"id\n1\n2\n", [{"id": "1"}, {"id": "2"}]),
),
)
def test_rows_from_file_detects_single_column_csv(content, expected):
# A single-column file has no delimiter for csv.Sniffer to detect; it
# used to raise csv.Error("Could not determine delimiter").
rows, format_ = utils.rows_from_file(io.BytesIO(content))
assert format_ == utils.Format.CSV
assert list(rows) == expected
def test_maximize_csv_field_size_limit():
# Reset to default in case other tests have changed it
csv.field_size_limit(utils.ORIGINAL_CSV_FIELD_SIZE_LIMIT)