Repository navigation
Fix DeepDiff on dict subclasses with non-standard iteration - #674
Open
tony1233200 wants to merge 1 commit into
Open
tony1233200 wants to merge 1 commit into
tony1233200 wants to merge 1 commit into
Conversation
Dict subclasses may override __iter__ to yield values instead of keys
(google.cloud.spanner JsonObject holding a JSON array does this), which
made _diff_dict crash building its key sets on unhashable values. Collect
keys via .keys() instead, which is identical for well-behaved dicts.
Dict subclasses exposing no keys at all but unequal content (JsonObject
holding a scalar) are now reported as changed instead of silently
returning {}.
Fixes qlustered#569
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.
Fix DeepDiff on dict subclasses with non-standard iteration
Fixes #569.
Problem
DeepDiffdid not detect differences insidegoogle.cloud.spanner_v1.data_types.JsonObject:Root cause
JsonObjectis adictsubclass, but for the JSON-array case it stores the elements in a separate_array_valuelist and overrides__iter__to yield the array values instead of keys:_diff_dict()collected keys withfor key in t1/for key in t2, so it tried to build a key set out of the (unhashable) array elements and crashed. For the scalar case (JsonObject(5))__iter__raisesTypeErroroutright, and for a scalar the object exposes no keys at all, so even a non-crashing path silently returned{}.Change
deepdiff/diff.py,_diff_dict()only:t1.keys()/t2.keys()instead of the dicts directly. For well-behaved dictsiter(d)anditer(d.keys())are identical, so this is a no-op for normal input and only changes behaviour for subclasses with a non-standard__iter__.JsonObject), the level is reported asvalues_changedinstead of silently returning{}. Raw keys (before private-key filtering) are used for the check, so a dict differing only in ignored private keys is still correctly reported as{}.Tests
tests/test_diff_text.py: two new tests using local stub dict subclasses that mimicJsonObject(no new test dependency ongoogle-cloud-spanner):test_dict_subclass_whose_iter_yields_values— array case: added/removed/changed/nested/equal.test_dict_subclass_with_no_keys_but_hidden_state— scalar case: changed vs equal, plus empty-dict sanity.JsonObject(installedgoogle-cloud-spannerin a scratch venv): before the fix the issue repro raisedTypeError: unhashable type: 'dict'; after the fix it returns{'dictionary_item_added': ['root[0]']}. Scalar case before:TypeError: 'int' object is not iterable; after:{'values_changed': {'root': {'new_value': 6, 'old_value': 5}}}. Plain-objectJsonObject, null, nested andignore_private_variablescases all behave correctly, with no false positives on empty/equal dicts.pytest tests/— 1346 passed, 45 skipped, 0 failed.flake8on the touched files reports the same 288 pre-existing findings as the clean tree — no new violations.Notes
JsonObject.__iter__arguably violates theMappingprotocol, butDeepDiffshould not crash on adictsubclass; this fix keeps the workaround on our side and is behaviour-preserving for normal dicts.