Skip to content

Commit d87a821

Browse files
fix astanin#365 - exception on empty data with maxheadercolwidths option
Consistency between `maxcolwidths` and `maxheadercolwidths`.
1 parent 74885be commit d87a821

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

tabulate/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2262,7 +2262,10 @@ def tabulate(
22622262
)
22632263

22642264
if maxheadercolwidths is not None:
2265-
num_cols = len(list_of_lists[0])
2265+
if len(list_of_lists):
2266+
num_cols = len(list_of_lists[0])
2267+
else:
2268+
num_cols = 0
22662269
if isinstance(maxheadercolwidths, int): # Expand scalar for all columns
22672270
maxheadercolwidths = _expand_iterable(
22682271
maxheadercolwidths, num_cols, maxheadercolwidths

test/test_regression.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,12 @@ def test_exception_on_empty_data_with_maxcolwidths():
515515
assert_equal(result, "")
516516

517517

518+
def test_exception_on_empty_data_with_maxheadercolwidths():
519+
"Regression: exception on empty data when using maxheadercolwidths (github issue #365)"
520+
result = tabulate([], maxheadercolwidths=5)
521+
assert_equal(result, "")
522+
523+
518524
def test_numpy_int64_as_integer():
519525
"Regression: format numpy.int64 as integer (github issue #18)"
520526
try:

0 commit comments

Comments
 (0)