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): 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"