Skip to content

Commit

Permalink
Supports functions with no arguments (#26)
Browse files Browse the repository at this point in the history
* consider supporting functions with no arguments

* supports functions with no arguments

* remove extra change

* bumped version
  • Loading branch information
springcomp authored Dec 26, 2023
1 parent bfbeb8f commit aef45e9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion jmespath/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from jmespath import parser
from jmespath.visitor import Options

__version__ = '1.1.2'
__version__ = '1.1.3'


def compile(expression, options=None):
Expand Down
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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name='jmespath-community',
version='1.1.2',
version='1.1.3',
description='JSON Matching Expressions',
long_description=io.open('README.rst', encoding='utf-8').read(),
author='James Saryerwinnie, Springcomp',
Expand Down
15 changes: 15 additions & 0 deletions tests/test_signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ 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)

def _make_test(self, funcName):
for name, method in compat.get_methods(TestFunctionSignatures):
print(name)
Expand All @@ -64,6 +72,13 @@ def _function_with_variadic_arguments(self, arg1, *arguments):
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

if __name__ == '__main__':
unittest.main()

0 comments on commit aef45e9

Please sign in to comment.