Skip to content

Commit 787851f

Browse files
committed
Use missingval instead of empty string for None values
1 parent 06e363d commit 787851f

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

tabulate/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,7 +1623,7 @@ def _normalize_tabular_data(tabular_data, headers, showindex="default"):
16231623
return rows, headers, headers_pad
16241624

16251625

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

22482248
numparses = _expand_numparse(disable_numparse, num_cols)
22492249
list_of_lists = _wrap_text_to_colwidths(
2250-
list_of_lists, maxcolwidths, numparses=numparses
2250+
list_of_lists, maxcolwidths, numparses=numparses, missingval=missingval
22512251
)
22522252

22532253
if maxheadercolwidths is not None:
@@ -2261,7 +2261,7 @@ def tabulate(
22612261

22622262
numparses = _expand_numparse(disable_numparse, num_cols)
22632263
headers = _wrap_text_to_colwidths(
2264-
[headers], maxheadercolwidths, numparses=numparses
2264+
[headers], maxheadercolwidths, numparses=numparses, missingval=missingval
22652265
)[0]
22662266

22672267
# empty values in the first column of RST tables should be escaped (issue #82)

test/test_textwrapper.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,7 @@ def test_wrap_datetime():
224224

225225
def test_wrap_none_value():
226226
"""TextWrapper: Show that None can be wrapped without crashing"""
227-
data = [
228-
["First Entry", None],
229-
["Second Entry", None]
230-
]
227+
data = [["First Entry", None], ["Second Entry", None]]
231228
headers = ["Title", "Value"]
232229
result = tabulate(data, headers=headers, tablefmt="grid", maxcolwidths=[7, 5])
233230

@@ -244,3 +241,26 @@ def test_wrap_none_value():
244241
]
245242
expected = "\n".join(expected)
246243
assert_equal(expected, result)
244+
245+
246+
def test_wrap_none_value_with_missingval():
247+
"""TextWrapper: Show that None can be wrapped without crashing and with a missing value"""
248+
data = [["First Entry", None], ["Second Entry", None]]
249+
headers = ["Title", "Value"]
250+
result = tabulate(
251+
data, headers=headers, tablefmt="grid", maxcolwidths=[7, 5], missingval="???"
252+
)
253+
254+
expected = [
255+
"+---------+---------+",
256+
"| Title | Value |",
257+
"+=========+=========+",
258+
"| First | ??? |",
259+
"| Entry | |",
260+
"+---------+---------+",
261+
"| Second | ??? |",
262+
"| Entry | |",
263+
"+---------+---------+",
264+
]
265+
expected = "\n".join(expected)
266+
assert_equal(expected, result)

0 commit comments

Comments
 (0)