Skip to content

Commit 2494ea9

Browse files
authored
Merge pull request #3 from includeamin/improve-interface
chore: improve naming and enhance comments
2 parents 0192d78 + 8dc84d1 commit 2494ea9

4 files changed

Lines changed: 42 additions & 27 deletions

File tree

src/diff/delta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
@dataclasses.dataclass
66
class Delta:
7-
op: typing.Literal["deleted", "modified", "added"]
7+
operation: typing.Literal["deleted", "modified", "added"]
88
path: str
99
new_value: typing.Any | None
1010
old_value: typing.Any | None

src/diff/diff.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,46 @@
1+
import typing
2+
13
from diff import json_path
24
from diff.delta import Delta
35

46

5-
def diff(new: dict, old: dict) -> list[Delta]:
7+
def diff(new: dict[str, typing.Any], old: dict[str, typing.Any]) -> list[Delta]:
68
new_path_map = json_path.path_value_map(
79
new, include_root=True, leaves_only=True, include_containers=False
810
)
911
old_path_map = json_path.path_value_map(
1012
old, include_root=True, leaves_only=False, include_containers=False
1113
)
12-
ops: list[Delta] = []
14+
operations: list[Delta] = []
1315

14-
# deleted
1516
deleted = old_path_map.keys() - new_path_map.keys()
16-
for item in deleted:
17-
ops.append( # noqa: PERF401
18-
Delta(path=item, op="deleted", old_value=old_path_map[item], new_value=None)
17+
for key in deleted:
18+
operations.append( # noqa: PERF401
19+
Delta(
20+
path=key,
21+
operation="deleted",
22+
old_value=old_path_map[key],
23+
new_value=None,
24+
)
1925
)
2026

21-
# added
2227
added = new_path_map.keys() - old_path_map.keys()
23-
for item in added:
24-
ops.append( # noqa: PERF401
25-
Delta(path=item, op="added", old_value=None, new_value=new_path_map[item])
28+
for key in added:
29+
operations.append( # noqa: PERF401
30+
Delta(
31+
path=key, operation="added", old_value=None, new_value=new_path_map[key]
32+
)
2633
)
2734

28-
# modified
2935
shared_keys = new_path_map.keys() & old_path_map.keys()
30-
for item in shared_keys:
31-
if old_path_map[item] != new_path_map[item]:
32-
ops.append( # noqa: PERF401
36+
for key in shared_keys:
37+
if old_path_map[key] != new_path_map[key]:
38+
operations.append( # noqa: PERF401
3339
Delta(
34-
path=item,
35-
op="modified",
36-
old_value=old_path_map[item],
37-
new_value=new_path_map[item],
40+
path=key,
41+
operation="modified",
42+
old_value=old_path_map[key],
43+
new_value=new_path_map[key],
3844
)
3945
)
40-
# exlude root diff
41-
return ops
46+
return operations

src/diff/patch.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,10 @@ def pop_by_json_path(
356356
def patch(base: dict[str, typing.Any], deltas: list[Delta]) -> dict[str, typing.Any]:
357357
output = copy.deepcopy(base)
358358
for op in deltas:
359-
if op.op == "deleted":
359+
if op.operation == "deleted":
360360
pop_by_json_path(output, op.path, prune_empty=True, remove_from_list=True)
361361
continue
362+
# we use set for both modify and add actions
362363
set_by_json_path(
363364
output,
364365
op.path,

tests/test_diff.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,14 @@ def _mk_expected_operation(**kwargs):
3737
(
3838
{"name": "Amin"},
3939
{"name": "Amin2"},
40-
[Delta(op="modified", path="$.name", old_value="Amin", new_value="Amin2")],
40+
[
41+
Delta(
42+
operation="modified",
43+
path="$.name",
44+
old_value="Amin",
45+
new_value="Amin2",
46+
)
47+
],
4148
)
4249
],
4350
)
@@ -76,7 +83,9 @@ def test_added_key_expected_op_and_patch_roundtrip():
7683

7784
# At least one 'added' operation to $.age (value semantics may differ: new_value vs value)
7885
assert any(
79-
o.op == "added" and o.path == "$.age" and _op_get(o, "new_value", "value") == 30
86+
o.operation == "added"
87+
and o.path == "$.age"
88+
and _op_get(o, "new_value", "value") == 30
8089
for o in ops
8190
)
8291

@@ -91,7 +100,7 @@ def test_deleted_key_expected_op_and_patch_roundtrip():
91100

92101
# At least one 'deleted' operation from $.name (old_value/value semantics)
93102
assert any(
94-
o.op == "deleted"
103+
o.operation == "deleted"
95104
and o.path == "$.name"
96105
and _op_get(o, "old_value", "value") == "Amin"
97106
for o in ops
@@ -151,7 +160,7 @@ def test_none_handling_as_value_vs_absence():
151160
old2 = {"y": None}
152161
new2: dict[str, typing.Any] = {}
153162
ops2 = diff(new=new2, old=old2)
154-
assert any(o.op == "deleted" and o.path == "$.y" for o in ops2)
163+
assert any(o.operation == "deleted" and o.path == "$.y" for o in ops2)
155164
assert patch(base=old2, deltas=ops2) == new2
156165

157166

@@ -176,7 +185,7 @@ def test_multiple_changes_in_one_structure():
176185
"$.user.role": "added",
177186
"$.settings.theme": "modified",
178187
}
179-
seen = {o.path: o.op for o in ops if o.path in expect_paths}
188+
seen = {o.path: o.operation for o in ops if o.path in expect_paths}
180189
for p, expected_op in expect_paths.items():
181190
assert seen.get(p) == expected_op, (
182191
f"Expected {expected_op} at {p}, got {seen.get(p)}"

0 commit comments

Comments
 (0)