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

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

Issue 11610: Improving property to accept abstract methods
Patch Set: Created 8 years, 7 months 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
« no previous file with comments | « Lib/numbers.py ('k') | Misc/NEWS » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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(hasattr(bar, "__isabstractmethod__"))
21 21
22 def test_abstractproperty_basics(self): 22 def test_abstractproperty_basics(self):
23 "abstractproperty is deprecated in python-3.3"
23 @abc.abstractproperty 24 @abc.abstractproperty
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(hasattr(bar, "__isabstractmethod__"))
28 29
29 class C(metaclass=abc.ABCMeta): 30 class C(metaclass=abc.ABCMeta):
30 @abc.abstractproperty 31 @abc.abstractproperty
31 def foo(self): return 3 32 def foo(self): return 3
33 self.assertRaises(TypeError, C)
32 class D(C): 34 class D(C):
33 @property 35 @property
34 def foo(self): return super().foo 36 def foo(self): return super().foo
35 self.assertEqual(D().foo, 3) 37 self.assertEqual(D().foo, 3)
36 38
37 def test_abstractclassmethod_basics(self): 39 def test_abstractclassmethod_basics(self):
38 @abc.abstractclassmethod 40 @abc.abstractclassmethod
39 def foo(cls): pass 41 def foo(cls): pass
40 self.assertTrue(foo.__isabstractmethod__) 42 self.assertTrue(foo.__isabstractmethod__)
41 @classmethod 43 @classmethod
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 def foo(self): pass 92 def foo(self): pass
91 self.assertEqual(E.__abstractmethods__, set()) 93 self.assertEqual(E.__abstractmethods__, set())
92 E() # now foo is concrete, too 94 E() # now foo is concrete, too
93 self.assertFalse(isabstract(E)) 95 self.assertFalse(isabstract(E))
94 class F(E): 96 class F(E):
95 @abstractthing 97 @abstractthing
96 def bar(self): pass # abstract override of concrete 98 def bar(self): pass # abstract override of concrete
97 self.assertEqual(F.__abstractmethods__, {"bar"}) 99 self.assertEqual(F.__abstractmethods__, {"bar"})
98 self.assertRaises(TypeError, F) # because bar is abstract now 100 self.assertRaises(TypeError, F) # because bar is abstract now
99 self.assertTrue(isabstract(F)) 101 self.assertTrue(isabstract(F))
102
103 def test_descriptors_with_abstractmethod(self):
104 class C(metaclass=abc.ABCMeta):
105 @property
106 @abc.abstractmethod
107 def foo(self): return 3
108 @foo.setter
109 @abc.abstractmethod
110 def foo(self, val): pass
111 self.assertRaises(TypeError, C)
112 class D(C):
113 @C.foo.getter
114 def foo(self): return super().foo
115 self.assertRaises(TypeError, D)
116 class E(D):
117 @D.foo.setter
118 def foo(self, val): pass
119 self.assertEqual(E().foo, 3)
120 class F(C):
121 @property
122 def foo(self): return 3
123 self.assertRaises(TypeError, F)
124
125 def test_pathologically_named_descriptors(self):
126 """Test that attribute names containing "." doesn't choke ABCMeta"""
127 class MyProperty(property):
128 pass
129 class C(metaclass=abc.ABCMeta):
130 @MyProperty
131 @abc.abstractmethod
132 def foo(self): return 1
133 setattr(foo, 'pathological.desc_method', None)
134 @MyProperty
135 def bar(self): return
136 setattr(bar, 'pathological.desc_method', None)
137 setattr(self, 'pathological.bar', foo)
138 self.assertRaises(TypeError, C)
139 class D(C):
140 @C.foo.getter
141 def foo(self): return 2
142 self.assertEqual(D().foo, 2)
100 143
101 def test_metaclass_abc(self): 144 def test_metaclass_abc(self):
102 # Metaclasses can be ABCs, too. 145 # Metaclasses can be ABCs, too.
103 class A(metaclass=abc.ABCMeta): 146 class A(metaclass=abc.ABCMeta):
104 @abc.abstractmethod 147 @abc.abstractmethod
105 def x(self): 148 def x(self):
106 pass 149 pass
107 self.assertEqual(A.__abstractmethods__, {"x"}) 150 self.assertEqual(A.__abstractmethods__, {"x"})
108 class meta(type, A): 151 class meta(type, A):
109 def x(self): 152 def x(self):
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 C() 306 C()
264 self.assertEqual(B.counter, 1) 307 self.assertEqual(B.counter, 1)
265 308
266 309
267 def test_main(): 310 def test_main():
268 support.run_unittest(TestABC) 311 support.run_unittest(TestABC)
269 312
270 313
271 if __name__ == "__main__": 314 if __name__ == "__main__":
272 unittest.main() 315 unittest.main()
OLDNEW
« no previous file with comments | « Lib/numbers.py ('k') | Misc/NEWS » ('j') | no next file with comments »

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