From f84bb7a9d9166d8a7abc502cebea57af20eef811 Mon Sep 17 00:00:00 2001 From: varunavy Date: Tue, 24 Nov 2026 11:52:07 -0800 Subject: [PATCH 1/2] Report rows with missing fields clearly instead of crashing (#30) --- csv_diff/__init__.py | 9 ++++++++- csv_diff/cli.py | 11 +++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/csv_diff/__init__.py b/csv_diff/__init__.py index 59a2eaf..32c16ad 100644 --- a/csv_diff/__init__.py +++ b/csv_diff/__init__.py @@ -16,7 +16,14 @@ def load_csv(fp, key=None, dialect=None): pass fp = csv.reader(fp, dialect=(dialect or "excel")) headings = next(fp) - rows = [dict(zip(headings, line)) for line in fp] + rows = [] + for line in fp: + if not line: + continue + # A row with fewer fields than the header would be silently mis-aligned by zip(); fail clearly instead (#30) + if len(line) < len(headings): + raise ValueError(f"Line {fp.line_num}: expected {len(headings)} fields but found {len(line)}") + rows.append(dict(zip(headings, line))) if key: keyfn = lambda r: r[key] else: diff --git a/csv_diff/cli.py b/csv_diff/cli.py index 261ef6d..e79bc9b 100644 --- a/csv_diff/cli.py +++ b/csv_diff/cli.py @@ -19,7 +19,7 @@ @click.option( "--format", type=click.Choice(["csv", "tsv", "json"]), - default=None, + default=None, help="Explicitly specify input format (csv, tsv, json) instead of auto-detecting", ) @click.option( @@ -66,9 +66,12 @@ def load(filename): if format == "json": return load_json(open(filename), key=key) else: - return load_csv( - open(filename, newline=""), key=key, dialect=dialect.get(format) - ) + try: + return load_csv( + open(filename, newline=""), key=key, dialect=dialect.get(format) + ) + except ValueError as e: + raise click.ClickException(f"{filename}: {e}") previous_data = load(previous) current_data = load(current) From b466aecf6555f6f643b534d5d19cc9f3586ecf81 Mon Sep 17 00:00:00 2001 From: varunavy Date: Tue, 24 Nov 2026 11:52:07 -0800 Subject: [PATCH 2/2] Add tests for missing, empty, quoted, blank and extra fields (#30) --- tests/test_cli.py | 11 +++++++++++ tests/test_csv_diff.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/tests/test_cli.py b/tests/test_cli.py index 12f4e12..47b3b7d 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -300,3 +300,14 @@ def test_diff_with_extras(tmpdir): """ ).strip() assert result.output.strip() == expected + + +def test_cli_row_with_missing_field_shows_clean_error(tmpdir): + one = tmpdir / "one.csv" + one.write("id,name,age\n1,Cleo,4\n2,Pancakes\n") + two = tmpdir / "two.csv" + two.write("id,name,age\n1,Cleo,4\n2,Pancakes,2\n") + result = CliRunner().invoke(cli.cli, [str(one), str(two), "--key", "id"]) + assert result.exit_code == 1 + assert "expected 3 fields but found 2" in result.output + assert "Traceback" not in result.output \ No newline at end of file diff --git a/tests/test_csv_diff.py b/tests/test_csv_diff.py index 0e3670f..2dbf216 100644 --- a/tests/test_csv_diff.py +++ b/tests/test_csv_diff.py @@ -115,3 +115,38 @@ def test_tsv(): "columns_added": [], "columns_removed": [], } == diff + + +import io +import pytest + + +def test_load_csv_row_with_missing_field_raises_clear_error(): + data = "id,name,age\n1,Cleo,4\n2,Pancakes\n" + with pytest.raises(ValueError) as excinfo: + load_csv(io.StringIO(data), key="id") + assert "Line 3: expected 3 fields but found 2" in str(excinfo.value) + + +def test_load_csv_empty_field_is_allowed(): + data = "id,name,age\n1,Cleo,\n2,Pancakes,2\n" + rows = load_csv(io.StringIO(data), key="id") + assert rows["1"] == {"id": "1", "name": "Cleo", "age": ""} + + +def test_load_csv_quoted_comma_counts_as_one_field(): + data = 'id,name,age\n1,"Cleo, the cat",4\n2,Pancakes,2\n' + rows = load_csv(io.StringIO(data), key="id") + assert rows["1"]["name"] == "Cleo, the cat" + + +def test_load_csv_blank_line_is_skipped(): + data = "id,name,age\n1,Cleo,4\n\n2,Pancakes,2\n" + rows = load_csv(io.StringIO(data), key="id") + assert set(rows) == {"1", "2"} + + +def test_load_csv_extra_fields_keep_existing_behaviour(): + data = "id,name,age\n1,Cleo,4,extra\n2,Pancakes,2\n" + rows = load_csv(io.StringIO(data), key="id") + assert rows["1"] == {"id": "1", "name": "Cleo", "age": "4"} \ No newline at end of file