From 487f482ed726215127e338cace65352aeeb74f85 Mon Sep 17 00:00:00 2001 From: Bradley Kirton Date: Tue, 10 Mar 2026 12:38:58 +0200 Subject: [PATCH] fix: Correctly pass numparse to _type call in _wrap_text_to_colwidths --- README.md | 2 +- tabulate/__init__.py | 2 +- test/test_internal.py | 27 +++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d412328..78869e2 100644 --- a/README.md +++ b/README.md @@ -1274,4 +1274,4 @@ Keyacom, Andrew Coffey, Arpit Jain, Israel Roldan, ilya112358, Dan Nicholson, Frederik Scheerer, cdar07 (cdar), Racerroar888, Perry Kundert, Hnasar, Jun Koo, Jo2234, Bjorn Olsen, George Schizas, Kadir Can Ozden, Jeff Quast, Mayukha Vadari, Rebecca Jean Herman, -Ján Jančár (J08nY). +Ján Jančár (J08nY), Bradley Kirton. diff --git a/tabulate/__init__.py b/tabulate/__init__.py index 449f78c..69c63cc 100644 --- a/tabulate/__init__.py +++ b/tabulate/__init__.py @@ -1653,7 +1653,7 @@ def _wrap_text_to_colwidths( else ( str(cell) if cell == "" or _isnumber(cell) - else str(_type(cell, numparse)(cell)) + else str(_type(cell, numparse=numparse)(cell)) ) ) wrapped = [ diff --git a/test/test_internal.py b/test/test_internal.py index 49ae0ba..290a22c 100644 --- a/test/test_internal.py +++ b/test/test_internal.py @@ -1,5 +1,7 @@ """Tests of the internal tabulate functions.""" +import pytest + import tabulate as T from common import assert_equal, cols_to_pipe_str, rows_to_pipe_table_str, skip @@ -210,6 +212,31 @@ def test_wrap_text_to_numbers(): assert_equal(expected, result) +def test_wrap_text_to_colwidths_numparses_disabled(): + "Internal: Test _wrap_text_to_colwidths to show it will correctly pass along the numparses input" + rows = [ + ["a", "b"], + [ + "1,000,000", + "1,000,000.00", + ], + ] + widths = [20, 20] + numparses = [True, True] + + # This will raise because _type identifies 1,000,000 as int and 1,000,000.00 as float. + # See _isnumber_with_thousands_separator + with pytest.raises(ValueError): + T._wrap_text_to_colwidths(rows, widths, numparses=numparses) + + # Switch off number parsing + numparses = [False, False] + expected = [["a", "b"], ["1,000,000", "1,000,000.00"]] + + result = T._wrap_text_to_colwidths(rows, widths, numparses=numparses) + assert_equal(expected, result) + + def test_wrap_text_to_colwidths_single_ansi_colors_full_cell(): """Internal: autowrapped text can retain a single ANSI colors when it is at the beginning and end of full cell"""