-
-
Notifications
You must be signed in to change notification settings - Fork 359
/
test_slots.py
772 lines (563 loc) · 18.1 KB
/
test_slots.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
# SPDX-License-Identifier: MIT
"""
Unit tests for slots-related functionality.
"""
import pickle
import sys
import types
import weakref
from unittest import mock
import pytest
import attr
from attr._compat import PYPY, just_warn, make_set_closure_cell
# Pympler doesn't work on PyPy.
try:
from pympler.asizeof import asizeof
has_pympler = True
except BaseException: # Won't be an import error.
has_pympler = False
@attr.s
class C1:
x = attr.ib(validator=attr.validators.instance_of(int))
y = attr.ib()
def method(self):
return self.x
@classmethod
def classmethod(cls):
return "clsmethod"
@staticmethod
def staticmethod():
return "staticmethod"
def my_class(self):
return __class__
def my_super(self):
"""Just to test out the no-arg super."""
return super().__repr__()
@attr.s(slots=True, hash=True)
class C1Slots:
x = attr.ib(validator=attr.validators.instance_of(int))
y = attr.ib()
def method(self):
return self.x
@classmethod
def classmethod(cls):
return "clsmethod"
@staticmethod
def staticmethod():
return "staticmethod"
def my_class(self):
return __class__
def my_super(self):
"""Just to test out the no-arg super."""
return super().__repr__()
def test_slots_being_used():
"""
The class is really using __slots__.
"""
non_slot_instance = C1(x=1, y="test")
slot_instance = C1Slots(x=1, y="test")
assert "__dict__" not in dir(slot_instance)
assert "__slots__" in dir(slot_instance)
assert "__dict__" in dir(non_slot_instance)
assert "__slots__" not in dir(non_slot_instance)
assert {"__weakref__", "x", "y"} == set(slot_instance.__slots__)
if has_pympler:
assert asizeof(slot_instance) < asizeof(non_slot_instance)
non_slot_instance.t = "test"
with pytest.raises(AttributeError):
slot_instance.t = "test"
assert 1 == non_slot_instance.method()
assert 1 == slot_instance.method()
assert attr.fields(C1Slots) == attr.fields(C1)
assert attr.asdict(slot_instance) == attr.asdict(non_slot_instance)
def test_basic_attr_funcs():
"""
Comparison, `__eq__`, `__hash__`, `__repr__`, `attrs.asdict` work.
"""
a = C1Slots(x=1, y=2)
b = C1Slots(x=1, y=3)
a_ = C1Slots(x=1, y=2)
# Comparison.
assert b > a
assert a_ == a
# Hashing.
hash(b) # Just to assert it doesn't raise.
# Repr.
assert "C1Slots(x=1, y=2)" == repr(a)
assert {"x": 1, "y": 2} == attr.asdict(a)
def test_inheritance_from_nonslots():
"""
Inheritance from a non-slotted class works.
Note that a slotted class inheriting from an ordinary class loses most of
the benefits of slotted classes, but it should still work.
"""
@attr.s(slots=True, hash=True)
class C2Slots(C1):
z = attr.ib()
c2 = C2Slots(x=1, y=2, z="test")
assert 1 == c2.x
assert 2 == c2.y
assert "test" == c2.z
c2.t = "test" # This will work, using the base class.
assert "test" == c2.t
assert 1 == c2.method()
assert "clsmethod" == c2.classmethod()
assert "staticmethod" == c2.staticmethod()
assert {"z"} == set(C2Slots.__slots__)
c3 = C2Slots(x=1, y=3, z="test")
assert c3 > c2
c2_ = C2Slots(x=1, y=2, z="test")
assert c2 == c2_
assert "C2Slots(x=1, y=2, z='test')" == repr(c2)
hash(c2) # Just to assert it doesn't raise.
assert {"x": 1, "y": 2, "z": "test"} == attr.asdict(c2)
def test_nonslots_these():
"""
Enhancing a dict class using 'these' works.
This will actually *replace* the class with another one, using slots.
"""
class SimpleOrdinaryClass:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def method(self):
return self.x
@classmethod
def classmethod(cls):
return "clsmethod"
@staticmethod
def staticmethod():
return "staticmethod"
C2Slots = attr.s(
these={"x": attr.ib(), "y": attr.ib(), "z": attr.ib()},
init=False,
slots=True,
hash=True,
)(SimpleOrdinaryClass)
c2 = C2Slots(x=1, y=2, z="test")
assert 1 == c2.x
assert 2 == c2.y
assert "test" == c2.z
with pytest.raises(AttributeError):
c2.t = "test" # We have slots now.
assert 1 == c2.method()
assert "clsmethod" == c2.classmethod()
assert "staticmethod" == c2.staticmethod()
assert {"__weakref__", "x", "y", "z"} == set(C2Slots.__slots__)
c3 = C2Slots(x=1, y=3, z="test")
assert c3 > c2
c2_ = C2Slots(x=1, y=2, z="test")
assert c2 == c2_
assert "SimpleOrdinaryClass(x=1, y=2, z='test')" == repr(c2)
hash(c2) # Just to assert it doesn't raise.
assert {"x": 1, "y": 2, "z": "test"} == attr.asdict(c2)
def test_inheritance_from_slots():
"""
Inheriting from an attrs slotted class works.
"""
@attr.s(slots=True, hash=True)
class C2Slots(C1Slots):
z = attr.ib()
@attr.s(slots=True, hash=True)
class C2(C1):
z = attr.ib()
c2 = C2Slots(x=1, y=2, z="test")
assert 1 == c2.x
assert 2 == c2.y
assert "test" == c2.z
assert {"z"} == set(C2Slots.__slots__)
assert 1 == c2.method()
assert "clsmethod" == c2.classmethod()
assert "staticmethod" == c2.staticmethod()
with pytest.raises(AttributeError):
c2.t = "test"
non_slot_instance = C2(x=1, y=2, z="test")
if has_pympler:
assert asizeof(c2) < asizeof(non_slot_instance)
c3 = C2Slots(x=1, y=3, z="test")
assert c3 > c2
c2_ = C2Slots(x=1, y=2, z="test")
assert c2 == c2_
assert "C2Slots(x=1, y=2, z='test')" == repr(c2)
hash(c2) # Just to assert it doesn't raise.
assert {"x": 1, "y": 2, "z": "test"} == attr.asdict(c2)
def test_inheritance_from_slots_with_attribute_override():
"""
Inheriting from a slotted class doesn't re-create existing slots
"""
class HasXSlot:
__slots__ = ("x",)
@attr.s(slots=True, hash=True)
class C2Slots(C1Slots):
# y re-defined here but it shouldn't get a slot
y = attr.ib()
z = attr.ib()
@attr.s(slots=True, hash=True)
class NonAttrsChild(HasXSlot):
# Parent class has slot for "x" already, so we skip it
x = attr.ib()
y = attr.ib()
z = attr.ib()
c2 = C2Slots(1, 2, "test")
assert 1 == c2.x
assert 2 == c2.y
assert "test" == c2.z
assert {"z"} == set(C2Slots.__slots__)
na = NonAttrsChild(1, 2, "test")
assert 1 == na.x
assert 2 == na.y
assert "test" == na.z
assert {"__weakref__", "y", "z"} == set(NonAttrsChild.__slots__)
def test_inherited_slot_reuses_slot_descriptor():
"""
We reuse slot descriptor for an attr.ib defined in a slotted attr.s
"""
class HasXSlot:
__slots__ = ("x",)
class OverridesX(HasXSlot):
@property
def x(self):
return None
@attr.s(slots=True)
class Child(OverridesX):
x = attr.ib()
assert Child.x is not OverridesX.x
assert Child.x is HasXSlot.x
c = Child(1)
assert 1 == c.x
assert set() == set(Child.__slots__)
ox = OverridesX()
assert ox.x is None
def test_bare_inheritance_from_slots():
"""
Inheriting from a bare attrs slotted class works.
"""
@attr.s(
init=False, eq=False, order=False, hash=False, repr=False, slots=True
)
class C1BareSlots:
x = attr.ib(validator=attr.validators.instance_of(int))
y = attr.ib()
def method(self):
return self.x
@classmethod
def classmethod(cls):
return "clsmethod"
@staticmethod
def staticmethod():
return "staticmethod"
@attr.s(init=False, eq=False, order=False, hash=False, repr=False)
class C1Bare:
x = attr.ib(validator=attr.validators.instance_of(int))
y = attr.ib()
def method(self):
return self.x
@classmethod
def classmethod(cls):
return "clsmethod"
@staticmethod
def staticmethod():
return "staticmethod"
@attr.s(slots=True, hash=True)
class C2Slots(C1BareSlots):
z = attr.ib()
@attr.s(slots=True, hash=True)
class C2(C1Bare):
z = attr.ib()
c2 = C2Slots(x=1, y=2, z="test")
assert 1 == c2.x
assert 2 == c2.y
assert "test" == c2.z
assert 1 == c2.method()
assert "clsmethod" == c2.classmethod()
assert "staticmethod" == c2.staticmethod()
with pytest.raises(AttributeError):
c2.t = "test"
non_slot_instance = C2(x=1, y=2, z="test")
if has_pympler:
assert asizeof(c2) < asizeof(non_slot_instance)
c3 = C2Slots(x=1, y=3, z="test")
assert c3 > c2
c2_ = C2Slots(x=1, y=2, z="test")
assert c2 == c2_
assert "C2Slots(x=1, y=2, z='test')" == repr(c2)
hash(c2) # Just to assert it doesn't raise.
assert {"x": 1, "y": 2, "z": "test"} == attr.asdict(c2)
class TestClosureCellRewriting:
def test_closure_cell_rewriting(self):
"""
Slotted classes support proper closure cell rewriting.
This affects features like `__class__` and the no-arg super().
"""
non_slot_instance = C1(x=1, y="test")
slot_instance = C1Slots(x=1, y="test")
assert non_slot_instance.my_class() is C1
assert slot_instance.my_class() is C1Slots
# Just assert they return something, and not an exception.
assert non_slot_instance.my_super()
assert slot_instance.my_super()
def test_inheritance(self):
"""
Slotted classes support proper closure cell rewriting when inheriting.
This affects features like `__class__` and the no-arg super().
"""
@attr.s
class C2(C1):
def my_subclass(self):
return __class__
@attr.s
class C2Slots(C1Slots):
def my_subclass(self):
return __class__
non_slot_instance = C2(x=1, y="test")
slot_instance = C2Slots(x=1, y="test")
assert non_slot_instance.my_class() is C1
assert slot_instance.my_class() is C1Slots
# Just assert they return something, and not an exception.
assert non_slot_instance.my_super()
assert slot_instance.my_super()
assert non_slot_instance.my_subclass() is C2
assert slot_instance.my_subclass() is C2Slots
def test_cls_static(self, slots):
"""
Slotted classes support proper closure cell rewriting for class- and
static methods.
"""
# Python can reuse closure cells, so we create new classes just for
# this test.
@attr.s(slots=slots)
class C:
@classmethod
def clsmethod(cls):
return __class__
assert C.clsmethod() is C
@attr.s(slots=slots)
class D:
@staticmethod
def statmethod():
return __class__
assert D.statmethod() is D
@pytest.mark.skipif(PYPY, reason="set_closure_cell always works on PyPy")
@pytest.mark.skipif(
sys.version_info >= (3, 8),
reason="can't break CodeType.replace() via monkeypatch",
)
def test_code_hack_failure(self, monkeypatch):
"""
Keeps working if function/code object introspection doesn't work
on this (nonstandard) interpreter.
A warning is emitted that points to the actual code.
"""
# This is a pretty good approximation of the behavior of
# the actual types.CodeType on Brython.
monkeypatch.setattr(types, "CodeType", lambda: None)
func = make_set_closure_cell()
with pytest.warns(RuntimeWarning) as wr:
func()
w = wr.pop()
assert __file__ == w.filename
assert (
"Running interpreter doesn't sufficiently support code object "
"introspection. Some features like bare super() or accessing "
"__class__ will not work with slotted classes.",
) == w.message.args
assert just_warn is func
@pytest.mark.skipif(PYPY, reason="__slots__ only block weakref on CPython")
def test_not_weakrefable():
"""
Instance is not weak-referenceable when `weakref_slot=False` in CPython.
"""
@attr.s(slots=True, weakref_slot=False)
class C:
pass
c = C()
with pytest.raises(TypeError):
weakref.ref(c)
@pytest.mark.skipif(
not PYPY, reason="slots without weakref_slot should only work on PyPy"
)
def test_implicitly_weakrefable():
"""
Instance is weak-referenceable even when `weakref_slot=False` in PyPy.
"""
@attr.s(slots=True, weakref_slot=False)
class C:
pass
c = C()
w = weakref.ref(c)
assert c is w()
def test_weakrefable():
"""
Instance is weak-referenceable when `weakref_slot=True`.
"""
@attr.s(slots=True, weakref_slot=True)
class C:
pass
c = C()
w = weakref.ref(c)
assert c is w()
def test_weakref_does_not_add_a_field():
"""
`weakref_slot=True` does not add a field to the class.
"""
@attr.s(slots=True, weakref_slot=True)
class C:
field = attr.ib()
assert [f.name for f in attr.fields(C)] == ["field"]
def tests_weakref_does_not_add_when_inheriting_with_weakref():
"""
`weakref_slot=True` does not add a new __weakref__ slot when inheriting
one.
"""
@attr.s(slots=True, weakref_slot=True)
class C:
pass
@attr.s(slots=True, weakref_slot=True)
class D(C):
pass
d = D()
w = weakref.ref(d)
assert d is w()
def tests_weakref_does_not_add_with_weakref_attribute():
"""
`weakref_slot=True` does not add a new __weakref__ slot when an attribute
of that name exists.
"""
@attr.s(slots=True, weakref_slot=True)
class C:
__weakref__ = attr.ib(
init=False, hash=False, repr=False, eq=False, order=False
)
c = C()
w = weakref.ref(c)
assert c is w()
def test_slots_empty_cell():
"""
Tests that no `ValueError: Cell is empty` exception is raised when
closure cells are present with no contents in a `slots=True` class.
(issue https://github.com/python-attrs/attrs/issues/589)
On Python 3, if a method mentions `__class__` or uses the no-arg `super()`,
the compiler will bake a reference to the class in the method itself as
`method.__closure__`. Since `attrs` replaces the class with a clone,
`_ClassBuilder._create_slots_class(self)` will rewrite these references so
it keeps working. This method was not properly covering the edge case where
the closure cell was empty, we fixed it and this is the non-regression
test.
"""
@attr.s(slots=True)
class C:
field = attr.ib()
def f(self, a):
super(C, self).__init__()
C(field=1)
@attr.s(getstate_setstate=True)
class C2:
x = attr.ib()
@attr.s(slots=True, getstate_setstate=True)
class C2Slots:
x = attr.ib()
class TestPickle:
@pytest.mark.parametrize("protocol", range(pickle.HIGHEST_PROTOCOL))
def test_pickleable_by_default(self, protocol):
"""
If nothing else is passed, slotted classes can be pickled and
unpickled with all supported protocols.
"""
i1 = C1Slots(1, 2)
i2 = pickle.loads(pickle.dumps(i1, protocol))
assert i1 == i2
assert i1 is not i2
def test_no_getstate_setstate_for_dict_classes(self):
"""
As long as getstate_setstate is None, nothing is done to dict
classes.
"""
assert getattr(object, "__getstate__", None) is getattr(
C1, "__getstate__", None
)
assert getattr(object, "__setstate__", None) is getattr(
C1, "__setstate__", None
)
def test_no_getstate_setstate_if_option_false(self):
"""
Don't add getstate/setstate if getstate_setstate is False.
"""
@attr.s(slots=True, getstate_setstate=False)
class C:
x = attr.ib()
assert getattr(object, "__getstate__", None) is getattr(
C, "__getstate__", None
)
assert getattr(object, "__setstate__", None) is getattr(
C, "__setstate__", None
)
@pytest.mark.parametrize("cls", [C2(1), C2Slots(1)])
def test_getstate_set_state_force_true(self, cls):
"""
If getstate_setstate is True, add them unconditionally.
"""
assert None is not getattr(cls, "__getstate__", None)
assert None is not getattr(cls, "__setstate__", None)
def test_slots_super_property_get():
"""
Both `super()` and `super(self.__class__, self)` work.
"""
@attr.s(slots=True)
class A:
x = attr.ib()
@property
def f(self):
return self.x
@attr.s(slots=True)
class B(A):
@property
def f(self):
return super().f ** 2
@attr.s(slots=True)
class C(A):
@property
def f(self):
return super(C, self).f ** 2
assert B(11).f == 121
assert B(17).f == 289
assert C(11).f == 121
assert C(17).f == 289
def test_slots_super_property_get_shortcut():
"""
On Python 3, the `super()` shortcut is allowed.
"""
@attr.s(slots=True)
class A:
x = attr.ib()
@property
def f(self):
return self.x
@attr.s(slots=True)
class B(A):
@property
def f(self):
return super().f ** 2
assert B(11).f == 121
assert B(17).f == 289
@attr.s(slots=True)
class A:
x = attr.ib()
b = attr.ib()
c = attr.ib()
@pytest.mark.parametrize("cls", [A])
def test_slots_unpickle_after_change(cls):
"""
We don't assign properties we don't have anymore if the class has changed.
"""
a = cls(1, 2, 3)
a_pickled = pickle.dumps(a)
@attr.s(slots=True)
class NEW_A:
x = attr.ib()
c = attr.ib()
with mock.patch(f"{__name__}.A", NEW_A):
new_a = pickle.loads(a_pickled)
assert new_a.x == 1
assert new_a.c == 3
assert not hasattr(new_a, "b")