Skip to content

Commit

Permalink
added test for unhashable exceptions and touched up exception messages
Browse files Browse the repository at this point in the history
  • Loading branch information
tclose committed Mar 18, 2024
1 parent d046182 commit 4915f8a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pydra/engine/specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1000,8 +1000,8 @@ def get_value(
"types across multiple processes (see bytes_repr() "
'"singledispatch "function in pydra/utils/hash.py).'
"You may need to implement a specific `bytes_repr()` "
'"singledispatch overload"s or `__bytes_repr__()` '
"dunder methods to handle one or more types in "
"implementations (see pydra.utils.hash.register_serializer) or a "
"`__bytes_repr__()` dunder methods to handle one or more types in "
"your interface inputs."
)
_, split_depth = TypeParser.strip_splits(self.type)
Expand Down
6 changes: 3 additions & 3 deletions pydra/engine/submitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ async def expand_workflow(self, wf, rerun=False):
"types across multiple processes (see bytes_repr() "
'"singledispatch "function in pydra/utils/hash.py).'
"You may need to implement a specific `bytes_repr()` "
'"singledispatch overload"s or `__bytes_repr__()` '
"dunder methods to handle one or more types in "
"your interface inputs."
"implementation (see pydra.utils.hash.register_serializer) "
"s or `__bytes_repr__()` dunder methods to handle one "
"or more types in your interface inputs."
)
raise RuntimeError(msg)
for task in tasks:
Expand Down
11 changes: 10 additions & 1 deletion pydra/utils/hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,16 @@ def hash_object(
try:
return hash_single(obj, cache)
except Exception as e:
add_exc_note(e, f"Therefore cannot hash object {obj!r}")
tp = type(obj)
add_exc_note(
e,
(
f"and therefore cannot hash `{obj!r}` of type "
f"`{tp.__module__}.{tp.__name__}`. Consider implementing a "
"specific `bytes_repr()`(see pydra.utils.hash.register_serializer) "
"or a `__bytes_repr__()` dunder methods for this type"

Check warning on line 230 in pydra/utils/hash.py

View check run for this annotation

Codecov / codecov/patch

pydra/utils/hash.py#L230

Added line #L230 was not covered by tests
),
)
raise e


Expand Down
25 changes: 25 additions & 0 deletions pydra/utils/tests/test_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,28 @@ def test_persistent_hash_cache_not_dir(text_file):
"""
with pytest.raises(ValueError, match="is not a directory"):
PersistentCache(text_file.fspath)


def test_unhashable():
"""
Test that an error is raised if an unhashable object is provided
"""

class A:

def __bytes_repr__(self, cache: Cache) -> ty.Generator[bytes, None, None]:
raise TypeError("unhashable")

def __repr__(self):
return "A()"

# hash_object(A())

with pytest.raises(
TypeError,
match=(
"unhashable\nand therefore cannot hash `A\(\)` of type "
"`pydra.utils.tests.test_hash.A`"
),
):
hash_object(A())

0 comments on commit 4915f8a

Please sign in to comment.