Skip to content
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
24 changes: 15 additions & 9 deletions tabulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1646,15 +1646,21 @@ def _wrap_text_to_colwidths(
# 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 = (
missingval
if cell is None
else (
str(cell)
if cell == "" or _isnumber(cell)
else str(_type(cell, numparse)(cell))
)
)
#
# Pass numparse by keyword: `_type(cell, numparse)` bound it to
# has_invisible and ignored disable_numparse (github issue #448).
# Thousands-separated strings type as int/float but cannot be
# constructed with int()/float(); fall back to str(cell).
if cell is None:
casted_cell = missingval
elif cell == "" or _isnumber(cell):
casted_cell = str(cell)
else:
cell_type = _type(cell, numparse=numparse)
try:
casted_cell = str(cell_type(cell))
except (TypeError, ValueError):
casted_cell = str(cell)
wrapped = [
"\n".join(wrapper.wrap(line))
for line in casted_cell.splitlines()
Expand Down
16 changes: 16 additions & 0 deletions test/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,3 +598,19 @@ def test_github_escape_pipe_character():
result = tabulate([["foo|bar"]], headers=("spam|eggs",), tablefmt="github")
expected = "| spam\\|eggs |\n|:------------|\n| foo\\|bar |"
assert_equal(expected, result)


def test_maxcolwidths_thousands_separator_with_disable_numparse():
"Regression: honor disable_numparse when wrapping '100,120' (github issue #448)"
table = [["100,120"]]
expected = "100,120"
result = tabulate(table, tablefmt="plain", disable_numparse=True, maxcolwidths=20)
assert_equal(expected, result)


def test_maxcolwidths_thousands_separator_default_numparse():
"Regression: wrapping a thousands-separated number must not raise ValueError"
table = [["100,120"]]
expected = "100,120"
result = tabulate(table, tablefmt="plain", maxcolwidths=20)
assert_equal(expected, result)