Skip to content

Remove unreachable code from make_patch and the jsonpointer < 3.2 shim - #227

Merged
stefankoegl merged 2 commits into
masterfrom
claude/remove-dead-code
Oct 10, 2026
Merged

stefankoegl merged 2 commits into
masterfrom
claude/remove-dead-code

Conversation

@stefankoegl

@stefankoegl stefankoegl commented Oct 10, 2026 •

Copy link
Copy Markdown
Owner

This removes internal code that #226 found no way to reach. Branch coverage of jsonpatch.py goes to 100%, with no new tests. The only lines still uncovered are the public index helpers on PatchOperation and MoveOperation (path, key, get_part, set_part, from_path, from_key, get_from_part, set_from_part). They have no callers in jsonpatch, but they are public and may be used outside of it, so they stay.

What is removed

_item_after(), and the "move into own child" check in _item_removed(). _item_after had a single caller, which passed inserted=False. In that mode it would only shift a location if the earlier add sat after the removed item in the same array.

make_patch compares the documents in order, and every add and remove happens where the comparison currently stands. Items in the part already compared never change index. So an earlier add that a later remove turns into a move is always in front of the removed item. That has two consequences:

  • _item_after always returned its input.
  • The added item can't be inside the removed one, so the _is_inside(target, moved_from) check never triggered.

A comment in _item_removed now states this invariant. The mirror check in _item_added stays, because it is reachable.

_to_last(). #178 added it to reject pointers into strings while jsonpointer < 3.2 still indexed into them. jsonpointer>=3.2 has since become a requirement, and it raises the same JsonPointerException itself, so the helper's check could no longer trigger. The call sites now use pointer.to_last() directly. One side effect: a custom pointer_cls that overrides to_last to index into strings would no longer be stopped by jsonpatch.

Verification

  • tests.py and property_tests.py pass, run as in CI. The MakePatchProperties tests also pass with HYPOTHESIS_PROFILE=thorough.
  • Differential check against master's jsonpatch.py:
    • make_patch gave byte-identical patches for 200,000 random document pairs, built to produce many moves.
    • 1,000,000 random single operations gave the same result, or the same exception type and message. These included pointers into strings, - and indices past the end.
    • The same harness flagged a deliberately broken _item_removed within the first few thousand cases, so it does catch changes.

🤖 Generated with Claude Code

https://claude.ai/code/session_015o6rtuujv61fERXZhQaL9i

- PatchOperation.path, .key, get_part() and set_part(), and
  MoveOperation.from_path, .from_key, get_from_part() and
  set_from_part() kept the indices of the old DiffBuilder's operations
  up to date, and have had no callers since it was rewritten (#179)
- _item_after() was only called with inserted=False, and never moved
  its location then either: make_patch compares the documents in order,
  so an earlier 'add' that a 'remove' turns into a move is in front of
  the removed item. For the same reason it is never inside it, so the
  check against moving a value into its own children is dropped from
  _item_removed()
- _to_last() rejected pointers into strings for jsonpointer < 3.2,
  which is no longer supported; jsonpointer itself rejects them now

make_patch gives the same patches as before, and applying patches gives
the same results and exceptions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015o6rtuujv61fERXZhQaL9i
They are public, and may be used outside of jsonpatch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015o6rtuujv61fERXZhQaL9i
@stefankoegl stefankoegl changed the title Remove dead code Remove unreachable code from make_patch and the jsonpointer < 3.2 shim Oct 10, 2026
@stefankoegl
stefankoegl merged commit f193719 into master Oct 10, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants