diff --git a/tabulate/__init__.py b/tabulate/__init__.py index 12a2950..b5f3763 100644 --- a/tabulate/__init__.py +++ b/tabulate/__init__.py @@ -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() diff --git a/test/test_regression.py b/test/test_regression.py index 9555676..8d2d077 100644 --- a/test/test_regression.py +++ b/test/test_regression.py @@ -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)