Skip to content
Draft
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
3 changes: 2 additions & 1 deletion tabulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1646,12 +1646,13 @@ 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.
# Preserve boolean strings: bool("False") would change their value.
casted_cell = (
missingval
if cell is None
else (
str(cell)
if cell == "" or _isnumber(cell)
if cell == "" or _isnumber(cell) or _isbool(cell)
else str(_type(cell, numparse)(cell))
)
)
Expand Down
25 changes: 25 additions & 0 deletions test/test_textwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import datetime
from textwrap import TextWrapper as OTW

import pytest

from tabulate import _CustomTextWrap as CTW, _strip_ansi, tabulate

from common import assert_equal, skip
Expand Down Expand Up @@ -326,6 +328,29 @@ def test_wrap_optional_bool_strs():
assert_equal(expected, result)


@pytest.mark.parametrize("value", ["True", "False", True, False])
@pytest.mark.parametrize("width", [3, 5, 10])
@pytest.mark.parametrize("options", [{}, {"disable_numparse": True}])
def test_wrap_bool_values_preserves_text(value, width, options):
"""Wrapping must not interpret nonempty boolean strings as truthy."""
expected = str(value)
if width == 3 and (isinstance(value, str) or options.get("disable_numparse")):
expected = expected[:3] + "\n" + expected[3:]
result = tabulate([[value]], tablefmt="plain", maxcolwidths=width, **options)
assert_equal(expected, result)


@pytest.mark.parametrize("value", ["True", "False"])
@pytest.mark.parametrize("width", [3, 5, 10])
@pytest.mark.parametrize("options", [{}, {"disable_numparse": True}])
def test_wrap_bool_headers_preserves_text(value, width, options):
expected = value
if width == 3:
expected = expected[:3] + "\n" + expected[3:]
result = tabulate([], headers=[value], tablefmt="plain", maxheadercolwidths=width, **options)
assert_equal(expected, result)


def test_wrap_wide_char_no_column_overflow():
"TextWrapper: wide chars must not overflow the requested column width."
try:
Expand Down