Repository navigation
fix: add_row() no longer mutates caller's input dictionary - #3690
Conversation
|
Performance benchmarks:
|
Krishsharma179
left a comment
There was a problem hiding this comment.
This is the self review of my PR and would appreciate the peer review from anyone of you guys @souro26 @codebreaker32
| # If the row user passed includes the unexpected fields | ||
| unexpected = [k for k in row if k not in self.fields] | ||
| if unexpected: | ||
| raise ValueError(f"Row contains unexpected fields: {unexpected}") |
There was a problem hiding this comment.
the the user's input includes the unexpected fields it will raise the Valueerror
| # Adding the row to rows | ||
| row_to_add = {k: row[k] for k in self.fields} | ||
| self.rows.append(row_to_add) |
There was a problem hiding this comment.
This was the core problem before it was k : row.pop(k) now k:row[k] it was altering the users input by popping it so I changed it
| dataset = TableDataSet("new", fields=["a", "b", "c"]) | ||
| with pytest.raises(ValueError, match="row is empty"): | ||
| dataset.add_row({}) |
There was a problem hiding this comment.
Added a test that expects a [ValueError] when the row is empty. The existing tests already cover unexpected fields and missing required fields
| def test_add_row_does_not_mutate_input(): | ||
| """add_row should not mutate the user's input.""" | ||
| dataset = TableDataSet("test", fields=["a", "b", "c"]) | ||
| row = {"a": 1, "b": 2, "c": 3} | ||
| original = row.copy() | ||
| dataset.add_row(row) | ||
| assert row == original |
There was a problem hiding this comment.
This checks if the row passed by the user gets affected or not after calling add_row
| def test_add_row_reuse_same_dict(): | ||
| """Tests for resuablity of add_row.""" | ||
| dataset = TableDataSet("t", fields=["a", "b"]) | ||
|
|
||
| row = {"a": 1, "b": 2} | ||
|
|
||
| for i in range(1, 11): | ||
| row["a"] = row["a"] * i | ||
| row["b"] = row["b"] * i | ||
| dataset.add_row(row) # should not raise any error | ||
|
|
||
| assert len(dataset.rows) == 10 | ||
|
|
There was a problem hiding this comment.
This test checks the reusability of add_row() it verifies that reusing the same dict across 10 calls adds 10 rows and raises no error
|
I agree that this should be addressed. I am less convinced by how you address it. Why not simply copy the original row? |
@quaquel I thought creating an unnecessary copy of the user's dict is wasteful instead, simply not modifying the user's dict in the first place is cleaner. Using row[k] just fetches the value without any extra memory allocation. That said, if you prefer row.copy() I am happy to change it! |
|
Not sure, but the current implementation involves iterating 3 times over fields. |
yes, it’s technically less efficient, but the real impact is minimal unless you’re adding huge numbers of rows with many fields |
4e98b4c to
7614a0e
Compare
|
@quaquel please check the changes I did now it doesnt iterate over fields 3 times |
(cherry picked from commit 598efe5)
Pre-PR Checklist
Summary
add_row()was mutating the caller's input dictionary (row), causing aKeyErrorwhen the same rowdictwas reused across multiple calls.Bug / Issue
Closes #3689
add_row()internally usedrow.pop(k)to buildrow_to_add, which destructively removes keys from the user's original dictionary. This means after the first call, the passed-in row becomes {}, and any subsequent use of thatdictraises aKeyErrorImplementation
Testing
test_table_dataset— added empty row check, add_row({}) raises ValueErrortest_add_row_does_not_mutate_input— verifies the original dict is unchanged after calling add_row()test_add_row_reuse_same_dict— verifies that reusing the same dict across 10 iterations raises no error.Additional Notes
It was important to fix this because
add_row()should never alter the data the user passes in modifying a caller's input is unexpected behaviour and can cause silent bugs that are hard to track down