Skip to content

Commit 732518a

Browse files
committed
Use singular argument when expected arity is 1
Before: invalid-arity: Expected 1 arguments for function length(), received 2 Now: invalid-arity: Expected 1 argument for function length(), received 2
1 parent 50c15d1 commit 732518a

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

jmespath/exceptions.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,29 @@ def __init__(self, expected, actual, name):
7171
self.expression = None
7272

7373
def __str__(self):
74-
return ("Expected %s arguments for function %s(), "
75-
"received %s" % (self.expected_arity,
76-
self.function_name,
77-
self.actual_arity))
74+
return ("Expected %s %s for function %s(), "
75+
"received %s" % (
76+
self.expected_arity,
77+
self._pluralize('argument', self.expected_arity),
78+
self.function_name,
79+
self.actual_arity))
80+
81+
def _pluralize(self, word, count):
82+
if count == 1:
83+
return word
84+
else:
85+
return word + 's'
7886

7987

8088
@with_str_method
8189
class VariadictArityError(ArityError):
8290
def __str__(self):
83-
return ("Expected at least %s arguments for function %s, "
84-
"received %s" % (self.expected_arity,
85-
self.function_name,
86-
self.actual_arity))
91+
return ("Expected at least %s %s for function %s(), "
92+
"received %s" % (
93+
self.expected_arity,
94+
self._pluralize('argument', self.expected_arity),
95+
self.function_name,
96+
self.actual_arity))
8797

8898

8999
@with_str_method

tests/test_functions.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import json
55

66
import jmespath
7-
from jmespath.exceptions import JMESPathTypeError
7+
from jmespath import exceptions
88

99

1010
class TestFunctions(unittest.TestCase):
@@ -19,7 +19,7 @@ def test_can_max_datetimes(self):
1919
self.assertEqual(json.loads(result), str(data[-1]))
2020

2121
def test_type_error_messages(self):
22-
with self.assertRaises(JMESPathTypeError) as e:
22+
with self.assertRaises(exceptions.JMESPathTypeError) as e:
2323
jmespath.search('length(@)', 2)
2424
exception = e.exception
2525
# 1. Function name should be in error message
@@ -31,3 +31,27 @@ def test_type_error_messages(self):
3131
str(exception))
3232
# 4. Mention the actual type.
3333
self.assertIn('received: "number"', str(exception))
34+
35+
def test_singular_in_error_message(self):
36+
with self.assertRaises(exceptions.ArityError) as e:
37+
jmespath.search('length(@, @)', [0, 1])
38+
exception = e.exception
39+
self.assertEqual(
40+
str(exception),
41+
'Expected 1 argument for function length(), received 2')
42+
43+
def test_error_message_is_pluralized(self):
44+
with self.assertRaises(exceptions.ArityError) as e:
45+
jmespath.search('sort_by(@)', [0, 1])
46+
exception = e.exception
47+
self.assertEqual(
48+
str(exception),
49+
'Expected 2 arguments for function sort_by(), received 1')
50+
51+
def test_variadic_is_pluralized(self):
52+
with self.assertRaises(exceptions.VariadictArityError) as e:
53+
jmespath.search('not_null()', 'foo')
54+
exception = e.exception
55+
self.assertEqual(
56+
str(exception),
57+
'Expected at least 1 argument for function not_null(), received 0')

0 commit comments

Comments
 (0)