Skip to content

Commit

Permalink
Use missingval instead of empty string for None values
Browse files Browse the repository at this point in the history
  • Loading branch information
gschizas committed Oct 22, 2024
1 parent 06e363d commit 787851f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
8 changes: 4 additions & 4 deletions tabulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1623,7 +1623,7 @@ def _normalize_tabular_data(tabular_data, headers, showindex="default"):
return rows, headers, headers_pad


def _wrap_text_to_colwidths(list_of_lists, colwidths, numparses=True):
def _wrap_text_to_colwidths(list_of_lists, colwidths, numparses=True, missingval=_DEFAULT_MISSINGVAL):
if len(list_of_lists):
num_cols = len(list_of_lists[0])
else:
Expand All @@ -1646,7 +1646,7 @@ def _wrap_text_to_colwidths(list_of_lists, colwidths, numparses=True):
# explicit than just `str` of the object. Also doesn't work for
# custom floatfmt/intfmt, nor with any missing/blank cells.
casted_cell = (
'' if cell is None else str(cell) if _isnumber(cell) else _type(cell, numparse)(cell)
missingval if cell is None else str(cell) if _isnumber(cell) else _type(cell, numparse)(cell)
)
wrapped = [
"\n".join(wrapper.wrap(line))
Expand Down Expand Up @@ -2247,7 +2247,7 @@ def tabulate(

numparses = _expand_numparse(disable_numparse, num_cols)
list_of_lists = _wrap_text_to_colwidths(
list_of_lists, maxcolwidths, numparses=numparses
list_of_lists, maxcolwidths, numparses=numparses, missingval=missingval
)

if maxheadercolwidths is not None:
Expand All @@ -2261,7 +2261,7 @@ def tabulate(

numparses = _expand_numparse(disable_numparse, num_cols)
headers = _wrap_text_to_colwidths(
[headers], maxheadercolwidths, numparses=numparses
[headers], maxheadercolwidths, numparses=numparses, missingval=missingval
)[0]

# empty values in the first column of RST tables should be escaped (issue #82)
Expand Down
28 changes: 24 additions & 4 deletions test/test_textwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,7 @@ def test_wrap_datetime():

def test_wrap_none_value():
"""TextWrapper: Show that None can be wrapped without crashing"""
data = [
["First Entry", None],
["Second Entry", None]
]
data = [["First Entry", None], ["Second Entry", None]]
headers = ["Title", "Value"]
result = tabulate(data, headers=headers, tablefmt="grid", maxcolwidths=[7, 5])

Expand All @@ -244,3 +241,26 @@ def test_wrap_none_value():
]
expected = "\n".join(expected)
assert_equal(expected, result)


def test_wrap_none_value_with_missingval():
"""TextWrapper: Show that None can be wrapped without crashing and with a missing value"""
data = [["First Entry", None], ["Second Entry", None]]
headers = ["Title", "Value"]
result = tabulate(
data, headers=headers, tablefmt="grid", maxcolwidths=[7, 5], missingval="???"
)

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

0 comments on commit 787851f

Please sign in to comment.