Repository navigation
Deepcopy new values when applying a Delta - #678
Open
tony1233200 wants to merge 1 commit into
Open
tony1233200 wants to merge 1 commit into
tony1233200 wants to merge 1 commit into
Conversation
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.
Fixes #655.
Applying the same
Deltato separate objects with the defaultmutate=Falseput the same mutable value into every result: editingone result changed the others and corrupted the values used by later
applications of that Delta.
Delta.__add__already deepcopies the input object, but the valuestaken from the stored diff were inserted by reference:
_do_item_addedand_do_values_or_type_changedboth passnew_valuestraight fromself.diffinto_set_new_value, whichstored it as-is.
Fix (in
deepdiff/delta.py,deepcopyis already imported):_set_new_value, deepcopynew_valuebefore storing it. Thiscovers
dictionary_item_added,iterable_item_added,attribute_added,values_changedandtype_changesin one place._do_iterable_opcodes, deepcopyopcode.new_valueswhenextending
transformedforreplace/inserttags (theequaltag copies from the already-deepcopied root and needs no change).
Verification:
{'tags': []}for both thesecond result and a third application of the Delta (before the fix
both printed
{'tags': ['left only']}).tests/test_delta.py— the Applying a Delta shares mutable values between independent results #655 repro and atype-change case whose new value is mutable — fail on unpatched
code and pass with the fix.
python -m pytest tests/test_delta.py: 130 passed; the one failure(
test_delta_repr) fails identically on the unpatched tree, so itis pre-existing and unrelated.
Out of scope, noted for follow-up:
_do_set_or_frozenset_itembuildsa new set but its members are still the diff's objects.