-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
221 lines (202 loc) · 6.32 KB
/
tests.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
import abc
from metaclass import ABC, Object, Type
from unittest import TestCase, main
import types
class Test(TestCase):
def test_classes(self):
# PEP 422: Simple class initialisation hook
class BaseC(Object):
def __init_subclass__(cls):
cls.x = 0
class C(BaseC):
pass
self.assertEqual(0, C.x)
# inherited:
class D(C):
pass
self.assertEqual(0, D.__dict__['x'])
# overwrite:
class E(C):
x = 1
self.assertEqual(0, E.__dict__['x'])
# override:
class BaseF(C):
def __init_subclass__(cls):
cls.y = 1
class F(BaseF):
pass
self.assertEqual(1, F.y)
self.assertEqual(0, F.x)
self.assertNotIn('x', F.__dict__)
self.assertFalse(hasattr(C, 'y'))
# super:
class BaseG(C):
def __init_subclass__(cls):
super().__init_subclass__()
cls.y = 1
class G(BaseG):
pass
self.assertEqual(1, G.y)
self.assertEqual(0, G.__dict__['x'])
self.assertFalse(hasattr(C, 'y'))
# __class__:
class BaseI(Object):
def __init_subclass__(cls):
cls.x = 0
__class__.y += 1
y = 0
class I(BaseI):
pass
self.assertEqual(0, I.x)
self.assertEqual(1, I.y)
class J(I):
pass
self.assertEqual(0, J.__dict__['x'])
self.assertEqual(2, I.y)
class BaseK(J):
def __init_subclass__(cls):
super().__init_subclass__()
__class__.z += 1
z = 0
class K(BaseK):
pass
self.assertEqual(0, K.__dict__['x'])
self.assertEqual(4, I.y)
self.assertEqual(1, K.z)
self.assertFalse(hasattr(J, 'z'))
# multiple inheritance:
class L(Object):
def __init_subclass__(cls):
pass
class BaseM(L):
def __init_subclass__(cls):
super().__init_subclass__()
cls.x = 0
class M(BaseM):
pass
self.assertEqual(0, M.x)
class BaseN(L):
def __init_subclass__(cls):
super().__init_subclass__()
cls.y = 1
class N(BaseN):
pass
self.assertEqual(1, N.y)
class BaseO(M, N):
def __init_subclass__(cls):
super().__init_subclass__()
cls.z = 2
class O(BaseO):
pass
self.assertEqual(0, O.__dict__['x'])
self.assertEqual(1, O.__dict__['y'])
self.assertEqual(2, O.__dict__['z'])
# decorators:
def dec1(cls):
cls.x = 1
return cls
def dec2(cls):
cls.x = 2
return cls
@dec2
@dec1
class P:
def __init_subclass__(cls):
cls.x = 0
self.assertEqual(2, P.x)
# __init_subclass__ raises an exception:
S = sentinel = object()
with self.assertRaisesRegex(KeyError, 'xxx'):
class BaseS(Object):
def __init_subclass__(cls):
raise KeyError('xxx')
class S(BaseS):
pass
self.assertIs(sentinel, S)
def test_types(self):
# PEP 422: Simple class initialisation hook
def c(ns):
def __init_subclass__(cls):
cls.x = 0
ns['__init_subclass__'] = __init_subclass__
BaseC = types.new_class("BaseC", (Object,), exec_body=c)
C = types.new_class("C", (BaseC,))
self.assertEqual(C.x, 0)
# inherited:
D = types.new_class("D", (C,))
self.assertEqual(D.__dict__['x'], 0)
# overwrite:
def e(ns):
ns['x'] = 1
E = types.new_class("E", (C,), exec_body=e)
self.assertEqual(E.__dict__['x'], 0)
# override:
def f(ns):
def __init_subclass__(cls):
cls.y = 1
ns['__init_subclass__'] = __init_subclass__
BaseF = types.new_class("BaseF", (C,), exec_body=f)
F = types.new_class("F", (BaseF,))
self.assertEqual(F.y, 1)
self.assertEqual(F.x, 0)
self.assertNotIn('x', F.__dict__)
self.assertFalse(hasattr(C, 'y'))
# __init_subclass__ raises an exception:
def s(ns):
def __init_subclass__(cls):
raise KeyError('xxx')
ns['__init_subclass__'] = __init_subclass__
S = sentinel = object()
with self.assertRaisesRegex(KeyError, 'xxx'):
BaseS = types.new_class("BaseS", (Object,), exec_body=s)
S = types.new_class("S", (BaseS,))
self.assertIs(S, sentinel)
def test_args(self):
with self.assertRaises(TypeError):
class C(Object, some_arg=4):
pass
class Base(Object):
def __init_subclass__(cls, some_arg, **kwargs):
super().__init_subclass__(**kwargs)
cls.store_arg = some_arg
class A(Base, some_arg=3):
pass
self.assertEqual(A.store_arg, 3)
with self.assertRaises(TypeError):
class B(Base):
pass
def test_namespace(self):
class Class(Object):
a = 1
def b(self):
pass
c = 3
self.assertEqual(Class.__attribute_order__,
("__module__", "__qualname__", "a", "b", "c"))
def test_descriptor(self):
class Descriptor:
def __set_owner__(self, owner, name):
self.name = name
self.owner = owner
class NoDescriptor:
pass
class Class(Object):
d = Descriptor()
nd = NoDescriptor()
self.assertEqual(Class.d.name, "d")
self.assertIs(Class.d.owner, Class)
def test_abc(self):
class Base(abc.ABC):
@abc.abstractmethod
def f(self):
pass
class Incomplete(Base, ABC):
pass
class Complete(Incomplete):
def f(self):
pass
with self.assertRaises(TypeError):
a = Incomplete()
b = Complete()
if __name__ == "__main__":
main()