From d87a821a8e107b12ab317e25e81fbd2e1e81cdf2 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sat, 13 Sep 2025 17:03:12 +0200 Subject: [PATCH] fix #365 - exception on empty data with maxheadercolwidths option Consistency between `maxcolwidths` and `maxheadercolwidths`. --- tabulate/__init__.py | 5 ++++- test/test_regression.py | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/tabulate/__init__.py b/tabulate/__init__.py index e100c097..13fe5fa6 100644 --- a/tabulate/__init__.py +++ b/tabulate/__init__.py @@ -2262,7 +2262,10 @@ def tabulate( ) if maxheadercolwidths is not None: - num_cols = len(list_of_lists[0]) + if len(list_of_lists): + num_cols = len(list_of_lists[0]) + else: + num_cols = 0 if isinstance(maxheadercolwidths, int): # Expand scalar for all columns maxheadercolwidths = _expand_iterable( maxheadercolwidths, num_cols, maxheadercolwidths diff --git a/test/test_regression.py b/test/test_regression.py index bf262470..39ee076b 100644 --- a/test/test_regression.py +++ b/test/test_regression.py @@ -515,6 +515,12 @@ def test_exception_on_empty_data_with_maxcolwidths(): assert_equal(result, "") +def test_exception_on_empty_data_with_maxheadercolwidths(): + "Regression: exception on empty data when using maxheadercolwidths (github issue #365)" + result = tabulate([], maxheadercolwidths=5) + assert_equal(result, "") + + def test_numpy_int64_as_integer(): "Regression: format numpy.int64 as integer (github issue #18)" try: