diff options
| author | Michael Merickel <michael@merickel.org> | 2017-05-22 15:11:05 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-05-22 15:11:05 -0700 |
| commit | b3f153efd2a22a074ae833e73a6411669d340a52 (patch) | |
| tree | 98ab3b066fff97fc4bc6d72240fdaeadbb782a20 | |
| parent | 8c4d422965b633f31967ceed1e6cc25cc616d0bf (diff) | |
| parent | 96dd805d55c5576778580ff52e978ff8aebc3a04 (diff) | |
| download | pyramid-b3f153efd2a22a074ae833e73a6411669d340a52.tar.gz pyramid-b3f153efd2a22a074ae833e73a6411669d340a52.tar.bz2 pyramid-b3f153efd2a22a074ae833e73a6411669d340a52.zip | |
Merge pull request #3054 from fangpenlin/feature.closest-predicate-match-error-msg
Fix #1603, add closest predicate name in error message
| -rw-r--r-- | pyramid/config/util.py | 17 | ||||
| -rw-r--r-- | pyramid/tests/test_config/test_util.py | 10 |
2 files changed, 23 insertions, 4 deletions
diff --git a/pyramid/config/util.py b/pyramid/config/util.py index 67bba9593..63f06ff9b 100644 --- a/pyramid/config/util.py +++ b/pyramid/config/util.py @@ -36,7 +36,7 @@ class not_(object): config.add_view( 'mypackage.views.my_view', - route_name='ok', + route_name='ok', request_method=not_('POST') ) @@ -69,7 +69,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 +90,7 @@ class Notted(object): # over = before class PredicateList(object): - + def __init__(self): self.sorter = TopologicalSorter() self.last_added = None @@ -152,7 +152,16 @@ class PredicateList(object): 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. # 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 |
