Skip to content

Commit

Permalink
KeyBuilder: allow hashing of classes w/ update_persistent_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasdiener committed Jan 31, 2024
1 parent 1c88a79 commit 1375efd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pytools/persistent_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def rec(self, key_hash, key):

digest = getattr(key, "_pytools_persistent_hash_digest", None)

if digest is None:
if digest is None and not isinstance(key, type):
try:
method = key.update_persistent_hash
except AttributeError:
Expand Down
22 changes: 22 additions & 0 deletions pytools/test/test_persistent_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,28 @@ def test_frozenorderedset_hashing():
assert keyb(FrozenOrderedSet([1, 2, 3])) == keyb(FrozenOrderedSet([3, 2, 1]))


def test_class_hashing():
keyb = KeyBuilder()

class WithUpdateMethod:
def update_persistent_hash(self, key_hash, key_builder):
# Only called for instances of this class, not for the class itself
key_builder.rec(key_hash, 42)

class TagClass(Tag):
# Inherits update_persistent_hash from 'Tag'
pass

@tag_dataclass
class TagClass2(Tag):
# Inherits update_persistent_hash from 'Tag'
pass

assert keyb(WithUpdateMethod) != keyb(WithUpdateMethod())
assert keyb(TagClass) != keyb(TagClass())
assert keyb(TagClass2) != keyb(TagClass2())


if __name__ == "__main__":
if len(sys.argv) > 1:
exec(sys.argv[1])
Expand Down

0 comments on commit 1375efd

Please sign in to comment.