Conversation
There was a problem hiding this comment.
🔵 Needs a closer look
Explicit empty key names bypass duplicate-key validation, and loader tests do not assert the promised concrete exception type.
Pull request overview
This PR rejects duplicate explicit row keys in CSV/TSV and JSON inputs, adds actionable CLI errors, raises DuplicateKeyError, and documents the behavior.
Changes:
- Adds shared duplicate-key validation for loaders.
- Updates CLI error reporting.
- Adds loader and CLI coverage.
- Documents row numbering and no-key deduplication.
File summaries
| File | Reviewed changes and final comments |
|---|---|
tests/test_csv_diff.py |
Tests loader validation and deduplication. Nit (1 vote): assert DuplicateKeyError specifically for CSV and JSON cases. |
tests/test_cli.py |
Tests CSV, TSV, and JSON CLI error reporting. |
README.md |
Documents duplicate-key behavior and row numbering. |
csv_diff/cli.py |
Formats duplicate-key errors with filenames and row details. |
csv_diff/__init__.py |
Implements duplicate-key validation. Moderate (1 vote): explicit empty keys bypass validation in both CSV and JSON branches; use an explicit None check. |
Review details
Suppressed comments (4)
csv_diff/init.py:40
- This branch is still guarded by
if key:, so an explicitly supplied empty key name (for example, a CSV header,nameloaded withkey="") skips_validate_unique_keysand falls back to content hashing. That violates the documented behavior for any explicit--keyand allows repeated values in the selected empty-name column; test forkey is not Noneinstead.
_validate_unique_keys(rows, key)
csv_diff/init.py:56
- The JSON loader has the same falsy-key gap:
key=""is treated as no key, so duplicate values in a valid empty-named JSON property are not rejected. Since the change promises validation whenever an explicit key is provided, use an explicitNonecheck for this branch.
_validate_unique_keys(raw_list, key)
tests/test_csv_diff.py:132
- The public contract says duplicate explicit keys raise
DuplicateKeyError, but this assertion accepts anyValueError; a regression to a genericValueErrorwould still pass. Assert the concrete exception type here so the CSV loader's new API is covered.
with pytest.raises(ValueError, match="Duplicate key") as error:
tests/test_csv_diff.py:146
- This JSON test has the same coverage gap: it only verifies the
ValueErrorbase class, so it would not catch a regression that stops exposing the promisedDuplicateKeyErrortype. Assert the concrete exception class here as well.
with pytest.raises(ValueError, match="Duplicate key") as error:
- Files reviewed: 5/5 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With an explicit
--key, repeated values currently overwrite earlier rows silently. In the sample from #31, the command exits successfully and reports a removed column even though the input contains duplicate row keys.This change rejects duplicate explicit keys in CSV/TSV and JSON inputs. The CLI reports the input filename, key column and value, and the first and repeated data row numbers, then exits with status 1 without printing a diff. Python callers receive
DuplicateKeyError, a subclass ofValueError.Both loaders share the check because both previously indexed rows by key using a dictionary comprehension. Identical rows with an explicit key are rejected too, since that key must uniquely identify a row. Empty field names are supported:
--key=orkey=""selects the empty-named field. Omitting the key or passingkey=Noneretains content-based deduplication. Callers previously using an empty string to mean no key should omit it or useNone.Row numbers count data records from 1, excluding the CSV/TSV header; they are not physical line numbers for multiline CSV fields. The README documents these behaviors.
This targets duplicate row-key values in #31. The separate change in #45 targets duplicate column headings; this patch does not add heading validation.
Validation on Windows / Python 3.9.13:
python -m pytest -q).0andnull, either input in CSV/TSV/JSON CLI comparisons, and unchanged omitted/None-key deduplication.DuplicateKeyErrorand verify itsValueErrorcompatibility.git diff --checkand incremental patch application checks passed.Hosted checks for the updated head are tracked separately in this PR; the local results above do not claim a completed multi-version CI matrix.
Fixes #31.