From 19c0abdfc42ef2fd72f35bc844f1f3dd81b65d72 Mon Sep 17 00:00:00 2001 From: Berend Klein Haneveld Date: Tue, 26 May 2026 09:31:51 +0200 Subject: [PATCH] Fix to_json mutating caller's ops 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 --- patchdiff/serialize.py | 5 +---- tests/test_serialize.py | 12 +++++++++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/patchdiff/serialize.py b/patchdiff/serialize.py index 1bcf6b1..e2eab68 100644 --- a/patchdiff/serialize.py +++ b/patchdiff/serialize.py @@ -3,10 +3,7 @@ def to_str_paths(ops: List) -> List: - str_ops = ops.copy() - for op in str_ops: - op["path"] = str(op["path"]) - return str_ops + return [{**op, "path": str(op["path"])} for op in ops] def to_json(ops: List, **kwargs) -> str: diff --git a/tests/test_serialize.py b/tests/test_serialize.py index 0665cdb..e2ebef2 100644 --- a/tests/test_serialize.py +++ b/tests/test_serialize.py @@ -1,4 +1,14 @@ -from patchdiff import diff, to_json +from patchdiff import apply, diff, to_json + + +def test_to_json_does_not_mutate_ops(): + a = {"x": [1, 2, 3]} + b = {"x": [1, 9, 3]} + ops, _ = diff(a, b) + + to_json(ops) + + assert apply(a, ops) == b def test_to_json():