Skip to content
Open
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
6 changes: 5 additions & 1 deletion src/formencode/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,10 @@ class UnicodeString(ByteString):
True
>>> UnicodeString(encoding='utf-7').to_python('Ni Ni Ni') == 'Ni Ni Ni'
True
>>> UnicodeString().to_python(0) == '0'
True
>>> UnicodeString().to_python(False) == 'False'
True

"""
encoding = 'utf-8'
Expand All @@ -1122,7 +1126,7 @@ def __init__(self, **kw):
self.outputEncoding = self.encoding

def _convert_to_python(self, value, state):
if not value:
if not value and not isinstance(value, (int, bool)):
return ''
if isinstance(value, str):
return value
Expand Down
24 changes: 18 additions & 6 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,24 @@ def test_unicode_encoding(self):
self.assertEqual(uv.from_python(us), us)
self.assertIs(type(uv.from_python(us)), str)

def test_unicode_empty(self):
iv = self.validator()
for value in [None, b"", ""]:
result = iv.to_python(value)
self.assertEqual(result, "")
self.assertIsInstance(result, str)
def test_unicode_empty(self):
iv = self.validator()
for value in [None, b"", ""]:
result = iv.to_python(value)
self.assertEqual(result, "")
self.assertIsInstance(result, str)

def test_unicode_int(self):
iv = self.validator()
result = iv.to_python(0)
self.assertEqual(result, "0")
self.assertIsInstance(result, str)

def test_unicode_bool(self):
iv = self.validator()
result = iv.to_python(False)
self.assertEqual(result, "False")
self.assertIsInstance(result, str)


class TestIntValidator(unittest.TestCase):
Expand Down