Add input validation for out-of-bounds column numbers in cload.py. #461
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.
Fixes: #209
Original Issues: #135
Related Issues:
cooler cloadOverview
This pull request adds input validation for column indices in the
cooler cloadcommand to catch out-of-bounds values early and provide clear error messages. Previously, if a user specified a column index larger than the number of columns in the file, the program failed with an unhelpfulValueError: No objects to concatenatetraceback. This update prevents that by checking the indices against the file’s actual column count, raising a detailed error if invalid indices are detected.What Was Happening Before?
The Problem:
When users passed column indices beyond the number of available columns (e.g.,
-c2 4 -p2 5for a file with only 4 columns),pandasfailed silently during concatenation, leading to a vague traceback.Example Error:
This error offered no guidance on the actual issue, making debugging frustrating.
What’s Changed?
This update introduces proactive validation of column indices and streamlines file handling for better error reporting and compatibility with standard input.
Input Validation (
validate_pairs_columns)cload.pyto validate column indices against the actual number of columns in the input file.ValueErrorwith a clear message if any index is out of bounds.Header Handling (
get_header)peek()dependency (which broke withStringIOduring tests).readline()to handle headers, buffers remaining content, and returns a new stream if needed.Updated
pairsCommandvalidate_pairs_columns, centralizing validation logic.get_headercalls and fixed potential double-read issues with stdin.kwargs = {}beforepd.read_csvto preventClickoption leakage causingTypeError.How It Works Now
Valid Case:
✅ Passes and proceeds with loading.
Invalid Column Index:
❌ Fails early with:
Empty or Header-Only File:
❌ Fails with:
Benefits
StringIOimprove compatibility for piped input and testing.Testing
New Tests (7 cases in
tests/test_cload.py)Coverage:
cload.py, with untested lines only in unrelated subcommands.pytestand passed all tests:Notes
htmlcov/andcoverage.xml.