From 535dd6e1c3ba6f8848238b07a02d01fba5e1676b Mon Sep 17 00:00:00 2001 From: Gyanu Mayank Date: Wed, 2 Sep 2026 08:12:16 +0530 Subject: [PATCH] Return null for a slice whose step is 0. value[::0] raises ValueError in Python. A projection that cannot run should be null, same as slicing a non-array. --- jmespath/visitor.py | 7 +++++-- tests/test_search.py | 3 +++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/jmespath/visitor.py b/jmespath/visitor.py index 15fb1774..4045665a 100644 --- a/jmespath/visitor.py +++ b/jmespath/visitor.py @@ -218,8 +218,11 @@ def visit_index_expression(self, node, value): def visit_slice(self, node, value): if not isinstance(value, list): return None - s = slice(*node['children']) - return value[s] + try: + s = slice(*node['children']) + return value[s] + except ValueError: + return None def visit_key_val_pair(self, node, value): return self.visit(node['children'][0], value) diff --git a/tests/test_search.py b/tests/test_search.py index 4832079b..260669ba 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -62,3 +62,6 @@ def test_can_handle_decimals_as_numeric_type(self): result = decimal.Decimal('3') self.assertEqual(jmespath.search('[?a >= `1`].a', [{'a': result}]), [result]) + + def test_zero_step_slice_returns_none(self): + self.assertIsNone(jmespath.search('[::0]', [1, 2, 3]))