Skip to content

Commit add1cfe

Browse files
committed
Optional custom function arguments
1 parent 2ad18b0 commit add1cfe

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

jmespath/functions.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,22 @@ def call_function(self, function_name, resolved_args):
8181
return function(self, *resolved_args)
8282

8383
def _validate_arguments(self, args, signature, function_name):
84-
if signature and signature[-1].get('variadic'):
85-
if len(args) < len(signature):
86-
raise exceptions.VariadictArityError(
87-
len(signature), len(args), function_name)
88-
elif len(args) != len(signature):
89-
raise exceptions.ArityError(
90-
len(signature), len(args), function_name)
91-
return self._type_check(args, signature, function_name)
84+
min_args = sum(1 for sig in signature if not sig.get('optional', False))
85+
if len(args) < min_args:
86+
raise exceptions.ArityError(min_args, len(args), function_name)
87+
88+
if len(args) > len(signature) and not signature[-1].get('variadic'):
89+
raise exceptions.ArityError(len(signature), len(args), function_name)
90+
91+
self._type_check(args, signature, function_name)
92+
9293

9394
def _type_check(self, actual, signature, function_name):
94-
for i in range(len(signature)):
95+
for i in range(len(actual)):
9596
allowed_types = signature[i]['types']
96-
if allowed_types:
97-
self._type_check_single(actual[i], allowed_types,
98-
function_name)
97+
if allowed_types and not (signature[i].get('optional') and actual[i] is None):
98+
self._type_check_single(actual[i], allowed_types, function_name)
99+
99100

100101
def _type_check_single(self, current, types, function_name):
101102
# Type checking involves checking the top level type,

0 commit comments

Comments
 (0)