Skip to content

Fix handling "True"/"False" bool str and None #362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions tabulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1641,13 +1641,7 @@ def _wrap_text_to_colwidths(list_of_lists, colwidths, numparses=True):

if width is not None:
wrapper = _CustomTextWrap(width=width)
# Cast based on our internal type handling. Any future custom
# formatting of types (such as datetimes) may need to be more
# explicit than just `str` of the object. Also doesn't work for
# custom floatfmt/intfmt, nor with any missing/blank cells.
casted_cell = (
str(cell) if _isnumber(cell) else _type(cell, numparse)(cell)
)
casted_cell = str(cell)
wrapped = [
"\n".join(wrapper.wrap(line))
for line in casted_cell.splitlines()
Expand Down
24 changes: 24 additions & 0 deletions test/test_textwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,27 @@ def test_wrap_datetime():
]
expected = "\n".join(expected)
assert_equal(expected, result)


def test_wrap_optional_bool_strs():
"""TextWrapper: Show that str bools and None can be wrapped without crashing"""
data = [
["First Entry", "True"],
["Second Entry", None],
]
headers = ["Title", "When"]
result = tabulate(data, headers=headers, tablefmt="grid", maxcolwidths=[7, 5])

expected = [
"+---------+--------+",
"| Title | When |",
"+=========+========+",
"| First | True |",
"| Entry | |",
"+---------+--------+",
"| Second | None |",
"| Entry | |",
"+---------+--------+",
]
expected = "\n".join(expected)
assert_equal(expected, result)