1 | n/a | # Copyright 2007 Google, Inc. All Rights Reserved. |
---|
2 | n/a | # Licensed to PSF under a Contributor Agreement. |
---|
3 | n/a | |
---|
4 | n/a | """Unit tests for abc.py.""" |
---|
5 | n/a | |
---|
6 | n/a | import unittest |
---|
7 | n/a | from test import support |
---|
8 | n/a | |
---|
9 | n/a | import abc |
---|
10 | n/a | from inspect import isabstract |
---|
11 | n/a | |
---|
12 | n/a | |
---|
13 | n/a | class TestLegacyAPI(unittest.TestCase): |
---|
14 | n/a | |
---|
15 | n/a | def test_abstractproperty_basics(self): |
---|
16 | n/a | @abc.abstractproperty |
---|
17 | n/a | def foo(self): pass |
---|
18 | n/a | self.assertTrue(foo.__isabstractmethod__) |
---|
19 | n/a | def bar(self): pass |
---|
20 | n/a | self.assertFalse(hasattr(bar, "__isabstractmethod__")) |
---|
21 | n/a | |
---|
22 | n/a | class C(metaclass=abc.ABCMeta): |
---|
23 | n/a | @abc.abstractproperty |
---|
24 | n/a | def foo(self): return 3 |
---|
25 | n/a | self.assertRaises(TypeError, C) |
---|
26 | n/a | class D(C): |
---|
27 | n/a | @property |
---|
28 | n/a | def foo(self): return super().foo |
---|
29 | n/a | self.assertEqual(D().foo, 3) |
---|
30 | n/a | self.assertFalse(getattr(D.foo, "__isabstractmethod__", False)) |
---|
31 | n/a | |
---|
32 | n/a | def test_abstractclassmethod_basics(self): |
---|
33 | n/a | @abc.abstractclassmethod |
---|
34 | n/a | def foo(cls): pass |
---|
35 | n/a | self.assertTrue(foo.__isabstractmethod__) |
---|
36 | n/a | @classmethod |
---|
37 | n/a | def bar(cls): pass |
---|
38 | n/a | self.assertFalse(getattr(bar, "__isabstractmethod__", False)) |
---|
39 | n/a | |
---|
40 | n/a | class C(metaclass=abc.ABCMeta): |
---|
41 | n/a | @abc.abstractclassmethod |
---|
42 | n/a | def foo(cls): return cls.__name__ |
---|
43 | n/a | self.assertRaises(TypeError, C) |
---|
44 | n/a | class D(C): |
---|
45 | n/a | @classmethod |
---|
46 | n/a | def foo(cls): return super().foo() |
---|
47 | n/a | self.assertEqual(D.foo(), 'D') |
---|
48 | n/a | self.assertEqual(D().foo(), 'D') |
---|
49 | n/a | |
---|
50 | n/a | def test_abstractstaticmethod_basics(self): |
---|
51 | n/a | @abc.abstractstaticmethod |
---|
52 | n/a | def foo(): pass |
---|
53 | n/a | self.assertTrue(foo.__isabstractmethod__) |
---|
54 | n/a | @staticmethod |
---|
55 | n/a | def bar(): pass |
---|
56 | n/a | self.assertFalse(getattr(bar, "__isabstractmethod__", False)) |
---|
57 | n/a | |
---|
58 | n/a | class C(metaclass=abc.ABCMeta): |
---|
59 | n/a | @abc.abstractstaticmethod |
---|
60 | n/a | def foo(): return 3 |
---|
61 | n/a | self.assertRaises(TypeError, C) |
---|
62 | n/a | class D(C): |
---|
63 | n/a | @staticmethod |
---|
64 | n/a | def foo(): return 4 |
---|
65 | n/a | self.assertEqual(D.foo(), 4) |
---|
66 | n/a | self.assertEqual(D().foo(), 4) |
---|
67 | n/a | |
---|
68 | n/a | |
---|
69 | n/a | class TestABC(unittest.TestCase): |
---|
70 | n/a | |
---|
71 | n/a | def test_ABC_helper(self): |
---|
72 | n/a | # create an ABC using the helper class and perform basic checks |
---|
73 | n/a | class C(abc.ABC): |
---|
74 | n/a | @classmethod |
---|
75 | n/a | @abc.abstractmethod |
---|
76 | n/a | def foo(cls): return cls.__name__ |
---|
77 | n/a | self.assertEqual(type(C), abc.ABCMeta) |
---|
78 | n/a | self.assertRaises(TypeError, C) |
---|
79 | n/a | class D(C): |
---|
80 | n/a | @classmethod |
---|
81 | n/a | def foo(cls): return super().foo() |
---|
82 | n/a | self.assertEqual(D.foo(), 'D') |
---|
83 | n/a | |
---|
84 | n/a | def test_abstractmethod_basics(self): |
---|
85 | n/a | @abc.abstractmethod |
---|
86 | n/a | def foo(self): pass |
---|
87 | n/a | self.assertTrue(foo.__isabstractmethod__) |
---|
88 | n/a | def bar(self): pass |
---|
89 | n/a | self.assertFalse(hasattr(bar, "__isabstractmethod__")) |
---|
90 | n/a | |
---|
91 | n/a | def test_abstractproperty_basics(self): |
---|
92 | n/a | @property |
---|
93 | n/a | @abc.abstractmethod |
---|
94 | n/a | def foo(self): pass |
---|
95 | n/a | self.assertTrue(foo.__isabstractmethod__) |
---|
96 | n/a | def bar(self): pass |
---|
97 | n/a | self.assertFalse(getattr(bar, "__isabstractmethod__", False)) |
---|
98 | n/a | |
---|
99 | n/a | class C(metaclass=abc.ABCMeta): |
---|
100 | n/a | @property |
---|
101 | n/a | @abc.abstractmethod |
---|
102 | n/a | def foo(self): return 3 |
---|
103 | n/a | self.assertRaises(TypeError, C) |
---|
104 | n/a | class D(C): |
---|
105 | n/a | @C.foo.getter |
---|
106 | n/a | def foo(self): return super().foo |
---|
107 | n/a | self.assertEqual(D().foo, 3) |
---|
108 | n/a | |
---|
109 | n/a | def test_abstractclassmethod_basics(self): |
---|
110 | n/a | @classmethod |
---|
111 | n/a | @abc.abstractmethod |
---|
112 | n/a | def foo(cls): pass |
---|
113 | n/a | self.assertTrue(foo.__isabstractmethod__) |
---|
114 | n/a | @classmethod |
---|
115 | n/a | def bar(cls): pass |
---|
116 | n/a | self.assertFalse(getattr(bar, "__isabstractmethod__", False)) |
---|
117 | n/a | |
---|
118 | n/a | class C(metaclass=abc.ABCMeta): |
---|
119 | n/a | @classmethod |
---|
120 | n/a | @abc.abstractmethod |
---|
121 | n/a | def foo(cls): return cls.__name__ |
---|
122 | n/a | self.assertRaises(TypeError, C) |
---|
123 | n/a | class D(C): |
---|
124 | n/a | @classmethod |
---|
125 | n/a | def foo(cls): return super().foo() |
---|
126 | n/a | self.assertEqual(D.foo(), 'D') |
---|
127 | n/a | self.assertEqual(D().foo(), 'D') |
---|
128 | n/a | |
---|
129 | n/a | def test_abstractstaticmethod_basics(self): |
---|
130 | n/a | @staticmethod |
---|
131 | n/a | @abc.abstractmethod |
---|
132 | n/a | def foo(): pass |
---|
133 | n/a | self.assertTrue(foo.__isabstractmethod__) |
---|
134 | n/a | @staticmethod |
---|
135 | n/a | def bar(): pass |
---|
136 | n/a | self.assertFalse(getattr(bar, "__isabstractmethod__", False)) |
---|
137 | n/a | |
---|
138 | n/a | class C(metaclass=abc.ABCMeta): |
---|
139 | n/a | @staticmethod |
---|
140 | n/a | @abc.abstractmethod |
---|
141 | n/a | def foo(): return 3 |
---|
142 | n/a | self.assertRaises(TypeError, C) |
---|
143 | n/a | class D(C): |
---|
144 | n/a | @staticmethod |
---|
145 | n/a | def foo(): return 4 |
---|
146 | n/a | self.assertEqual(D.foo(), 4) |
---|
147 | n/a | self.assertEqual(D().foo(), 4) |
---|
148 | n/a | |
---|
149 | n/a | def test_abstractmethod_integration(self): |
---|
150 | n/a | for abstractthing in [abc.abstractmethod, abc.abstractproperty, |
---|
151 | n/a | abc.abstractclassmethod, |
---|
152 | n/a | abc.abstractstaticmethod]: |
---|
153 | n/a | class C(metaclass=abc.ABCMeta): |
---|
154 | n/a | @abstractthing |
---|
155 | n/a | def foo(self): pass # abstract |
---|
156 | n/a | def bar(self): pass # concrete |
---|
157 | n/a | self.assertEqual(C.__abstractmethods__, {"foo"}) |
---|
158 | n/a | self.assertRaises(TypeError, C) # because foo is abstract |
---|
159 | n/a | self.assertTrue(isabstract(C)) |
---|
160 | n/a | class D(C): |
---|
161 | n/a | def bar(self): pass # concrete override of concrete |
---|
162 | n/a | self.assertEqual(D.__abstractmethods__, {"foo"}) |
---|
163 | n/a | self.assertRaises(TypeError, D) # because foo is still abstract |
---|
164 | n/a | self.assertTrue(isabstract(D)) |
---|
165 | n/a | class E(D): |
---|
166 | n/a | def foo(self): pass |
---|
167 | n/a | self.assertEqual(E.__abstractmethods__, set()) |
---|
168 | n/a | E() # now foo is concrete, too |
---|
169 | n/a | self.assertFalse(isabstract(E)) |
---|
170 | n/a | class F(E): |
---|
171 | n/a | @abstractthing |
---|
172 | n/a | def bar(self): pass # abstract override of concrete |
---|
173 | n/a | self.assertEqual(F.__abstractmethods__, {"bar"}) |
---|
174 | n/a | self.assertRaises(TypeError, F) # because bar is abstract now |
---|
175 | n/a | self.assertTrue(isabstract(F)) |
---|
176 | n/a | |
---|
177 | n/a | def test_descriptors_with_abstractmethod(self): |
---|
178 | n/a | class C(metaclass=abc.ABCMeta): |
---|
179 | n/a | @property |
---|
180 | n/a | @abc.abstractmethod |
---|
181 | n/a | def foo(self): return 3 |
---|
182 | n/a | @foo.setter |
---|
183 | n/a | @abc.abstractmethod |
---|
184 | n/a | def foo(self, val): pass |
---|
185 | n/a | self.assertRaises(TypeError, C) |
---|
186 | n/a | class D(C): |
---|
187 | n/a | @C.foo.getter |
---|
188 | n/a | def foo(self): return super().foo |
---|
189 | n/a | self.assertRaises(TypeError, D) |
---|
190 | n/a | class E(D): |
---|
191 | n/a | @D.foo.setter |
---|
192 | n/a | def foo(self, val): pass |
---|
193 | n/a | self.assertEqual(E().foo, 3) |
---|
194 | n/a | # check that the property's __isabstractmethod__ descriptor does the |
---|
195 | n/a | # right thing when presented with a value that fails truth testing: |
---|
196 | n/a | class NotBool(object): |
---|
197 | n/a | def __bool__(self): |
---|
198 | n/a | raise ValueError() |
---|
199 | n/a | __len__ = __bool__ |
---|
200 | n/a | with self.assertRaises(ValueError): |
---|
201 | n/a | class F(C): |
---|
202 | n/a | def bar(self): |
---|
203 | n/a | pass |
---|
204 | n/a | bar.__isabstractmethod__ = NotBool() |
---|
205 | n/a | foo = property(bar) |
---|
206 | n/a | |
---|
207 | n/a | |
---|
208 | n/a | def test_customdescriptors_with_abstractmethod(self): |
---|
209 | n/a | class Descriptor: |
---|
210 | n/a | def __init__(self, fget, fset=None): |
---|
211 | n/a | self._fget = fget |
---|
212 | n/a | self._fset = fset |
---|
213 | n/a | def getter(self, callable): |
---|
214 | n/a | return Descriptor(callable, self._fget) |
---|
215 | n/a | def setter(self, callable): |
---|
216 | n/a | return Descriptor(self._fget, callable) |
---|
217 | n/a | @property |
---|
218 | n/a | def __isabstractmethod__(self): |
---|
219 | n/a | return (getattr(self._fget, '__isabstractmethod__', False) |
---|
220 | n/a | or getattr(self._fset, '__isabstractmethod__', False)) |
---|
221 | n/a | class C(metaclass=abc.ABCMeta): |
---|
222 | n/a | @Descriptor |
---|
223 | n/a | @abc.abstractmethod |
---|
224 | n/a | def foo(self): return 3 |
---|
225 | n/a | @foo.setter |
---|
226 | n/a | @abc.abstractmethod |
---|
227 | n/a | def foo(self, val): pass |
---|
228 | n/a | self.assertRaises(TypeError, C) |
---|
229 | n/a | class D(C): |
---|
230 | n/a | @C.foo.getter |
---|
231 | n/a | def foo(self): return super().foo |
---|
232 | n/a | self.assertRaises(TypeError, D) |
---|
233 | n/a | class E(D): |
---|
234 | n/a | @D.foo.setter |
---|
235 | n/a | def foo(self, val): pass |
---|
236 | n/a | self.assertFalse(E.foo.__isabstractmethod__) |
---|
237 | n/a | |
---|
238 | n/a | def test_metaclass_abc(self): |
---|
239 | n/a | # Metaclasses can be ABCs, too. |
---|
240 | n/a | class A(metaclass=abc.ABCMeta): |
---|
241 | n/a | @abc.abstractmethod |
---|
242 | n/a | def x(self): |
---|
243 | n/a | pass |
---|
244 | n/a | self.assertEqual(A.__abstractmethods__, {"x"}) |
---|
245 | n/a | class meta(type, A): |
---|
246 | n/a | def x(self): |
---|
247 | n/a | return 1 |
---|
248 | n/a | class C(metaclass=meta): |
---|
249 | n/a | pass |
---|
250 | n/a | |
---|
251 | n/a | def test_registration_basics(self): |
---|
252 | n/a | class A(metaclass=abc.ABCMeta): |
---|
253 | n/a | pass |
---|
254 | n/a | class B(object): |
---|
255 | n/a | pass |
---|
256 | n/a | b = B() |
---|
257 | n/a | self.assertFalse(issubclass(B, A)) |
---|
258 | n/a | self.assertFalse(issubclass(B, (A,))) |
---|
259 | n/a | self.assertNotIsInstance(b, A) |
---|
260 | n/a | self.assertNotIsInstance(b, (A,)) |
---|
261 | n/a | B1 = A.register(B) |
---|
262 | n/a | self.assertTrue(issubclass(B, A)) |
---|
263 | n/a | self.assertTrue(issubclass(B, (A,))) |
---|
264 | n/a | self.assertIsInstance(b, A) |
---|
265 | n/a | self.assertIsInstance(b, (A,)) |
---|
266 | n/a | self.assertIs(B1, B) |
---|
267 | n/a | class C(B): |
---|
268 | n/a | pass |
---|
269 | n/a | c = C() |
---|
270 | n/a | self.assertTrue(issubclass(C, A)) |
---|
271 | n/a | self.assertTrue(issubclass(C, (A,))) |
---|
272 | n/a | self.assertIsInstance(c, A) |
---|
273 | n/a | self.assertIsInstance(c, (A,)) |
---|
274 | n/a | |
---|
275 | n/a | def test_register_as_class_deco(self): |
---|
276 | n/a | class A(metaclass=abc.ABCMeta): |
---|
277 | n/a | pass |
---|
278 | n/a | @A.register |
---|
279 | n/a | class B(object): |
---|
280 | n/a | pass |
---|
281 | n/a | b = B() |
---|
282 | n/a | self.assertTrue(issubclass(B, A)) |
---|
283 | n/a | self.assertTrue(issubclass(B, (A,))) |
---|
284 | n/a | self.assertIsInstance(b, A) |
---|
285 | n/a | self.assertIsInstance(b, (A,)) |
---|
286 | n/a | @A.register |
---|
287 | n/a | class C(B): |
---|
288 | n/a | pass |
---|
289 | n/a | c = C() |
---|
290 | n/a | self.assertTrue(issubclass(C, A)) |
---|
291 | n/a | self.assertTrue(issubclass(C, (A,))) |
---|
292 | n/a | self.assertIsInstance(c, A) |
---|
293 | n/a | self.assertIsInstance(c, (A,)) |
---|
294 | n/a | self.assertIs(C, A.register(C)) |
---|
295 | n/a | |
---|
296 | n/a | def test_isinstance_invalidation(self): |
---|
297 | n/a | class A(metaclass=abc.ABCMeta): |
---|
298 | n/a | pass |
---|
299 | n/a | class B: |
---|
300 | n/a | pass |
---|
301 | n/a | b = B() |
---|
302 | n/a | self.assertFalse(isinstance(b, A)) |
---|
303 | n/a | self.assertFalse(isinstance(b, (A,))) |
---|
304 | n/a | token_old = abc.get_cache_token() |
---|
305 | n/a | A.register(B) |
---|
306 | n/a | token_new = abc.get_cache_token() |
---|
307 | n/a | self.assertNotEqual(token_old, token_new) |
---|
308 | n/a | self.assertTrue(isinstance(b, A)) |
---|
309 | n/a | self.assertTrue(isinstance(b, (A,))) |
---|
310 | n/a | |
---|
311 | n/a | def test_registration_builtins(self): |
---|
312 | n/a | class A(metaclass=abc.ABCMeta): |
---|
313 | n/a | pass |
---|
314 | n/a | A.register(int) |
---|
315 | n/a | self.assertIsInstance(42, A) |
---|
316 | n/a | self.assertIsInstance(42, (A,)) |
---|
317 | n/a | self.assertTrue(issubclass(int, A)) |
---|
318 | n/a | self.assertTrue(issubclass(int, (A,))) |
---|
319 | n/a | class B(A): |
---|
320 | n/a | pass |
---|
321 | n/a | B.register(str) |
---|
322 | n/a | class C(str): pass |
---|
323 | n/a | self.assertIsInstance("", A) |
---|
324 | n/a | self.assertIsInstance("", (A,)) |
---|
325 | n/a | self.assertTrue(issubclass(str, A)) |
---|
326 | n/a | self.assertTrue(issubclass(str, (A,))) |
---|
327 | n/a | self.assertTrue(issubclass(C, A)) |
---|
328 | n/a | self.assertTrue(issubclass(C, (A,))) |
---|
329 | n/a | |
---|
330 | n/a | def test_registration_edge_cases(self): |
---|
331 | n/a | class A(metaclass=abc.ABCMeta): |
---|
332 | n/a | pass |
---|
333 | n/a | A.register(A) # should pass silently |
---|
334 | n/a | class A1(A): |
---|
335 | n/a | pass |
---|
336 | n/a | self.assertRaises(RuntimeError, A1.register, A) # cycles not allowed |
---|
337 | n/a | class B(object): |
---|
338 | n/a | pass |
---|
339 | n/a | A1.register(B) # ok |
---|
340 | n/a | A1.register(B) # should pass silently |
---|
341 | n/a | class C(A): |
---|
342 | n/a | pass |
---|
343 | n/a | A.register(C) # should pass silently |
---|
344 | n/a | self.assertRaises(RuntimeError, C.register, A) # cycles not allowed |
---|
345 | n/a | C.register(B) # ok |
---|
346 | n/a | |
---|
347 | n/a | def test_register_non_class(self): |
---|
348 | n/a | class A(metaclass=abc.ABCMeta): |
---|
349 | n/a | pass |
---|
350 | n/a | self.assertRaisesRegex(TypeError, "Can only register classes", |
---|
351 | n/a | A.register, 4) |
---|
352 | n/a | |
---|
353 | n/a | def test_registration_transitiveness(self): |
---|
354 | n/a | class A(metaclass=abc.ABCMeta): |
---|
355 | n/a | pass |
---|
356 | n/a | self.assertTrue(issubclass(A, A)) |
---|
357 | n/a | self.assertTrue(issubclass(A, (A,))) |
---|
358 | n/a | class B(metaclass=abc.ABCMeta): |
---|
359 | n/a | pass |
---|
360 | n/a | self.assertFalse(issubclass(A, B)) |
---|
361 | n/a | self.assertFalse(issubclass(A, (B,))) |
---|
362 | n/a | self.assertFalse(issubclass(B, A)) |
---|
363 | n/a | self.assertFalse(issubclass(B, (A,))) |
---|
364 | n/a | class C(metaclass=abc.ABCMeta): |
---|
365 | n/a | pass |
---|
366 | n/a | A.register(B) |
---|
367 | n/a | class B1(B): |
---|
368 | n/a | pass |
---|
369 | n/a | self.assertTrue(issubclass(B1, A)) |
---|
370 | n/a | self.assertTrue(issubclass(B1, (A,))) |
---|
371 | n/a | class C1(C): |
---|
372 | n/a | pass |
---|
373 | n/a | B1.register(C1) |
---|
374 | n/a | self.assertFalse(issubclass(C, B)) |
---|
375 | n/a | self.assertFalse(issubclass(C, (B,))) |
---|
376 | n/a | self.assertFalse(issubclass(C, B1)) |
---|
377 | n/a | self.assertFalse(issubclass(C, (B1,))) |
---|
378 | n/a | self.assertTrue(issubclass(C1, A)) |
---|
379 | n/a | self.assertTrue(issubclass(C1, (A,))) |
---|
380 | n/a | self.assertTrue(issubclass(C1, B)) |
---|
381 | n/a | self.assertTrue(issubclass(C1, (B,))) |
---|
382 | n/a | self.assertTrue(issubclass(C1, B1)) |
---|
383 | n/a | self.assertTrue(issubclass(C1, (B1,))) |
---|
384 | n/a | C1.register(int) |
---|
385 | n/a | class MyInt(int): |
---|
386 | n/a | pass |
---|
387 | n/a | self.assertTrue(issubclass(MyInt, A)) |
---|
388 | n/a | self.assertTrue(issubclass(MyInt, (A,))) |
---|
389 | n/a | self.assertIsInstance(42, A) |
---|
390 | n/a | self.assertIsInstance(42, (A,)) |
---|
391 | n/a | |
---|
392 | n/a | def test_all_new_methods_are_called(self): |
---|
393 | n/a | class A(metaclass=abc.ABCMeta): |
---|
394 | n/a | pass |
---|
395 | n/a | class B(object): |
---|
396 | n/a | counter = 0 |
---|
397 | n/a | def __new__(cls): |
---|
398 | n/a | B.counter += 1 |
---|
399 | n/a | return super().__new__(cls) |
---|
400 | n/a | class C(A, B): |
---|
401 | n/a | pass |
---|
402 | n/a | self.assertEqual(B.counter, 0) |
---|
403 | n/a | C() |
---|
404 | n/a | self.assertEqual(B.counter, 1) |
---|
405 | n/a | |
---|
406 | n/a | |
---|
407 | n/a | if __name__ == "__main__": |
---|
408 | n/a | unittest.main() |
---|