Disallow Booleans for configtypes.Int.from_py

This commit is contained in:
Florian Bruhin 2017-06-15 14:09:43 +02:00
parent 4e729bb9ec
commit 001312ca82
2 changed files with 9 additions and 4 deletions

View File

@ -120,10 +120,14 @@ class BaseType:
value: The value to check.
pytype: A Python type to check the value against.
"""
if value is None and not self.none_ok:
raise configexc.ValidationError(value, "may not be null!")
if value is None:
if not self.none_ok:
raise configexc.ValidationError(value, "may not be null!")
else:
return
if value is not None and not isinstance(value, pytype):
if (not isinstance(value, pytype) or
pytype is int and isinstance(value, bool)):
if isinstance(pytype, tuple):
expected = ' or '.join(typ.__name__ for typ in pytype)
else:
@ -132,7 +136,7 @@ class BaseType:
value, "expected a value of type {} but got {}.".format(
expected, type(value).__name__))
if value is not None and isinstance(value, str):
if isinstance(value, str):
self._basic_str_validation(value)
def _basic_str_validation(self, value):

View File

@ -736,6 +736,7 @@ class TestInt:
({}, 2.5),
({}, 'foobar'),
({'minval': 2, 'maxval': 3}, 1),
({}, True),
])
def test_from_py_invalid(self, klass, kwargs, val):
with pytest.raises(configexc.ValidationError):