-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_checks.py
180 lines (133 loc) · 3.82 KB
/
test_checks.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
from array import array
from datetime import date
from decimal import Decimal
from enum import Enum
from fractions import Fraction
from random import Random
from xml.etree.ElementTree import Element
import pytest
from slotscheck.checks import has_slotless_base, has_slots, slots_overlap
try:
from typing import TypedDict
except ImportError:
from typing_extensions import TypedDict
class HasSlots:
__slots__ = ("a", "b")
class GoodInherit(HasSlots):
__slots__ = ["c", "d", "e"]
class BadOverlaps(GoodInherit):
__slots__ = {"a": "some docstring", "f": "bla"}
class NoSlotsInherits(HasSlots):
pass
class BadInherit(Random):
__slots__ = ()
class BadInheritAndOverlap(NoSlotsInherits):
__slots__ = ["z", "b", "a"]
class ChildOfBadClass(BadInheritAndOverlap):
pass
class _RestrictiveMeta(type):
def __setattr__(self, name, value) -> None:
raise TypeError("BOOM!")
class _UnsettableClass(metaclass=_RestrictiveMeta):
pass
class OneStringSlot(HasSlots):
__slots__ = "baz"
class ArrayInherit(array):
__slots__ = ()
class FooMeta(type):
__slots__ = ()
class Foo(metaclass=FooMeta):
__slots__ = ()
class MyDict(TypedDict):
foo: str
class TestHasSlots:
@pytest.mark.parametrize(
"klass",
[type, dict, date, float, Decimal, Element, array],
)
def test_not_purepython(self, klass):
assert has_slots(klass)
def test_typeddict(self):
assert has_slots(MyDict)
@pytest.mark.parametrize(
"klass",
[
Fraction,
HasSlots,
GoodInherit,
BadInherit,
BadOverlaps,
OneStringSlot,
ArrayInherit,
Foo,
FooMeta,
],
)
def test_slots(self, klass):
assert has_slots(klass)
@pytest.mark.parametrize(
"klass",
[
Random,
Enum,
NoSlotsInherits,
ChildOfBadClass,
RuntimeError,
KeyboardInterrupt,
],
)
def test_no_slots(self, klass):
assert not has_slots(klass)
def test_immutable_class(self):
assert not has_slots(_UnsettableClass)
class TestSlotsOverlap:
@pytest.mark.parametrize(
"klass",
[type, dict, date, float, Decimal, AssertionError, RuntimeError],
)
def test_not_purepython(self, klass):
assert not slots_overlap(klass)
@pytest.mark.parametrize(
"klass",
[
Fraction,
HasSlots,
GoodInherit,
BadInherit,
OneStringSlot,
Foo,
FooMeta,
ArrayInherit,
],
)
def test_slots_ok(self, klass):
assert not slots_overlap(klass)
@pytest.mark.parametrize("klass", [BadOverlaps, BadInheritAndOverlap])
def test_slots_not_ok(self, klass):
assert slots_overlap(klass)
@pytest.mark.parametrize(
"klass", [Random, Enum, NoSlotsInherits, ChildOfBadClass]
)
def test_no_slots(self, klass):
assert not slots_overlap(klass)
class TestHasSlotlessBase:
@pytest.mark.parametrize(
"klass",
[type, dict, date, float, Decimal],
)
def test_not_purepython(self, klass):
assert not has_slotless_base(klass)
@pytest.mark.parametrize(
"klass", [Fraction, HasSlots, GoodInherit, BadOverlaps, OneStringSlot]
)
def test_slots_ok(self, klass):
assert not has_slotless_base(klass)
@pytest.mark.parametrize(
"klass",
[BadInherit, BadInheritAndOverlap, AssertionError, RuntimeError],
)
def test_slots_not_ok(self, klass):
assert has_slotless_base(klass)
@pytest.mark.parametrize("klass", [Enum, NoSlotsInherits, ChildOfBadClass])
def test_no_slots(self, klass):
assert not has_slotless_base(klass)