From ec9c861ae0331473106a25ed494f7b8e6afab215 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Wed, 20 Dec 2023 21:38:46 +0100 Subject: [PATCH] Avoid inserting dynamic values when row is already full --- src/tablib/core.py | 11 ++++++----- tests/test_tablib.py | 2 ++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/tablib/core.py b/src/tablib/core.py index 06624b42..ef3f6fe2 100644 --- a/src/tablib/core.py +++ b/src/tablib/core.py @@ -457,12 +457,13 @@ def insert(self, index, row, tags=()): The default behaviour is to insert the given row to the :class:`Dataset` object at the given index. - """ + """ self._validate(row) - for pos, func in self._dynamic_columns.items(): - row = list(row) - row.insert(pos, func(row)) + if len(row) < self.width: + for pos, func in self._dynamic_columns.items(): + row = list(row) + row.insert(pos, func(row)) self._data.insert(index, Row(row, tags=tags)) def rpush(self, row, tags=()): @@ -560,7 +561,7 @@ def insert_col(self, index, col=None, header=None): col = [] # Callable Columns... - if hasattr(col, '__call__'): + if callable(col): self._dynamic_columns[self.width] = col col = list(map(col, self._data)) diff --git a/tests/test_tablib.py b/tests/test_tablib.py index a77823a8..a335c44b 100755 --- a/tests/test_tablib.py +++ b/tests/test_tablib.py @@ -196,6 +196,8 @@ def initials(row): # Also acceptable when all dynamic column values are provided. self.founders.append(('Other', 'Second', 84, 'Other', 'OS')) + self.assertEqual(self.founders[3], ('Some', 'One', 71, 'Some', 'SO')) + self.assertEqual(self.founders[4], ('Other', 'Second', 84, 'Other', 'OS')) self.assertEqual( self.founders['first_again'], ['John', 'George', 'Thomas', 'Some', 'Other']