Skip to content

Commit 20c6370

Browse files
committed
fix astanin#180 - exception on empty data with maxcolwidths option
1 parent 90fbd7e commit 20c6370

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

tabulate/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,7 +1503,11 @@ def _normalize_tabular_data(tabular_data, headers, showindex="default"):
15031503

15041504

15051505
def _wrap_text_to_colwidths(list_of_lists, colwidths, numparses=True):
1506-
numparses = _expand_iterable(numparses, len(list_of_lists[0]), True)
1506+
if len(list_of_lists):
1507+
num_cols = len(list_of_lists[0])
1508+
else:
1509+
num_cols = 0
1510+
numparses = _expand_iterable(numparses, num_cols, True)
15071511

15081512
result = []
15091513

@@ -2062,7 +2066,10 @@ def tabulate(
20622066
list_of_lists, separating_lines = _remove_separating_lines(list_of_lists)
20632067

20642068
if maxcolwidths is not None:
2065-
num_cols = len(list_of_lists[0])
2069+
if len(list_of_lists):
2070+
num_cols = len(list_of_lists[0])
2071+
else:
2072+
num_cols = 0
20662073
if isinstance(maxcolwidths, int): # Expand scalar for all columns
20672074
maxcolwidths = _expand_iterable(maxcolwidths, num_cols, maxcolwidths)
20682075
else: # Ignore col width for any 'trailing' columns

test/test_regression.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,3 +486,9 @@ def test_preserve_line_breaks_with_maxcolwidths():
486486
)
487487
result = tabulate(table, tablefmt="grid", maxcolwidths=10)
488488
assert_equal(result, expected)
489+
490+
491+
def test_exception_on_empty_data_with_maxcolwidths():
492+
"Regression: exception on empty data when using maxcolwidths (github issue #180)"
493+
result = tabulate([], maxcolwidths=5)
494+
assert_equal(result, "")

0 commit comments

Comments
 (0)