Skip to content

Commit efcf79c

Browse files
committed
fix astanin#31: TypeError on Py2 on mixed column value types
The original patch was submitted by an anonymous user (Greg). The test was moved to test/test_regression.py and extended with a Unicode value.
1 parent eb1f547 commit efcf79c

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

tabulate.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,10 @@ def _format(val, valtype, floatfmt, missingval=""):
448448
if valtype in [int, _text_type]:
449449
return "{0}".format(val)
450450
elif valtype is _binary_type:
451-
return _text_type(val, "ascii")
451+
try:
452+
return _text_type(val, "ascii")
453+
except TypeError:
454+
return _text_type(val)
452455
elif valtype is float:
453456
return format(float(val), floatfmt)
454457
else:

test/test_regression.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,19 @@ def test_numeric_column_headers():
125125
" 3 3 3 3 3",
126126
" 4 4 4 4 4",])
127127
assert_equal(result, expected)
128+
129+
130+
def test_column_with_mixed_value_types():
131+
"Regression: mixed value types in the same column (issue #31)"
132+
expected = '\n'.join([
133+
'-----',
134+
'',
135+
'a',
136+
'я',
137+
'0',
138+
'False',
139+
'-----',
140+
])
141+
data = [[None], ['a'], ['\u044f'], [0], [False]]
142+
table = tabulate(data)
143+
assert_equal(table, expected)

0 commit comments

Comments
 (0)