Skip to content

Commit cab3a79

Browse files
committed
Merge remote-tracking branch 'gschizas/master'
2 parents 537d7b0 + 787851f commit cab3a79

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
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-
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,47 @@ def test_wrap_datetime():
220220
]
221221
expected = "\n".join(expected)
222222
assert_equal(expected, result)
223+
224+
225+
def test_wrap_none_value():
226+
"""TextWrapper: Show that None can be wrapped without crashing"""
227+
data = [["First Entry", None], ["Second Entry", None]]
228+
headers = ["Title", "Value"]
229+
result = tabulate(data, headers=headers, tablefmt="grid", maxcolwidths=[7, 5])
230+
231+
expected = [
232+
"+---------+---------+",
233+
"| Title | Value |",
234+
"+=========+=========+",
235+
"| First | |",
236+
"| Entry | |",
237+
"+---------+---------+",
238+
"| Second | |",
239+
"| Entry | |",
240+
"+---------+---------+",
241+
]
242+
expected = "\n".join(expected)
243+
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)