fix: iapply raises on invalid patch paths#35
Merged
Conversation
Pointer.evaluate previously swallowed KeyError/TypeError mid-traversal and returned a partially-walked (parent, key) pair, causing iapply to silently write to the wrong parent or surface confusing downstream AttributeErrors. Make the parent walk strict (any missing intermediate token raises KeyError/IndexError/TypeError from the underlying container), while keeping the leaf lookup lenient so add ops and the list "-" append token continue to work. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Korijn
reviewed
May 26, 2026
Korijn
approved these changes
May 26, 2026
Merged
berendkleinhaneveld
added a commit
that referenced
this pull request
May 26, 2026
This version includes the following: * ci: Update versions of steps and let setup-uv manage the python version (#40) * perf: trim common prefix/suffix in diff_lists (#36) * perf: use str.replace in Pointer escape/unescape (#37) * fix: produce() snapshots mutable values when recording patches (#34) * perf: linear reverse-op construction in diff_dicts/diff_sets (#38) * fix: iapply raises on invalid patch paths (#35) * fix: to_json mutating caller's ops (#33)
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.
Problem
Pointer.evaluate()inpatchdiff/pointer.pyswallowedKeyError/TypeErrormid-traversal and returned a partially-walked(parent, key)pair.iapplythen operated on whatever cursor was last reached, which produced:replaceon/missing/keyagainst{"present": 1}silently mutated the root dict to{"present": 1, "missing": 99}instead of failing./a/bwhereais anint) raisedAttributeError: 'int' object has no attribute 'remove'from deep insideiapplyinstead of a clear traversal error.Reproducer on
master:Approach (sketch B from the plan, narrowed)
I went with a hybrid: strict parent walk, lenient leaf lookup.
tokens[:-1]): notry/except— let the underlying container'sKeyError/IndexError/TypeErrorpropagate naturally.addops andPointer(["-"])rely on), but only whenparentis itself a known container (hasattrforkeys/append/add). If the parent is a primitive, the naturalTypeErrorfromparent[key]propagates.A pure Plan A (just delete the
try/except) initially broke 6 valid-path tests:diff()legitimately producesaddpatches whose leaf key doesn't exist in the source yet (e.g. adding"c"to{"present": 1}).Pointer(["-"]), and[1, 2, 3]["-"]raisesTypeError.The narrow form preserves both while fixing the silent-wrong-parent bug.
Callers of
evaluateOnly one production caller: patchdiff/apply.py:14 (
iapply). Tests reference it but only on valid paths. No external callers depend on the silent behavior.Decision points — please pick before merge
evaluate, lenient at leaf) over A (raise everywhere, simplifyiapply) because pure A breaks validaddand list-append patches. An alternative is to keepevaluatefully lenient and haveiapplyvalidate after — happy to switch if you prefer that surface.KeyError(dict),IndexError(list),TypeError(primitive / set / type-mismatched index). The plan flagged mixed paths as ambiguous; I think letting the container speak for itself is least surprising.ignore_missing=True). Not added. The previous silent behavior masked real bugs; I don't think any caller should rely on it. Flagging in case you disagree.Tests added
In
tests/test_apply.py:test_apply_raises_on_missing_dict_key— assertsKeyErrorforPointer(["missing", "key"])against{"present": 1}.test_apply_raises_on_out_of_range_list_index— assertsIndexErrorforPointer([10, "x"])against[1, 2, 3].test_apply_raises_when_traversing_into_primitive— assertsTypeErrorforPointer(["a", "b"])against{"a": 5}.In
tests/test_pointer.py(same three cases at theevaluatelevel):test_pointer_evaluate_raises_on_missing_dict_key→KeyErrortest_pointer_evaluate_raises_on_out_of_range_list_index→IndexErrortest_pointer_evaluate_raises_when_traversing_into_primitive→TypeErrorVerification
uv run pytest tests/test_apply.py tests/test_pointer.py -v— all 23 pass (6 new + 17 existing).uv run pytest -x -q— full suite, 274 passed.uv run ruff check/ruff format --checkon changed files — clean. (Pre-existing untrackedbenchmarks/profiler.pyhas unrelated T201 print warnings; not touched by this PR.)@berendkleinhaneveld — flagging for direction-check on the three decision points above before this comes out of draft.
🤖 Generated with Claude Code