Skip to content

Commit 03c1286

Browse files
committed
added a couple of hashing tests
1 parent 8ec38dd commit 03c1286

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

pydra/utils/hash.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ def hash_object(obj: object) -> Hash:
7272
try:
7373
return hash_single(obj, Cache({}))
7474
except Exception as e:
75-
hash_single(obj, Cache({}))
7675
raise UnhashableError(f"Cannot hash object {obj!r}") from e
7776

7877

@@ -227,9 +226,8 @@ def bytes_repr_dict(obj: dict, cache: Cache) -> Iterator[bytes]:
227226

228227
@register_serializer(_SpecialForm)
229228
@register_serializer(type)
230-
def bytes_repr_type(obj: type, cache: Cache) -> Iterator[bytes]:
231-
cls = type(obj)
232-
yield f"{cls.__module__}.{cls.__name__}".encode()
229+
def bytes_repr_type(klass: type, cache: Cache) -> Iterator[bytes]:
230+
yield f"type:({klass.__module__}.{klass.__name__})".encode()
233231

234232

235233
@register_serializer(list)

pydra/utils/tests/test_hash.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from hashlib import blake2b
33
from pathlib import Path
44

5+
import attrs
56
import pytest
67

78
from ..hash import Cache, UnhashableError, bytes_repr, hash_object, register_serializer
@@ -133,6 +134,20 @@ def __init__(self, x):
133134
assert re.match(rb".*\.MyClass:{str:1:x=.{16}}", obj_repr)
134135

135136

137+
def test_bytes_repr_attrs_slots():
138+
@attrs.define
139+
class MyClass:
140+
x: int
141+
142+
obj_repr = join_bytes_repr(MyClass(1))
143+
assert re.match(rb".*\.MyClass:{str:1:x=.{16}}", obj_repr)
144+
145+
146+
def test_bytes_repr_type():
147+
obj_repr = join_bytes_repr(Path)
148+
assert obj_repr == b"type:(pathlib.Path)"
149+
150+
136151
def test_recursive_object():
137152
a = []
138153
b = [a]

0 commit comments

Comments
 (0)