/
test_setattr.py
386 lines (276 loc) · 9.96 KB
/
test_setattr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
from __future__ import absolute_import, division, print_function
import pickle
import pytest
import attr
from attr import setters
from attr._compat import PY2
from attr.exceptions import FrozenAttributeError
from attr.validators import instance_of, matches_re
@attr.s(frozen=True)
class Frozen(object):
x = attr.ib()
@attr.s
class WithOnSetAttrHook(object):
x = attr.ib(on_setattr=lambda *args: None)
class TestSetAttr(object):
def test_change(self):
"""
The return value of a hook overwrites the value. But they are not run
on __init__.
"""
def hook(*a, **kw):
return "hooked!"
@attr.s
class Hooked(object):
x = attr.ib(on_setattr=hook)
y = attr.ib()
h = Hooked("x", "y")
assert "x" == h.x
assert "y" == h.y
h.x = "xxx"
h.y = "yyy"
assert "yyy" == h.y
assert "hooked!" == h.x
def test_frozen_attribute(self):
"""
Frozen attributes raise FrozenAttributeError, others are not affected.
"""
@attr.s
class PartiallyFrozen(object):
x = attr.ib(on_setattr=setters.frozen)
y = attr.ib()
pf = PartiallyFrozen("x", "y")
pf.y = "yyy"
assert "yyy" == pf.y
with pytest.raises(FrozenAttributeError):
pf.x = "xxx"
assert "x" == pf.x
@pytest.mark.parametrize(
"on_setattr",
[setters.validate, [setters.validate], setters.pipe(setters.validate)],
)
def test_validator(self, on_setattr):
"""
Validators are run and they don't alter the value.
"""
@attr.s(on_setattr=on_setattr)
class ValidatedAttribute(object):
x = attr.ib()
y = attr.ib(validator=[instance_of(str), matches_re("foo.*qux")])
va = ValidatedAttribute(42, "foobarqux")
with pytest.raises(TypeError) as ei:
va.y = 42
assert "foobarqux" == va.y
assert ei.value.args[0].startswith("'y' must be <")
with pytest.raises(ValueError) as ei:
va.y = "quxbarfoo"
assert ei.value.args[0].startswith("'y' must match regex '")
assert "foobarqux" == va.y
va.y = "foobazqux"
assert "foobazqux" == va.y
def test_pipe(self):
"""
Multiple hooks are possible, in that case the last return value is
used. They can be supplied using the pipe functions or by passing a
list to on_setattr.
"""
s = [setters.convert, lambda _, __, nv: nv + 1]
@attr.s
class Piped(object):
x1 = attr.ib(converter=int, on_setattr=setters.pipe(*s))
x2 = attr.ib(converter=int, on_setattr=s)
p = Piped("41", "22")
assert 41 == p.x1
assert 22 == p.x2
p.x1 = "41"
p.x2 = "22"
assert 42 == p.x1
assert 23 == p.x2
def test_make_class(self):
"""
on_setattr of make_class gets forwarded.
"""
C = attr.make_class("C", {"x": attr.ib()}, on_setattr=setters.frozen)
c = C(1)
with pytest.raises(FrozenAttributeError):
c.x = 2
def test_no_validator_no_converter(self):
"""
validate and convert tolerate missing validators and converters.
"""
@attr.s(on_setattr=[setters.convert, setters.validate])
class C(object):
x = attr.ib()
c = C(1)
c.x = 2
assert 2 == c.x
def test_validate_respects_run_validators_config(self):
"""
If run validators is off, validate doesn't run them.
"""
@attr.s(on_setattr=setters.validate)
class C(object):
x = attr.ib(validator=attr.validators.instance_of(int))
c = C(1)
attr.set_run_validators(False)
c.x = "1"
assert "1" == c.x
attr.set_run_validators(True)
with pytest.raises(TypeError) as ei:
c.x = "1"
assert ei.value.args[0].startswith("'x' must be <")
def test_frozen_on_setattr_class_is_caught(self):
"""
@attr.s(on_setattr=X, frozen=True) raises an ValueError.
"""
with pytest.raises(ValueError) as ei:
@attr.s(frozen=True, on_setattr=setters.validate)
class C(object):
x = attr.ib()
assert "Frozen classes can't use on_setattr." == ei.value.args[0]
def test_frozen_on_setattr_attribute_is_caught(self):
"""
attr.ib(on_setattr=X) on a frozen class raises an ValueError.
"""
with pytest.raises(ValueError) as ei:
@attr.s(frozen=True)
class C(object):
x = attr.ib(on_setattr=setters.validate)
assert "Frozen classes can't use on_setattr." == ei.value.args[0]
@pytest.mark.parametrize("slots", [True, False])
def test_setattr_reset_if_no_custom_setattr(self, slots):
"""
If a class with an active setattr is subclassed and no new setattr
is generated, the __setattr__ is set to object.__setattr__.
We do the double test because of Python 2.
"""
def boom(*args):
pytest.fail("Must not be called.")
@attr.s
class Hooked(object):
x = attr.ib(on_setattr=boom)
@attr.s(slots=slots)
class NoHook(WithOnSetAttrHook):
x = attr.ib()
if not PY2:
assert NoHook.__setattr__ == object.__setattr__
assert 1 == NoHook(1).x
@pytest.mark.parametrize("slots", [True, False])
def test_setattr_inherited_do_not_reset(self, slots):
"""
If we inherit a __setattr__ that has been written by the user, we must
not reset it unless necessary.
"""
class A(object):
"""
Not an attrs class on purpose to prevent accidental resets that
would render the asserts meaningless.
"""
def __setattr__(self, *args):
pass
@attr.s(slots=slots)
class B(A):
pass
assert B.__setattr__ == A.__setattr__
@attr.s(slots=slots)
class C(B):
pass
assert C.__setattr__ == A.__setattr__
@pytest.mark.parametrize("slots", [True, False])
def test_pickling_retains_attrs_own(self, slots):
"""
Pickling/Unpickling does not lose ownership information about
__setattr__.
"""
i = WithOnSetAttrHook(1)
assert True is i.__attrs_own_setattr__
i2 = pickle.loads(pickle.dumps(i))
assert True is i2.__attrs_own_setattr__
WOSAH = pickle.loads(pickle.dumps(WithOnSetAttrHook))
assert True is WOSAH.__attrs_own_setattr__
def test_slotted_class_can_have_custom_setattr(self):
"""
A slotted class can define a custom setattr and it doesn't get
overwritten.
Regression test for #680.
"""
@attr.s(slots=True)
class A(object):
def __setattr__(self, key, value):
raise SystemError
with pytest.raises(SystemError):
A().x = 1
@pytest.mark.xfail
def test_slotted_confused(self):
"""
If we have a in-between non-attrs class, setattr reset detection
should still work, but currently doesn't.
It works with dict classes because we can look the finished class and
patch it. With slotted classes we have to deduce it ourselves.
"""
@attr.s(slots=True)
class A(object):
x = attr.ib(on_setattr=setters.frozen)
class B(A):
pass
@attr.s(slots=True)
class C(B):
x = attr.ib()
C(1).x = 2
@pytest.mark.skipif(PY2, reason="Python 3-only.")
class TestSetAttrNoPy2(object):
"""
__setattr__ tests for Py3+ to avoid the skip repetition.
"""
@pytest.mark.parametrize("slots", [True, False])
def test_setattr_auto_detect_if_no_custom_setattr(self, slots):
"""
It's possible to remove the on_setattr hook from an attribute and
therefore write a custom __setattr__.
"""
assert 1 == WithOnSetAttrHook(1).x
@attr.s(auto_detect=True, slots=slots)
class RemoveNeedForOurSetAttr(WithOnSetAttrHook):
x = attr.ib()
def __setattr__(self, name, val):
object.__setattr__(self, name, val * 2)
i = RemoveNeedForOurSetAttr(1)
assert 2 == i.x
@pytest.mark.parametrize("slots", [True, False])
def test_setattr_restore_respects_auto_detect(self, slots):
"""
If __setattr__ should be restored but the user supplied its own and
set auto_detect, leave is alone.
"""
@attr.s(auto_detect=True, slots=slots)
class CustomSetAttr:
def __setattr__(self, _, __):
pass
assert CustomSetAttr.__setattr__ != object.__setattr__
@pytest.mark.parametrize("slots", [True, False])
def test_setattr_auto_detect_frozen(self, slots):
"""
frozen=True together with a detected custom __setattr__ are rejected.
"""
with pytest.raises(
ValueError, match="Can't freeze a class with a custom __setattr__."
):
@attr.s(auto_detect=True, slots=slots, frozen=True)
class CustomSetAttr(Frozen):
def __setattr__(self, _, __):
pass
@pytest.mark.parametrize("slots", [True, False])
def test_setattr_auto_detect_on_setattr(self, slots):
"""
on_setattr attributes together with a detected custom __setattr__ are
rejected.
"""
with pytest.raises(
ValueError,
match="Can't combine custom __setattr__ with on_setattr hooks.",
):
@attr.s(auto_detect=True, slots=slots)
class HookAndCustomSetAttr(object):
x = attr.ib(on_setattr=lambda *args: None)
def __setattr__(self, _, __):
pass