Skip to content

Commit 4042e61

Browse files
authored
Merge pull request #362 from Hnasar/fix-crash
Fix handling "True"/"False" bool str and None
2 parents 3cfb20c + d29909b commit 4042e61

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

tabulate/__init__.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,13 +1647,7 @@ def _wrap_text_to_colwidths(list_of_lists, colwidths, numparses=True):
16471647

16481648
if width is not None:
16491649
wrapper = _CustomTextWrap(width=width)
1650-
# Cast based on our internal type handling. Any future custom
1651-
# formatting of types (such as datetimes) may need to be more
1652-
# explicit than just `str` of the object. Also doesn't work for
1653-
# custom floatfmt/intfmt, nor with any missing/blank cells.
1654-
casted_cell = (
1655-
str(cell) if _isnumber(cell) else _type(cell, numparse)(cell)
1656-
)
1650+
casted_cell = str(cell)
16571651
wrapped = [
16581652
"\n".join(wrapper.wrap(line))
16591653
for line in casted_cell.splitlines()

test/test_textwrapper.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,27 @@ def test_wrap_datetime():
220220
]
221221
expected = "\n".join(expected)
222222
assert_equal(expected, result)
223+
224+
225+
def test_wrap_optional_bool_strs():
226+
"""TextWrapper: Show that str bools and None can be wrapped without crashing"""
227+
data = [
228+
["First Entry", "True"],
229+
["Second Entry", None],
230+
]
231+
headers = ["Title", "When"]
232+
result = tabulate(data, headers=headers, tablefmt="grid", maxcolwidths=[7, 5])
233+
234+
expected = [
235+
"+---------+--------+",
236+
"| Title | When |",
237+
"+=========+========+",
238+
"| First | True |",
239+
"| Entry | |",
240+
"+---------+--------+",
241+
"| Second | None |",
242+
"| Entry | |",
243+
"+---------+--------+",
244+
]
245+
expected = "\n".join(expected)
246+
assert_equal(expected, result)

0 commit comments

Comments
 (0)