diff --git a/CHANGES.txt b/CHANGES.txt index b65f23d..cb631f3 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -13,8 +13,11 @@ Bug Fixes reverted. When you supply ``None`` as the ``default`` argument to a String, the rendered serialize() value will again be ``'None'``. Sorry. -- Normalize ``message`` argument to ``colander.Function`` -> ``msg`` (matches - other APIs. +- Normalize ``colander.Function`` argument ``message`` to be ``msg``. This now + 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 ``ParseErrorr`` in ``parse_date``, so that the validation machinery diff --git a/colander/__init__.py b/colander/__init__.py index ddb3849..c5ef5cb 100644 --- a/colander/__init__.py +++ b/colander/__init__.py @@ -230,8 +230,18 @@ class Function(object): The default value for the ``msg`` when not provided via the constructor is ``Invalid value``. """ - def __init__(self, function, msg=_('Invalid value')): + def __init__(self, function, msg=None, message=None): 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 def __call__(self, node, value): diff --git a/colander/tests/test_colander.py b/colander/tests/test_colander.py index 63958de..9915c25 100644 --- a/colander/tests/test_colander.py +++ b/colander/tests/test_colander.py @@ -243,6 +243,27 @@ class TestFunction(unittest.TestCase): e = invalid_exc(validator, None, None) 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): validator = self._makeOne(lambda x: False, msg='fail ${val}') e = invalid_exc(validator, None, None)