From 3559782e72d2c0a8360915b47e60f5c17c25986a Mon Sep 17 00:00:00 2001 From: Illia Petrov Date: Fri, 27 Mar 2026 11:14:33 -0700 Subject: [PATCH 1/2] fix: update Index.__hash__ to reference self.indices instead of self.index The Index class was refactored to use *indices (tuple) instead of a single index, but __hash__ was not updated accordingly, causing AttributeError when Index objects are used in hash-based collections. Fixes #224 --- jsonpath_ng/jsonpath.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsonpath_ng/jsonpath.py b/jsonpath_ng/jsonpath.py index 9d9b9f5..6c95877 100644 --- a/jsonpath_ng/jsonpath.py +++ b/jsonpath_ng/jsonpath.py @@ -779,7 +779,7 @@ def _pad_value(self, value): value += [{} for __ in range(pad)] def __hash__(self): - return hash(self.index) + return hash(self.indices) class Slice(JSONPath): From 2104335442ee89b3368e085c0542e473526a27ea Mon Sep 17 00:00:00 2001 From: Illia Petrov Date: Fri, 27 Mar 2026 11:20:31 -0700 Subject: [PATCH 2/2] test: add tests for Index.__hash__ with single and multiple indices --- tests/test_jsonpath.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/test_jsonpath.py b/tests/test_jsonpath.py index c3930e7..f4e8765 100644 --- a/tests/test_jsonpath.py +++ b/tests/test_jsonpath.py @@ -3,7 +3,7 @@ import pytest from typing import Callable from jsonpath_ng.ext.parser import parse as ext_parse -from jsonpath_ng.jsonpath import DatumInContext, Fields, Root, This +from jsonpath_ng.jsonpath import DatumInContext, Fields, Index, Root, This from jsonpath_ng.lexer import JsonPathLexerError from jsonpath_ng.parser import parse as base_parse from jsonpath_ng import JSONPath @@ -435,3 +435,15 @@ def test_nested_index_auto_id(auto_id_field, parse, string, target): def test_invalid_hyphenation_in_key(): with pytest.raises(JsonPathLexerError): base_parse("foo.-baz") + + +def test_index_hashable(): + idx = Index(0) + assert hash(idx) == hash((0,)) + assert {idx: "value"}[idx] == "value" + + +def test_index_multi_indices_hashable(): + idx = Index(0, 1, 2) + assert hash(idx) == hash((0, 1, 2)) + assert {idx: "value"}[idx] == "value"