Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(170036)

Side by Side Diff: Lib/test/test_abc.py

Issue 11610: Improving property to accept abstract methods
Patch Set: Created 8 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright 2007 Google, Inc. All Rights Reserved. 1 # Copyright 2007 Google, Inc. All Rights Reserved.
2 # Licensed to PSF under a Contributor Agreement. 2 # Licensed to PSF under a Contributor Agreement.
3 3
4 """Unit tests for abc.py.""" 4 """Unit tests for abc.py."""
5 5
6 import unittest 6 import unittest
7 from test import support 7 from test import support
8 8
9 import abc 9 import abc
10 from inspect import isabstract 10 from inspect import isabstract
11 11
12 12
13 class TestABC(unittest.TestCase): 13 class TestABC(unittest.TestCase):
14 14
15 def test_abstractmethod_basics(self): 15 def test_abstractmethod_basics(self):
16 @abc.abstractmethod 16 @abc.abstractmethod
17 def foo(self): pass 17 def foo(self): pass
18 self.assertTrue(foo.__isabstractmethod__) 18 self.assertTrue(foo.__isabstractmethod__)
19 def bar(self): pass 19 def bar(self): pass
20 self.assertFalse(hasattr(bar, "__isabstractmethod__")) 20 self.assertFalse(getattr(bar, "__isabstractmethod__", False))
Nick Coghlan 2011/12/06 09:14:01 This test shouldn't be affected - please change it
dsdale24 2011/12/06 17:45:58 The original test wants to see that bar does not h
21 21
22 def test_abstractproperty_basics(self): 22 def test_abstractproperty_basics(self):
23 @abc.abstractproperty 23 @property
24 @abc.abstractmethod
Nick Coghlan 2011/12/06 09:14:01 Don't remove the tests for the old behaviours - th
24 def foo(self): pass 25 def foo(self): pass
25 self.assertTrue(foo.__isabstractmethod__) 26 self.assertTrue(foo.__isabstractmethod__)
26 def bar(self): pass 27 def bar(self): pass
27 self.assertFalse(hasattr(bar, "__isabstractmethod__")) 28 self.assertFalse(getattr(bar, "__isabstractmethod__", False))
28 29
29 class C(metaclass=abc.ABCMeta): 30 class C(metaclass=abc.ABCMeta):
30 @abc.abstractproperty 31 @property
32 @abc.abstractmethod
31 def foo(self): return 3 33 def foo(self): return 3
34 self.assertRaises(TypeError, C)
32 class D(C): 35 class D(C):
33 @property 36 @C.foo.getter
34 def foo(self): return super().foo 37 def foo(self): return super().foo
35 self.assertEqual(D().foo, 3) 38 self.assertEqual(D().foo, 3)
36 39
37 def test_abstractclassmethod_basics(self): 40 def test_abstractclassmethod_basics(self):
38 @abc.abstractclassmethod 41 @classmethod
42 @abc.abstractmethod
39 def foo(cls): pass 43 def foo(cls): pass
40 self.assertTrue(foo.__isabstractmethod__) 44 self.assertTrue(foo.__isabstractmethod__)
41 @classmethod 45 @classmethod
42 def bar(cls): pass 46 def bar(cls): pass
43 self.assertFalse(hasattr(bar, "__isabstractmethod__")) 47 self.assertFalse(getattr(bar, "__isabstractmethod__", False))
44 48
45 class C(metaclass=abc.ABCMeta): 49 class C(metaclass=abc.ABCMeta):
46 @abc.abstractclassmethod 50 @classmethod
51 @abc.abstractmethod
47 def foo(cls): return cls.__name__ 52 def foo(cls): return cls.__name__
48 self.assertRaises(TypeError, C) 53 self.assertRaises(TypeError, C)
49 class D(C): 54 class D(C):
50 @classmethod 55 @classmethod
51 def foo(cls): return super().foo() 56 def foo(cls): return super().foo()
52 self.assertEqual(D.foo(), 'D') 57 self.assertEqual(D.foo(), 'D')
53 self.assertEqual(D().foo(), 'D') 58 self.assertEqual(D().foo(), 'D')
54 59
55 def test_abstractstaticmethod_basics(self): 60 def test_abstractstaticmethod_basics(self):
56 @abc.abstractstaticmethod 61 @staticmethod
62 @abc.abstractmethod
57 def foo(): pass 63 def foo(): pass
58 self.assertTrue(foo.__isabstractmethod__) 64 self.assertTrue(foo.__isabstractmethod__)
59 @staticmethod 65 @staticmethod
60 def bar(): pass 66 def bar(): pass
61 self.assertFalse(hasattr(bar, "__isabstractmethod__")) 67 self.assertFalse(getattr(bar, "__isabstractmethod__", False))
62 68
63 class C(metaclass=abc.ABCMeta): 69 class C(metaclass=abc.ABCMeta):
64 @abc.abstractstaticmethod 70 @staticmethod
71 @abc.abstractmethod
65 def foo(): return 3 72 def foo(): return 3
66 self.assertRaises(TypeError, C) 73 self.assertRaises(TypeError, C)
67 class D(C): 74 class D(C):
68 @staticmethod 75 @staticmethod
69 def foo(): return 4 76 def foo(): return 4
70 self.assertEqual(D.foo(), 4) 77 self.assertEqual(D.foo(), 4)
71 self.assertEqual(D().foo(), 4) 78 self.assertEqual(D().foo(), 4)
72 79
73 def test_abstractmethod_integration(self): 80 def test_abstractmethod_integration(self):
74 for abstractthing in [abc.abstractmethod, abc.abstractproperty, 81 for abstractthing in [abc.abstractmethod, abc.abstractproperty,
(...skipping 15 matching lines...) Expand all
90 def foo(self): pass 97 def foo(self): pass
91 self.assertEqual(E.__abstractmethods__, set()) 98 self.assertEqual(E.__abstractmethods__, set())
92 E() # now foo is concrete, too 99 E() # now foo is concrete, too
93 self.assertFalse(isabstract(E)) 100 self.assertFalse(isabstract(E))
94 class F(E): 101 class F(E):
95 @abstractthing 102 @abstractthing
96 def bar(self): pass # abstract override of concrete 103 def bar(self): pass # abstract override of concrete
97 self.assertEqual(F.__abstractmethods__, {"bar"}) 104 self.assertEqual(F.__abstractmethods__, {"bar"})
98 self.assertRaises(TypeError, F) # because bar is abstract now 105 self.assertRaises(TypeError, F) # because bar is abstract now
99 self.assertTrue(isabstract(F)) 106 self.assertTrue(isabstract(F))
107
108 def test_descriptors_with_abstractmethod(self):
109 class C(metaclass=abc.ABCMeta):
110 @property
111 @abc.abstractmethod
112 def foo(self): return 3
113 @foo.setter
114 @abc.abstractmethod
115 def foo(self, val): pass
116 self.assertRaises(TypeError, C)
117 class D(C):
118 @C.foo.getter
119 def foo(self): return super().foo
120 self.assertRaises(TypeError, D)
121 class E(D):
122 @D.foo.setter
123 def foo(self, val): pass
124 self.assertEqual(E().foo, 3)
125 # check that the property's __isabstractmethod__ descriptor does the
126 # right thing when presented with a value that fails truth testing:
127 class NotBool(object):
128 def __nonzero__(self):
129 raise ValueError()
130 __len__ = __nonzero__
131 with self.assertRaises(ValueError):
132 class F(C):
133 def bar(self):
134 pass
135 bar.__isabstractmethod__ = NotBool()
136 foo = property(bar)
137
138
139 def test_customdescriptors_with_abstractmethod(self):
140 class Descriptor:
141 def __init__(self, fget, fset=None):
142 self._fget = fget
143 self._fset = fset
144 def getter(self, callable):
145 return Descriptor(callable, self._fget)
146 def setter(self, callable):
147 return Descriptor(self._fget, callable)
148 @property
149 def __isabstractmethod__(self):
150 return (getattr(self._fget, '__isabstractmethod__', False)
151 or getattr(self._fset, '__isabstractmethod__', False))
152 class C(metaclass=abc.ABCMeta):
153 @Descriptor
154 @abc.abstractmethod
155 def foo(self): return 3
156 @foo.setter
157 @abc.abstractmethod
158 def foo(self, val): pass
159 self.assertRaises(TypeError, C)
160 class D(C):
161 @C.foo.getter
162 def foo(self): return super().foo
163 self.assertRaises(TypeError, D)
164 class E(D):
165 @D.foo.setter
166 def foo(self, val): pass
167 self.assertFalse(E.foo.__isabstractmethod__)
100 168
101 def test_metaclass_abc(self): 169 def test_metaclass_abc(self):
102 # Metaclasses can be ABCs, too. 170 # Metaclasses can be ABCs, too.
103 class A(metaclass=abc.ABCMeta): 171 class A(metaclass=abc.ABCMeta):
104 @abc.abstractmethod 172 @abc.abstractmethod
105 def x(self): 173 def x(self):
106 pass 174 pass
107 self.assertEqual(A.__abstractmethods__, {"x"}) 175 self.assertEqual(A.__abstractmethods__, {"x"})
108 class meta(type, A): 176 class meta(type, A):
109 def x(self): 177 def x(self):
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 C() 331 C()
264 self.assertEqual(B.counter, 1) 332 self.assertEqual(B.counter, 1)
265 333
266 334
267 def test_main(): 335 def test_main():
268 support.run_unittest(TestABC) 336 support.run_unittest(TestABC)
269 337
270 338
271 if __name__ == "__main__": 339 if __name__ == "__main__":
272 unittest.main() 340 unittest.main()
OLDNEW

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+