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
14 changes: 14 additions & 0 deletions voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,20 @@ def test_coerce_in_set_is_applied():
assert Schema({int})({1, 2}) == {1, 2}


def test_coerce_callable_marks_invalid_without_msg():
# Coerce accepts any callable (not just a class). When such a callable
# raises on bad input, the value must be marked Invalid, as the docstring
# promises -- the Enum-message branch used to call issubclass() on the
# callable and leak a raw TypeError. See the class twin Coerce(int), which
# already behaves this way.
def parse_int(v):
return int(v)

validate = Schema(Coerce(parse_int))
with raises(MultipleInvalid, 'expected parse_int'):
validate('foo')


def test_lower_util_handles_various_inputs():
assert Lower(3) == "3"
assert Lower(u"3") == u"3"
Expand Down
7 changes: 6 additions & 1 deletion voluptuous/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,12 @@ def __call__(self, v):
return self.type(v)
except (ValueError, TypeError, InvalidOperation):
msg = self.msg or ('expected %s' % self.type_name)
if not self.msg and Enum and issubclass(self.type, Enum):
if (
not self.msg
and Enum
and isinstance(self.type, type)
and issubclass(self.type, Enum)
):
msg += " or one of %s" % str([e.value for e in self.type])[1:-1]
raise CoerceInvalid(msg)

Expand Down