File tree Expand file tree Collapse file tree 1 file changed +12
-3
lines changed Expand file tree Collapse file tree 1 file changed +12
-3
lines changed Original file line number Diff line number Diff line change 1616 Set ,
1717 _SpecialForm ,
1818)
19-
20- # import typing as ty
19+ import attrs .exceptions
2120
2221try :
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]:
103103def 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
You can’t perform that action at this time.
0 commit comments