Fix _validate() silently accepting empty rows/cols due to falsy check#654
Open
maxtaran2010 wants to merge 1 commit into
Open
Fix _validate() silently accepting empty rows/cols due to falsy check#654maxtaran2010 wants to merge 1 commit into
maxtaran2010 wants to merge 1 commit into
Conversation
`if row:` and `elif col:` are falsy for empty sequences (e.g. `()` or `[]`), causing `_validate` to fall through to the else branch which validates existing dataset dimensions instead of checking the provided row/col. Replace with `is not None` so an empty row passed to `insert()` or `__setitem__()` correctly raises `InvalidDimensions`. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Contributor
|
Thanks, but this needs a regression test. |
|
The elif col is not None:
if len(col) < 1:
is_valid = True # empty column still treated as valid
else:
...So an empty column inserted into a dataset that has rows still passes validation, and d = tablib.Dataset()
d.headers = ['a', 'b']
d.append([1, 2]); d.append([3, 4])
d.insert_col(0, col=None, header='z') # col=None -> []
# IndexError: list index out of range, and d.headers is now ['z', 'a', 'b'] (width still 2)Dropping the |
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.
Summary
Dataset._validate()usesif row:andelif col:to check whether a row or column was passed as an argument. This is a falsy check and fails silently whenroworcolis an empty sequence (()or[]).When
row=()is passed (e.g. viainsert()or__setitem__()), the conditionif row:evaluates toFalse, so execution falls through to theelsebranch, which validates the entire existing dataset dimensions instead of checking the incoming row's length. This means an empty row can be silently inserted into a dataset with existing columns without raisingInvalidDimensions.Reproduction
Fix
Replace
if row:withif row is not None:andelif col:withelif col is not None:so that empty sequences are correctly dispatched to the row/column dimension check instead of the fallback full-dataset check.Tests
All 176 existing tests continue to pass. The fix does not change behavior for any non-empty row/col argument.