From aafaf73655c11ca1d6645d85e1754be6996540dd Mon Sep 17 00:00:00 2001 From: Fang-Pen Lin Date: Mon, 22 May 2017 14:38:53 -0700 Subject: Add test for closest predicate error message --- pyramid/tests/test_config/test_util.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pyramid/tests/test_config/test_util.py b/pyramid/tests/test_config/test_util.py index 398b6fba8..bb86a1f56 100644 --- a/pyramid/tests/test_config/test_util.py +++ b/pyramid/tests/test_config/test_util.py @@ -365,6 +365,16 @@ class TestPredicateList(unittest.TestCase): 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 -- cgit v1.2.3 From 08422ee6340cbcd225dcfc26c7c0aa3165449a58 Mon Sep 17 00:00:00 2001 From: Fang-Pen Lin Date: Mon, 22 May 2017 14:40:44 -0700 Subject: Fix #1603, add closest predicate name in error message --- pyramid/config/util.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/pyramid/config/util.py b/pyramid/config/util.py index 67bba9593..d09d37d94 100644 --- a/pyramid/config/util.py +++ b/pyramid/config/util.py @@ -1,3 +1,4 @@ +from difflib import get_close_matches from hashlib import md5 import inspect @@ -36,7 +37,7 @@ class not_(object): config.add_view( 'mypackage.views.my_view', - route_name='ok', + route_name='ok', request_method=not_('POST') ) @@ -69,7 +70,7 @@ class Notted(object): # 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 +91,7 @@ class Notted(object): # over = before class PredicateList(object): - + def __init__(self): self.sorter = TopologicalSorter() self.last_added = None @@ -152,7 +153,15 @@ class PredicateList(object): weights.append(1 << n + 1) preds.append(pred) if kw: - raise ConfigurationError('Unknown predicate values: %r' % (kw,)) + 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. # -- cgit v1.2.3 From 96dd805d55c5576778580ff52e978ff8aebc3a04 Mon Sep 17 00:00:00 2001 From: Fang-Pen Lin Date: Mon, 22 May 2017 14:54:46 -0700 Subject: =?UTF-8?q?Load=20difflib=20on-demand=20so=20that=20it=20won?= =?UTF-8?q?=E2=80=99t=20take=20message=20proactively?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pyramid/config/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyramid/config/util.py b/pyramid/config/util.py index d09d37d94..63f06ff9b 100644 --- a/pyramid/config/util.py +++ b/pyramid/config/util.py @@ -1,4 +1,3 @@ -from difflib import get_close_matches from hashlib import md5 import inspect @@ -153,6 +152,7 @@ class PredicateList(object): weights.append(1 << n + 1) preds.append(pred) if kw: + from difflib import get_close_matches closest = [] names = [ name for name, _ in ordered ] for name in kw: -- cgit v1.2.3