allow type promotion to float

This commit is contained in:
Doug Hellmann 2012-11-29 15:12:40 -05:00
parent 33d852b2e4
commit 5c496a3078
2 changed files with 7 additions and 0 deletions

View File

@ -269,6 +269,11 @@ class TestTypes(unittest.TestCase):
except ValueError:
pass
def test_validate_float(self):
self.assertEqual(types.validate_value(float, 1), 1.0)
self.assertEqual(types.validate_value(float, '1'), 1.0)
self.assertEqual(types.validate_value(float, 1.1), 1.1)
def test_register_invalid_array(self):
self.assertRaises(ValueError, types.register_type, [])
self.assertRaises(ValueError, types.register_type, [int, str])

View File

@ -218,6 +218,8 @@ def validate_value(datatype, value):
value = value.decode()
elif datatype is bytes and isinstance(value, text):
value = value.encode()
elif datatype is float and not isinstance(value, float):
value = float(value)
elif not isinstance(value, datatype):
raise ValueError(
"Wrong type. Expected '%s', got '%s'" % (