From 9b2b44115c2c02906e9d8c79eeb69c5648c5cb1f Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Thu, 21 Mar 2024 21:07:58 +0100 Subject: [PATCH] Fixes #583 - Allow inserting columns for dataset with headers and no values --- src/tablib/core.py | 11 +++-------- tests/test_tablib.py | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/tablib/core.py b/src/tablib/core.py index 70e26f04..ad5ec8d6 100644 --- a/src/tablib/core.py +++ b/src/tablib/core.py @@ -367,19 +367,14 @@ def _set_dict(self, pickle): dict = property(_get_dict, _set_dict) def _clean_col(self, col): - """Prepares the given column for insert/append.""" + """Prepares the given column for insert/append. `col` is not supposed to + contain any header value. + """ col = list(col) - if self.headers: - header = [col.pop(0)] - else: - header = [] - if len(col) == 1 and hasattr(col[0], '__call__'): - col = list(map(col[0], self._data)) - col = tuple(header + col) return col diff --git a/tests/test_tablib.py b/tests/test_tablib.py index ee562b9b..fbb26f96 100755 --- a/tests/test_tablib.py +++ b/tests/test_tablib.py @@ -117,10 +117,19 @@ def test_add_column(self): # With Headers data.headers = ('fname', 'lname') - new_col = [21, 22] - data.append_col(new_col, header='age') + age_col = [21, 22] + data.append_col(age_col, header='age') + size_col = [1.65, 1.86] + data.insert_col(1, size_col, header='size') - self.assertEqual(data['age'], new_col) + self.assertEqual(data['age'], age_col) + self.assertEqual(data['size'], size_col) + + def test_add_column_no_data_with_headers(self): + """Verify adding empty column when dataset has only headers.""" + data.headers = ('fname', 'lname') + data.insert_col(1, [], header='size') + self.assertEqual(data.headers, ['fname', 'size', 'lname']) def test_add_column_no_data_no_headers(self): """Verify adding new column with no headers."""