Match nothing instead of raising KeyError for Index on a dict (fixes #93)#234
Open
gaoflow wants to merge 1 commit into
Open
Match nothing instead of raising KeyError for Index on a dict (fixes #93)#234gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
Index.find (concrete `[n]`) raised an uncaught KeyError when applied to a dict, e.g. `$.*[0]` where the wildcard matched a dict value. The guard `len(datum.value) > index` is satisfied by a dict, but `datum.value[index]` is then a key lookup that raises. The sibling Slice and Fields paths handle dicts gracefully, and the class docstring already promises it 'will not crash but will not match anything'. Return early when the datum is a dict so integer indexing matches nothing. Sequence and string indexing are unchanged. Fixes h2non#93
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #93.
Index.find(the concrete[n]syntax) raises an uncaughtKeyErrorwhen it is applied to a dict:The wildcard
*matches every value (including the dict{"d": 2}), then[0]is applied to each — and indexing the dict raises. Minimal repro:jsonpath_ng.parse("[0]").find({"foo": 1}).This contradicts the class's own docstring — "If the datum is None or not long enough, it will not crash but will not match anything" — and is inconsistent with the sibling paths:
Slice([*]) andFields(.foo) both handle a dict datum gracefully. OnlyIndexcrashes.Cause
In
Index._find_base, the guardassumes anything with a
len()is integer-indexable. A dict satisfieslen(datum.value) > index, butdatum.value[index]is then a key lookup that raisesKeyError.Fix
Return early when the datum is a dict — integer indices apply to sequences, not mappings, so (per the docstring) match nothing rather than crashing. Sequence and string indexing are untouched.
The fix is scoped to the
findcontract;find_or_createon an empty dict still converts it to a list first (in thecreateblock above), so that path is unaffected.Verification
find_test_casestable:("[0]", {"foo": 1}, [], [])and the issue Expression with wildcard then index selection errors with KeyError #93 repro("$.*[0].b", {"a": [{"b": 1}], "c": {"d": 2}}, [1], ["((a.[0]).b)"]). They fail withKeyErrorbefore the fix and pass after (confirmed viagit stash).### Fixedentry toCHANGELOG.mdunder[Unreleased].This pull request was prepared with the assistance of AI, under my direction and review.