From 7c7a6be492eb11ace35d05794c572713953eee2d Mon Sep 17 00:00:00 2001 From: eeshsaxena Date: Mon, 17 Aug 2026 20:55:18 +0530 Subject: [PATCH] Raise UnsupportedFormat for a non-dict row in a list of records --- src/tablib/core.py | 2 ++ tests/test_tablib.py | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/src/tablib/core.py b/src/tablib/core.py index b0b56fd2..79609b7a 100644 --- a/src/tablib/core.py +++ b/src/tablib/core.py @@ -371,6 +371,8 @@ def _set_dict(self, pickle): self.wipe() self.headers = list(pickle[0].keys()) for row in pickle: + if not isinstance(row, dict): + raise UnsupportedFormat(error_details) self.append(Row(list(row.values()))) else: raise UnsupportedFormat(error_details) diff --git a/tests/test_tablib.py b/tests/test_tablib.py index ca8ac05e..c16c7835 100755 --- a/tests/test_tablib.py +++ b/tests/test_tablib.py @@ -381,6 +381,16 @@ def test_book_export_no_exceptions(self): unsupported = ['csv', 'tsv', 'jira', 'latex', 'df'] self._test_export_data_in_all_formats(book, exclude=unsupported) + def test_dict_setter_with_non_dict_row(self): + # A list whose first item is a dict but which contains a non-dict item + # (e.g. from YAML "- null" or JSON "[{...}, null]") used to raise a bare + # AttributeError instead of UnsupportedFormat. + data = tablib.Dataset() + with self.assertRaises(UnsupportedFormat): + data.dict = [{'a': 1}, None] + with self.assertRaises(UnsupportedFormat): + tablib.import_set('[{"a": 1}, null]', format='json') + def test_book_unsupported_loading(self): with self.assertRaises(UnsupportedFormat): tablib.Databook().load('Any stream', 'csv')