File tree Expand file tree Collapse file tree 2 files changed +33
-8
lines changed Expand file tree Collapse file tree 2 files changed +33
-8
lines changed Original file line number Diff line number Diff line change @@ -106,15 +106,17 @@ def __bytes_repr__(self, cache: Cache) -> Iterator[bytes]:
106
106
def bytes_repr (obj : object , cache : Cache ) -> Iterator [bytes ]:
107
107
cls = obj .__class__
108
108
yield f"{ cls .__module__ } .{ cls .__name__ } :{{" .encode ()
109
- try :
110
- dct = obj .__dict__
111
- except AttributeError as e :
112
- # Attrs creates slots classes by default, so we add this here to handle those
113
- # cases
109
+ if attrs .has (type (obj )):
110
+ # Drop any attributes that aren't used in comparisons by default
111
+ dct = attrs .asdict (obj , recurse = False , filter = lambda a , _ : bool (a .eq )) # type: ignore
112
+ else :
114
113
try :
115
- dct = attrs .asdict (obj , recurse = False ) # type: ignore
116
- except attrs .exceptions .NotAnAttrsClassError :
117
- raise TypeError (f"Cannot hash { obj } as it is a slots class" ) from e
114
+ dct = obj .__dict__
115
+ except AttributeError as e :
116
+ try :
117
+ dct = {n : getattr (obj , n ) for n in obj .__slots__ } # type: ignore
118
+ except AttributeError :
119
+ raise e
118
120
yield from bytes_repr_mapping_contents (dct , cache )
119
121
yield b"}"
120
122
Original file line number Diff line number Diff line change @@ -135,6 +135,20 @@ def __init__(self, x):
135
135
assert re .match (rb".*\.MyClass:{str:1:x=.{16}}" , obj_repr )
136
136
137
137
138
+ def test_bytes_repr_slots_obj ():
139
+ class MyClass :
140
+ __slots__ = ("x" ,)
141
+
142
+ def __init__ (
143
+ self ,
144
+ x ,
145
+ ):
146
+ self .x = x
147
+
148
+ obj_repr = join_bytes_repr (MyClass (1 ))
149
+ assert re .match (rb".*\.MyClass:{str:1:x=.{16}}" , obj_repr )
150
+
151
+
138
152
def test_bytes_repr_attrs_slots ():
139
153
@attrs .define
140
154
class MyClass :
@@ -144,6 +158,15 @@ class MyClass:
144
158
assert re .match (rb".*\.MyClass:{str:1:x=.{16}}" , obj_repr )
145
159
146
160
161
+ def test_bytes_repr_attrs_no_slots ():
162
+ @attrs .define (slots = False )
163
+ class MyClass :
164
+ x : int
165
+
166
+ obj_repr = join_bytes_repr (MyClass (1 ))
167
+ assert re .match (rb".*\.MyClass:{str:1:x=.{16}}" , obj_repr )
168
+
169
+
147
170
def test_bytes_repr_type1 ():
148
171
obj_repr = join_bytes_repr (Path )
149
172
assert obj_repr == b"type:(pathlib.Path)"
You can’t perform that action at this time.
0 commit comments