import unittest import jmespath from jmespath import functions class CustomFunctions(functions.Functions): @functions.signature({'types': ['string', 'array', 'object', 'null']}) def _func_length0(self, s): return 0 if s is None else len(s) class TestCustomFunctions(unittest.TestCase): def setUp(self): self.options = jmespath.Options(custom_functions=CustomFunctions()) def test_null_to_nonetype(self): data = { 'a': { 'b': [1, 2, 3] } } self.assertEqual(jmespath.search('length0(a.b)', data, self.options), 3) self.assertEqual(jmespath.search('length0(a.c)', data, self.options), 0)