Skip to content

Commit

Permalink
supports functions with no arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
springcomp committed Dec 26, 2023
1 parent 6864c68 commit 954d95b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 7 additions & 2 deletions jmespath/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,14 @@ def call_function(self, function_name, resolved_args, *args, **kwargs):
return function(self, *resolved_args)

def _validate_arguments(self, args, signature, function_name):
required_arguments_count = len([param for param in signature if not param.get('optional') or not param['optional']])
optional_arguments_count = len([param for param in signature if param.get('optional') and param['optional']])

if len(signature) == 0:
return self._type_check(args, signature, function_name)

required_arguments_count = len([param for param in signature if param and (not param.get('optional') or not param['optional'])])
optional_arguments_count = len([param for param in signature if param and param.get('optional') and param['optional']])
has_variadic = signature[-1].get('variadic') if signature != None else False

if has_variadic:
if len(args) < len(signature):
raise exceptions.VariadictArityError(
Expand Down
8 changes: 8 additions & 0 deletions tests/test_signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def test_signature_with_variadic_arguments(self):
self._functions._validate_arguments(['string'], signature, function_name)
)

def test_signature_with_empty_argument(self):
(function_name, signature) = self._make_test("_function_with_empty_argument")
self._functions._validate_arguments([], signature, function_name)

def test_signature_with_no_arguments(self):
(function_name, signature) = self._make_test("_function_with_no_arguments")
self._functions._validate_arguments([], signature, function_name)
Expand Down Expand Up @@ -69,6 +73,10 @@ def _function_with_optional_arguments(self, arg1, opt1, opt2):
return None

@functions.signature({})
def _function_with_empty_argument(self):
return None

@functions.signature()
def _function_with_no_arguments(self):
return None

Expand Down

0 comments on commit 954d95b

Please sign in to comment.