Skip to content

Commit d046182

Browse files
committed
add note instead of catching and raising unhashable exception
1 parent 811dc45 commit d046182

File tree

4 files changed

+37
-18
lines changed

4 files changed

+37
-18
lines changed

pydra/utils/__init__.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1 @@
1-
from pathlib import Path
2-
import platformdirs
3-
from pydra._version import __version__
4-
5-
user_cache_dir = Path(
6-
platformdirs.user_cache_dir(
7-
appname="pydra",
8-
appauthor="nipype",
9-
version=__version__,
10-
)
11-
)
1+
from .misc import user_cache_dir, add_exc_note # noqa: F401

pydra/utils/hash.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from filelock import SoftFileLock
2020
import attrs.exceptions
2121
from fileformats.core import FileSet
22-
from . import user_cache_dir
22+
from . import user_cache_dir, add_exc_note
2323

2424
logger = logging.getLogger("pydra")
2525

@@ -197,10 +197,6 @@ def __contains__(self, object_id):
197197
return object_id in self._hashes
198198

199199

200-
class UnhashableError(ValueError):
201-
"""Error for objects that cannot be hashed"""
202-
203-
204200
def hash_function(obj, **kwargs):
205201
"""Generate hash of object."""
206202
return hash_object(obj, **kwargs).hex()
@@ -224,7 +220,8 @@ def hash_object(
224220
try:
225221
return hash_single(obj, cache)
226222
except Exception as e:
227-
raise UnhashableError(f"Cannot hash object {obj!r} due to '{e}'") from e
223+
add_exc_note(e, f"Therefore cannot hash object {obj!r}")
224+
raise e
228225

229226

230227
def hash_single(obj: object, cache: Cache) -> Hash:

pydra/utils/misc.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from pathlib import Path
2+
import platformdirs
3+
from pydra._version import __version__
4+
5+
user_cache_dir = Path(
6+
platformdirs.user_cache_dir(
7+
appname="pydra",
8+
appauthor="nipype",
9+
version=__version__,
10+
)
11+
)
12+
13+
14+
def add_exc_note(e: Exception, note: str) -> Exception:
15+
"""Adds a note to an exception in a Python <3.11 compatible way
16+
17+
Parameters
18+
----------
19+
e : Exception
20+
the exception to add the note to
21+
note : str
22+
the note to add
23+
24+
Returns
25+
-------
26+
Exception
27+
returns the exception again
28+
"""
29+
if hasattr(e, "add_note"):
30+
e.add_note(note)
31+
else:
32+
e.args = (e.args[0] + "\n" + note,)
33+
return e

pydra/utils/tests/test_hash.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from fileformats.text import TextFile
1212
from ..hash import (
1313
Cache,
14-
UnhashableError,
1514
bytes_repr,
1615
hash_object,
1716
register_serializer,

0 commit comments

Comments
 (0)