36 lines
884 B
Python
36 lines
884 B
Python
![]() |
"""Unit tests for slot-related functionality."""
|
||
|
import pytest
|
||
|
from pympler.asizeof import asizeof
|
||
|
|
||
|
import attr
|
||
|
|
||
|
|
||
|
@attr.s
|
||
|
class C1(object):
|
||
|
x = attr.ib(validator=attr.validators.instance_of(int))
|
||
|
y = attr.ib()
|
||
|
|
||
|
|
||
|
@attr.s(slots=True)
|
||
|
class C1Slots(object):
|
||
|
x = attr.ib(validator=attr.validators.instance_of(int))
|
||
|
y = attr.ib()
|
||
|
|
||
|
|
||
|
def test_slots_being_used():
|
||
|
"""Test whether the class really is 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 asizeof(slot_instance) < asizeof(non_slot_instance)
|
||
|
|
||
|
non_slot_instance.t = "test"
|
||
|
with pytest.raises(AttributeError):
|
||
|
slot_instance.t = "test"
|