Skip to content

Commit

Permalink
Merge pull request #300 from pzarfos/fix-#214-maxcolwidths-doesn't-ac…
Browse files Browse the repository at this point in the history
…cept-tuple

Fix TypeError when using a tuple for maxcolwidths #214
  • Loading branch information
astanin authored Sep 26, 2024
2 parents 3a8c941 + b206ba9 commit f99343a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1143,5 +1143,4 @@ Bart Broere, Vilhelm Prytz, Alexander Gažo, Hugo van Kemenade,
jamescooke, Matt Warner, Jérôme Provensal, Kevin Deldycke,
Kian-Meng Ang, Kevin Patterson, Shodhan Save, cleoold, KOLANICH,
Vijaya Krishna Kasula, Furcy Pin, Christian Fibich, Shaun Duncan,
Dimitri Papadopoulos, Élie Goudout.

Dimitri Papadopoulos, Élie Goudout, Racerroar888, Phill Zarfos.
2 changes: 2 additions & 0 deletions tabulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2183,6 +2183,8 @@ def tabulate(
list_of_lists, separating_lines = _remove_separating_lines(list_of_lists)

if maxcolwidths is not None:
if type(maxcolwidths) is tuple: # Check if tuple, convert to list if so
maxcolwidths = list(maxcolwidths)
if len(list_of_lists):
num_cols = len(list_of_lists[0])
else:
Expand Down
21 changes: 21 additions & 0 deletions test/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,27 @@ def test_preserve_line_breaks_with_maxcolwidths():
assert_equal(expected, result)


def test_maxcolwidths_accepts_list_or_tuple():
"Regression: maxcolwidths can accept a list or a tuple (github issue #214)"
table = [["lorem ipsum dolor sit amet"]*3]
expected = "\n".join(
[
"+-------------+----------+----------------------------+",
"| lorem ipsum | lorem | lorem ipsum dolor sit amet |",
"| dolor sit | ipsum | |",
"| amet | dolor | |",
"| | sit amet | |",
"+-------------+----------+----------------------------+",
]
)
# test with maxcolwidths as a list
result = tabulate(table, tablefmt="grid", maxcolwidths=[12, 8])
assert_equal(expected, result)
# test with maxcolwidths as a tuple
result = tabulate(table, tablefmt="grid", maxcolwidths=(12, 8))
assert_equal(expected, result)


def test_exception_on_empty_data_with_maxcolwidths():
"Regression: exception on empty data when using maxcolwidths (github issue #180)"
result = tabulate([], maxcolwidths=5)
Expand Down

0 comments on commit f99343a

Please sign in to comment.