@@ -15,6 +15,8 @@ def _compute_patches(
1515 old_value : Any ,
1616 new_value : Any ,
1717 path : tuple [KeyPart , ...],
18+ * ,
19+ remove_extra : bool = True ,
1820) -> list [_core .Patch ]:
1921 """Compute minimal patches to transform old_value into new_value at path."""
2022 if old_value == new_value :
@@ -24,7 +26,7 @@ def _compute_patches(
2426 new_is_dict = isinstance (new_value , dict )
2527
2628 if old_is_dict and new_is_dict :
27- return _diff_mappings (old_value , new_value , path )
29+ return _diff_mappings (old_value , new_value , path , remove_extra = remove_extra )
2830
2931 old_is_list = isinstance (old_value , list )
3032 new_is_list = isinstance (new_value , list )
@@ -42,14 +44,18 @@ def _diff_mappings(
4244 old : dict [str , Any ],
4345 new : dict [str , Any ],
4446 path : tuple [KeyPart , ...],
47+ * ,
48+ remove_extra : bool = True ,
4549) -> list [_core .Patch ]:
4650 """Diff two mappings and return patches."""
4751 patches : list [_core .Patch ] = []
4852
4953 # Keys in new that exist in old — recurse
5054 for key in new :
5155 if key in old :
52- child_patches = _compute_patches (old [key ], new [key ], (* path , key ))
56+ child_patches = _compute_patches (
57+ old [key ], new [key ], (* path , key ), remove_extra = remove_extra
58+ )
5359 patches .extend (child_patches )
5460 else :
5561 # New key — add
@@ -58,11 +64,12 @@ def _diff_mappings(
5864 patches .append (_core .Patch (route = route , operation = op ))
5965
6066 # Keys in old not in new — remove
61- for key in old :
62- if key not in new :
63- route = _core .Route ([* path , key ])
64- op = _core .Op .remove ()
65- patches .append (_core .Patch (route = route , operation = op ))
67+ if remove_extra :
68+ for key in old :
69+ if key not in new :
70+ route = _core .Route ([* path , key ])
71+ op = _core .Op .remove ()
72+ patches .append (_core .Patch (route = route , operation = op ))
6673
6774 return patches
6875
0 commit comments