fix: to_json no longer mutates caller's ops#33
Merged
Conversation
to_str_paths used a shallow list copy, so overwriting op["path"] with str(op["path"]) mutated the caller's Pointer-bearing ops dicts and broke subsequent apply() calls. Switch to a per-dict copy via a list comprehension so the originals stay intact. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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.
Summary
patchdiff.to_json(ops)was mutating the caller'sops.to_str_pathsdid a shallowops.copy(), so the inner dicts were still shared. Overwritingop[\"path\"]withstr(op[\"path\"])then replaced thePointerin the caller's dicts with strings, which broke subsequentapply()calls. Fix by copying each dict ({**op, \"path\": str(op[\"path\"])}) instead of just the outer list.Reproducer on
masterOn
masterthis raises:On this branch it prints
{'x': [1, 9, 3]}.New test
tests/test_serialize.py::test_to_json_does_not_mutate_ops— callsto_json(ops), discards the result, then assertsapply(a, ops) == b. Fails onmasterwith the traceback above; passes on this branch.Existing tests
tests/test_serialize.py::test_to_jsonstill passes (sanity check on the JSON output).uv run pytest -x -qis green (269 passed).Test plan
uv run pytest tests/test_serialize.py -vuv run pytest -x -quv run ruff check patchdiff/serialize.py tests/test_serialize.pyuv run ruff format --check patchdiff/serialize.py tests/test_serialize.py🤖 Generated with Claude Code