Michael Merickel
2017-05-23 b3f153efd2a22a074ae833e73a6411669d340a52
Merge pull request #3054 from fangpenlin/feature.closest-predicate-match-error-msg

Fix #1603, add closest predicate name in error message
2 files modified
27 ■■■■ changed files
pyramid/config/util.py 17 ●●●● patch | view | raw | blame | history
pyramid/tests/test_config/test_util.py 10 ●●●●● patch | view | raw | blame | history
pyramid/config/util.py
@@ -36,7 +36,7 @@
       config.add_view(
           'mypackage.views.my_view',
           route_name='ok',
           route_name='ok',
           request_method=not_('POST')
           )
@@ -69,7 +69,7 @@
        # if the underlying predicate doesnt return a value, it's not really
        # a predicate, it's just something pretending to be a predicate,
        # so dont update the hash
        if val:
        if val:
            val = '!' + val
        return val
@@ -90,7 +90,7 @@
# over = before
class PredicateList(object):
    def __init__(self):
        self.sorter = TopologicalSorter()
        self.last_added = None
@@ -152,7 +152,16 @@
                weights.append(1 << n + 1)
                preds.append(pred)
        if kw:
            raise ConfigurationError('Unknown predicate values: %r' % (kw,))
            from difflib import get_close_matches
            closest = []
            names = [ name for name, _ in ordered ]
            for name in kw:
                closest.extend(get_close_matches(name, names, 3))
            raise ConfigurationError(
                'Unknown predicate values: %r (did you mean %s)'
                % (kw, ','.join(closest))
            )
        # A "order" is computed for the predicate list.  An order is
        # a scoring.
        #
pyramid/tests/test_config/test_util.py
@@ -365,6 +365,16 @@
        from pyramid.exceptions import ConfigurationError
        self.assertRaises(ConfigurationError, self._callFUT, unknown=1)
    def test_predicate_close_matches(self):
        from pyramid.exceptions import ConfigurationError
        with  self.assertRaises(ConfigurationError) as context:
            self._callFUT(method='GET')
        expected_msg = (
            "Unknown predicate values: {'method': 'GET'} "
            "(did you mean request_method)"
        )
        self.assertEqual(context.exception.args[0], expected_msg)
    def test_notted(self):
        from pyramid.config import not_
        from pyramid.testing import DummyRequest