Skip to content

Commit 1c9c35c

Browse files
committed
Merge branch 'lostinthefrost-develop' into develop
PR #151 * lostinthefrost-develop: bugfix: min_by and max_by given an empty array
2 parents f4c8806 + 373c640 commit 1c9c35c

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

jmespath/functions.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,20 @@ def _func_min_by(self, array, expref):
331331
keyfunc = self._create_key_func(expref,
332332
['number', 'string'],
333333
'min_by')
334-
return min(array, key=keyfunc)
334+
if array:
335+
return min(array, key=keyfunc)
336+
else:
337+
return None
335338

336339
@signature({'types': ['array']}, {'types': ['expref']})
337340
def _func_max_by(self, array, expref):
338341
keyfunc = self._create_key_func(expref,
339342
['number', 'string'],
340-
'min_by')
341-
return max(array, key=keyfunc)
343+
'max_by')
344+
if array:
345+
return max(array, key=keyfunc)
346+
else:
347+
return None
342348

343349
def _create_key_func(self, expref, allowed_types, function_name):
344350
def keyfunc(x):

tests/compliance/functions.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,10 @@
701701
"expression": "max_by(people, &to_number(age_str))",
702702
"result": {"age": 50, "age_str": "50", "bool": false, "name": "d"}
703703
},
704+
{
705+
"expression": "max_by(`[]`, &age)",
706+
"result": null
707+
},
704708
{
705709
"expression": "min_by(people, &age)",
706710
"result": {"age": 10, "age_str": "10", "bool": true, "name": 3}
@@ -720,6 +724,10 @@
720724
{
721725
"expression": "min_by(people, &to_number(age_str))",
722726
"result": {"age": 10, "age_str": "10", "bool": true, "name": 3}
727+
},
728+
{
729+
"expression": "min_by(`[]`, &age)",
730+
"result": null
723731
}
724732
]
725733
}, {

0 commit comments

Comments
 (0)