Skip to content

Fall back to the built-in hash for hashable objects with unreadable slots - #675

Open
tony1233200 wants to merge 1 commit into
qlustered:devfrom
tony1233200:fix/560-hashable-opaque-objects
Open

tony1233200 wants to merge 1 commit into
qlustered:devfrom
tony1233200:fix/560-hashable-opaque-objects

Conversation

@tony1233200

Copy link
Copy Markdown

Fall back to the built-in hash for hashable objects with unreadable slots

Fixes #560.

Problem

DeepHash could not distinguish bson.ObjectId values:

from bson import ObjectId
from deepdiff import DeepHash

DeepHash(ObjectId('688a718ef3748592f129959c'))  # ...961aa767...
DeepHash(ObjectId('688a718ef3748592f129959d'))  # ...961aa767... (same!)

On the reported version this surfaced as Can not hash the following items: [ObjectId(...)]. On current dev the warning is gone but the result is worse: every ObjectId hashes to the same constant, so ignore_order comparisons silently treat different ids as equal. The issue's suggestion was to allow custom hash functions, or alternatively to try Python's built-in hash() before giving up — this PR implements the alternative.

Root cause

DeepHash._prep_obj() turns an object into a dict via __dict__ or __slots__. ObjectId has no __dict__ and declares __slots__ = ('__id',) without populating it, so _get_slots_dict() silently yields {} and the object hashes as the constant "obj{}".

Change

deepdiff/deephash.py, _prep_obj() only:

  • When the attribute representation comes back empty for an object that declares __slots__ (i.e. the representation is vacuous), and the object is hashable, fall back to the built-in hash() ("objhash:<hash>", still passed through the normal apply_hash step). Unhashable objects keep the previous empty representation; objects whose slot strategies raise AttributeError (e.g. the Bad test fixture whose __getattr__ raises) still end up unprocessed, exactly as before.

Tests

  • tests/test_hash.py: test_hashable_object_with_unreadable_slots (distinct opaque objects hash differently, same object stable within a run) and test_hashable_slots_object_hashes_by_value (equal objects share a hash, different ones do not, and set diffs distinguish them).
  • Verified against the real bson.ObjectId (pymongo installed in a scratch venv; no new test dependency): before the fix both ids hashed to 961aa767...; after the fix they hash differently, equal ids still share a hash, and DeepDiff({oid1}, {oid3}, ignore_order=True) correctly reports set_item_added/set_item_removed.
  • Existing Bad-object and slots tests still pass (test_bad_attribute, test_slots_with_no_initialized_attributes, test_slots_deepdiff_comparison).
  • Full suite: pytest tests/ — 1346 passed, 45 skipped, 0 failed.
  • Lint: flake8 on the touched files reports no new findings vs the clean tree.

Known limitation (not changed by this PR)

Bare ObjectId values still compare equal under default settings in plain DeepDiff, because the only readable attribute is the dunder slot __id, which the default ignore_private_variables=True filters out. With ignore_private_variables=False the difference is reported (root.__id values_changed), and list ignore_order diffs then report it too. That filtering is intended default behaviour for private attributes, so I left it alone; the hash path fixed here is what #560 asked about.

Notes

  • AI assistance: this change was prepared with AI assistance (Claude).

…lots

Objects that declare __slots__ but never populate them (e.g. bson
ObjectId, which declares '__id' without exposing it) produced an empty
attribute dict, so every such object hashed to the same constant and
ignore_order comparisons treated different values as equal. When the
attribute representation is vacuous and the object is hashable, use the
built-in hash instead.

Fixes qlustered#560
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant