Skip to content

Commit 4915f8a

Browse files
committed
added test for unhashable exceptions and touched up exception messages
1 parent d046182 commit 4915f8a

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

pydra/engine/specs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,8 +1000,8 @@ def get_value(
10001000
"types across multiple processes (see bytes_repr() "
10011001
'"singledispatch "function in pydra/utils/hash.py).'
10021002
"You may need to implement a specific `bytes_repr()` "
1003-
'"singledispatch overload"s or `__bytes_repr__()` '
1004-
"dunder methods to handle one or more types in "
1003+
"implementations (see pydra.utils.hash.register_serializer) or a "
1004+
"`__bytes_repr__()` dunder methods to handle one or more types in "
10051005
"your interface inputs."
10061006
)
10071007
_, split_depth = TypeParser.strip_splits(self.type)

pydra/engine/submitter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,9 @@ async def expand_workflow(self, wf, rerun=False):
229229
"types across multiple processes (see bytes_repr() "
230230
'"singledispatch "function in pydra/utils/hash.py).'
231231
"You may need to implement a specific `bytes_repr()` "
232-
'"singledispatch overload"s or `__bytes_repr__()` '
233-
"dunder methods to handle one or more types in "
234-
"your interface inputs."
232+
"implementation (see pydra.utils.hash.register_serializer) "
233+
"s or `__bytes_repr__()` dunder methods to handle one "
234+
"or more types in your interface inputs."
235235
)
236236
raise RuntimeError(msg)
237237
for task in tasks:

pydra/utils/hash.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,16 @@ def hash_object(
220220
try:
221221
return hash_single(obj, cache)
222222
except Exception as e:
223-
add_exc_note(e, f"Therefore cannot hash object {obj!r}")
223+
tp = type(obj)
224+
add_exc_note(
225+
e,
226+
(
227+
f"and therefore cannot hash `{obj!r}` of type "
228+
f"`{tp.__module__}.{tp.__name__}`. Consider implementing a "
229+
"specific `bytes_repr()`(see pydra.utils.hash.register_serializer) "
230+
"or a `__bytes_repr__()` dunder methods for this type"
231+
),
232+
)
224233
raise e
225234

226235

pydra/utils/tests/test_hash.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,28 @@ def test_persistent_hash_cache_not_dir(text_file):
385385
"""
386386
with pytest.raises(ValueError, match="is not a directory"):
387387
PersistentCache(text_file.fspath)
388+
389+
390+
def test_unhashable():
391+
"""
392+
Test that an error is raised if an unhashable object is provided
393+
"""
394+
395+
class A:
396+
397+
def __bytes_repr__(self, cache: Cache) -> ty.Generator[bytes, None, None]:
398+
raise TypeError("unhashable")
399+
400+
def __repr__(self):
401+
return "A()"
402+
403+
# hash_object(A())
404+
405+
with pytest.raises(
406+
TypeError,
407+
match=(
408+
"unhashable\nand therefore cannot hash `A\(\)` of type "
409+
"`pydra.utils.tests.test_hash.A`"
410+
),
411+
):
412+
hash_object(A())

0 commit comments

Comments
 (0)