diff --git a/CHANGELOG.rst b/CHANGELOG.rst index dc673df4..cf9dbb53 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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 `__) 1.1.0 diff --git a/jmespath/functions.py b/jmespath/functions.py index 627b569d..05fd6d10 100644 --- a/jmespath/functions.py +++ b/jmespath/functions.py @@ -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) diff --git a/tests/compliance/functions.json b/tests/compliance/functions.json index 7b554450..b889e1c3 100644 --- a/tests/compliance/functions.json +++ b/tests/compliance/functions.json @@ -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