Skip to content

Commit 4adac59

Browse files
committed
added handling for hashing attrs slots classes
1 parent ef3f7e0 commit 4adac59

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

pydra/utils/hash.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
Set,
1717
_SpecialForm,
1818
)
19-
20-
# import typing as ty
19+
import attrs.exceptions
2120

2221
try:
2322
from typing import Protocol
@@ -73,6 +72,7 @@ def hash_object(obj: object) -> Hash:
7372
try:
7473
return hash_single(obj, Cache({}))
7574
except Exception as e:
75+
hash_single(obj, Cache({}))
7676
raise UnhashableError(f"Cannot hash object {obj!r}") from e
7777

7878

@@ -103,7 +103,16 @@ def __bytes_repr__(self, cache: Cache) -> Iterator[bytes]:
103103
def bytes_repr(obj: object, cache: Cache) -> Iterator[bytes]:
104104
cls = obj.__class__
105105
yield f"{cls.__module__}.{cls.__name__}:{{".encode()
106-
yield from bytes_repr_mapping_contents(obj.__dict__, cache)
106+
try:
107+
dct = obj.__dict__
108+
except AttributeError as e:
109+
# Attrs creates slots classes by default, so we add this here to handle those
110+
# cases
111+
try:
112+
dct = attrs.asdict(obj, recurse=False) # type: ignore
113+
except attrs.exceptions.NotAnAttrsClassError:
114+
raise TypeError(f"Cannot hash {obj} as it is a slots class") from e
115+
yield from bytes_repr_mapping_contents(dct, cache)
107116
yield b"}"
108117

109118

0 commit comments

Comments
 (0)