Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
Next Release (TBD)
==================

* No changes yet.
* Fix ``merge()`` and other variadic functions leaking a raw Python
``TypeError``/``ValueError`` instead of a ``JMESPathTypeError`` when an
argument after the first had an invalid type
(`issue #329 <https://github.com/jmespath/jmespath.py/issues/329>`__)


1.1.0
Expand Down
10 changes: 8 additions & 2 deletions jmespath/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,14 @@ def _validate_arguments(self, args, signature, function_name):
return self._type_check(args, signature, function_name)

def _type_check(self, actual, signature, function_name):
for i in range(len(signature)):
allowed_types = signature[i]['types']
for i in range(len(actual)):
if i < len(signature):
allowed_types = signature[i]['types']
else:
# Variadic function: every trailing argument is described
# by the final signature entry, so keep validating against
# it instead of leaving the extra args unchecked.
allowed_types = signature[-1]['types']
if allowed_types:
self._type_check_single(actual[i], allowed_types,
function_name)
Expand Down
17 changes: 17 additions & 0 deletions tests/compliance/functions.json
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,23 @@
"expression": "merge(`{\"a\": 1, \"b\": 2}`, `{\"a\": 2, \"c\": 3}`, `{\"d\": 4}`)",
"result": {"a": 2, "b": 2, "c": 3, "d": 4}
},
{
"comment": "variadic argument after the first must also be type checked",
"expression": "merge(`{\"a\": 1}`, `null`)",
"error": "invalid-type"
},
{
"expression": "merge(`{\"a\": 1}`, null_key)",
"error": "invalid-type"
},
{
"expression": "merge(`{\"a\": 1}`, `{\"b\": 2}`, `5`)",
"error": "invalid-type"
},
{
"expression": "merge(`{\"a\": 1}`, str)",
"error": "invalid-type"
},
{
"expression": "min(numbers)",
"result": -1
Expand Down