Skip to content

Commit f99343a

Browse files
authored
Merge pull request #300 from pzarfos/fix-#214-maxcolwidths-doesn't-accept-tuple
Fix TypeError when using a tuple for maxcolwidths #214
2 parents 3a8c941 + b206ba9 commit f99343a

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,5 +1143,4 @@ Bart Broere, Vilhelm Prytz, Alexander Gažo, Hugo van Kemenade,
11431143
jamescooke, Matt Warner, Jérôme Provensal, Kevin Deldycke,
11441144
Kian-Meng Ang, Kevin Patterson, Shodhan Save, cleoold, KOLANICH,
11451145
Vijaya Krishna Kasula, Furcy Pin, Christian Fibich, Shaun Duncan,
1146-
Dimitri Papadopoulos, Élie Goudout.
1147-
1146+
Dimitri Papadopoulos, Élie Goudout, Racerroar888, Phill Zarfos.

tabulate/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2183,6 +2183,8 @@ def tabulate(
21832183
list_of_lists, separating_lines = _remove_separating_lines(list_of_lists)
21842184

21852185
if maxcolwidths is not None:
2186+
if type(maxcolwidths) is tuple: # Check if tuple, convert to list if so
2187+
maxcolwidths = list(maxcolwidths)
21862188
if len(list_of_lists):
21872189
num_cols = len(list_of_lists[0])
21882190
else:

test/test_regression.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,27 @@ def test_preserve_line_breaks_with_maxcolwidths():
488488
assert_equal(expected, result)
489489

490490

491+
def test_maxcolwidths_accepts_list_or_tuple():
492+
"Regression: maxcolwidths can accept a list or a tuple (github issue #214)"
493+
table = [["lorem ipsum dolor sit amet"]*3]
494+
expected = "\n".join(
495+
[
496+
"+-------------+----------+----------------------------+",
497+
"| lorem ipsum | lorem | lorem ipsum dolor sit amet |",
498+
"| dolor sit | ipsum | |",
499+
"| amet | dolor | |",
500+
"| | sit amet | |",
501+
"+-------------+----------+----------------------------+",
502+
]
503+
)
504+
# test with maxcolwidths as a list
505+
result = tabulate(table, tablefmt="grid", maxcolwidths=[12, 8])
506+
assert_equal(expected, result)
507+
# test with maxcolwidths as a tuple
508+
result = tabulate(table, tablefmt="grid", maxcolwidths=(12, 8))
509+
assert_equal(expected, result)
510+
511+
491512
def test_exception_on_empty_data_with_maxcolwidths():
492513
"Regression: exception on empty data when using maxcolwidths (github issue #180)"
493514
result = tabulate([], maxcolwidths=5)

0 commit comments

Comments
 (0)