add message back to Function and emit warning

fixes #64
This commit is contained in:
Clayton Parker
2013-08-16 12:37:07 +02:00
parent b46bde72d1
commit 6bade28ee3
3 changed files with 37 additions and 3 deletions

View File

@@ -13,8 +13,11 @@ Bug Fixes
reverted. When you supply ``None`` as the ``default`` argument to a String, reverted. When you supply ``None`` as the ``default`` argument to a String,
the rendered serialize() value will again be ``'None'``. Sorry. the rendered serialize() value will again be ``'None'``. Sorry.
- Normalize ``message`` argument to ``colander.Function`` -> ``msg`` (matches - Normalize ``colander.Function`` argument ``message`` to be ``msg``. This now
other APIs. matches other APIs within Colander. The ``message`` argument is now
deprecated and a warning will be emitted.
https://github.com/Pylons/colander/issues/31
https://github.com/Pylons/colander/issues/64
- ``iso8601.py``: Convert ``ValueError`` (raised by ``datetime``) into - ``iso8601.py``: Convert ``ValueError`` (raised by ``datetime``) into
``ParseErrorr`` in ``parse_date``, so that the validation machinery ``ParseErrorr`` in ``parse_date``, so that the validation machinery

View File

@@ -230,8 +230,18 @@ class Function(object):
The default value for the ``msg`` when not provided via the The default value for the ``msg`` when not provided via the
constructor is ``Invalid value``. constructor is ``Invalid value``.
""" """
def __init__(self, function, msg=_('Invalid value')): def __init__(self, function, msg=None, message=None):
self.function = function self.function = function
# Handle bw compat
if msg is None and message is None:
msg = _('Invalid value')
elif message is not None:
warnings.warn(
'The "message" argument has been deprecated, use "msg" '
'instead.',
DeprecationWarning
)
msg = message
self.msg = msg self.msg = msg
def __call__(self, node, value): def __call__(self, node, value):

View File

@@ -243,6 +243,27 @@ class TestFunction(unittest.TestCase):
e = invalid_exc(validator, None, None) e = invalid_exc(validator, None, None)
self.assertEqual(e.msg, 'fail') self.assertEqual(e.msg, 'fail')
def test_deprecated_message(self):
validator = self._makeOne(lambda x: False, message='depr')
e = invalid_exc(validator, None, None)
self.assertEqual(e.msg.interpolate(), 'depr')
def test_deprecated_message_warning(self):
import warnings
orig_warn = warnings.warn
log = []
def warn(message, category=None, stacklevel=1):
log.append((message, category, stacklevel))
try:
# Monkey patching warn since catch_warnings context manager
# is not working when running the full suite
warnings.warn = warn
validator = self._makeOne(lambda x: False, message='depr')
invalid_exc(validator, None, None)
self.assertEqual(len(log), 1)
finally:
warnings.warn = orig_warn
def test_error_message_adds_mapping_to_configured_message(self): def test_error_message_adds_mapping_to_configured_message(self):
validator = self._makeOne(lambda x: False, msg='fail ${val}') validator = self._makeOne(lambda x: False, msg='fail ${val}')
e = invalid_exc(validator, None, None) e = invalid_exc(validator, None, None)