1 | n/a | # Test case for DynamicClassAttribute |
---|
2 | n/a | # more tests are in test_descr |
---|
3 | n/a | |
---|
4 | n/a | import abc |
---|
5 | n/a | import sys |
---|
6 | n/a | import unittest |
---|
7 | n/a | from types import DynamicClassAttribute |
---|
8 | n/a | |
---|
9 | n/a | class PropertyBase(Exception): |
---|
10 | n/a | pass |
---|
11 | n/a | |
---|
12 | n/a | class PropertyGet(PropertyBase): |
---|
13 | n/a | pass |
---|
14 | n/a | |
---|
15 | n/a | class PropertySet(PropertyBase): |
---|
16 | n/a | pass |
---|
17 | n/a | |
---|
18 | n/a | class PropertyDel(PropertyBase): |
---|
19 | n/a | pass |
---|
20 | n/a | |
---|
21 | n/a | class BaseClass(object): |
---|
22 | n/a | def __init__(self): |
---|
23 | n/a | self._spam = 5 |
---|
24 | n/a | |
---|
25 | n/a | @DynamicClassAttribute |
---|
26 | n/a | def spam(self): |
---|
27 | n/a | """BaseClass.getter""" |
---|
28 | n/a | return self._spam |
---|
29 | n/a | |
---|
30 | n/a | @spam.setter |
---|
31 | n/a | def spam(self, value): |
---|
32 | n/a | self._spam = value |
---|
33 | n/a | |
---|
34 | n/a | @spam.deleter |
---|
35 | n/a | def spam(self): |
---|
36 | n/a | del self._spam |
---|
37 | n/a | |
---|
38 | n/a | class SubClass(BaseClass): |
---|
39 | n/a | |
---|
40 | n/a | spam = BaseClass.__dict__['spam'] |
---|
41 | n/a | |
---|
42 | n/a | @spam.getter |
---|
43 | n/a | def spam(self): |
---|
44 | n/a | """SubClass.getter""" |
---|
45 | n/a | raise PropertyGet(self._spam) |
---|
46 | n/a | |
---|
47 | n/a | @spam.setter |
---|
48 | n/a | def spam(self, value): |
---|
49 | n/a | raise PropertySet(self._spam) |
---|
50 | n/a | |
---|
51 | n/a | @spam.deleter |
---|
52 | n/a | def spam(self): |
---|
53 | n/a | raise PropertyDel(self._spam) |
---|
54 | n/a | |
---|
55 | n/a | class PropertyDocBase(object): |
---|
56 | n/a | _spam = 1 |
---|
57 | n/a | def _get_spam(self): |
---|
58 | n/a | return self._spam |
---|
59 | n/a | spam = DynamicClassAttribute(_get_spam, doc="spam spam spam") |
---|
60 | n/a | |
---|
61 | n/a | class PropertyDocSub(PropertyDocBase): |
---|
62 | n/a | spam = PropertyDocBase.__dict__['spam'] |
---|
63 | n/a | @spam.getter |
---|
64 | n/a | def spam(self): |
---|
65 | n/a | """The decorator does not use this doc string""" |
---|
66 | n/a | return self._spam |
---|
67 | n/a | |
---|
68 | n/a | class PropertySubNewGetter(BaseClass): |
---|
69 | n/a | spam = BaseClass.__dict__['spam'] |
---|
70 | n/a | @spam.getter |
---|
71 | n/a | def spam(self): |
---|
72 | n/a | """new docstring""" |
---|
73 | n/a | return 5 |
---|
74 | n/a | |
---|
75 | n/a | class PropertyNewGetter(object): |
---|
76 | n/a | @DynamicClassAttribute |
---|
77 | n/a | def spam(self): |
---|
78 | n/a | """original docstring""" |
---|
79 | n/a | return 1 |
---|
80 | n/a | @spam.getter |
---|
81 | n/a | def spam(self): |
---|
82 | n/a | """new docstring""" |
---|
83 | n/a | return 8 |
---|
84 | n/a | |
---|
85 | n/a | class ClassWithAbstractVirtualProperty(metaclass=abc.ABCMeta): |
---|
86 | n/a | @DynamicClassAttribute |
---|
87 | n/a | @abc.abstractmethod |
---|
88 | n/a | def color(): |
---|
89 | n/a | pass |
---|
90 | n/a | |
---|
91 | n/a | class ClassWithPropertyAbstractVirtual(metaclass=abc.ABCMeta): |
---|
92 | n/a | @abc.abstractmethod |
---|
93 | n/a | @DynamicClassAttribute |
---|
94 | n/a | def color(): |
---|
95 | n/a | pass |
---|
96 | n/a | |
---|
97 | n/a | class PropertyTests(unittest.TestCase): |
---|
98 | n/a | def test_property_decorator_baseclass(self): |
---|
99 | n/a | # see #1620 |
---|
100 | n/a | base = BaseClass() |
---|
101 | n/a | self.assertEqual(base.spam, 5) |
---|
102 | n/a | self.assertEqual(base._spam, 5) |
---|
103 | n/a | base.spam = 10 |
---|
104 | n/a | self.assertEqual(base.spam, 10) |
---|
105 | n/a | self.assertEqual(base._spam, 10) |
---|
106 | n/a | delattr(base, "spam") |
---|
107 | n/a | self.assertTrue(not hasattr(base, "spam")) |
---|
108 | n/a | self.assertTrue(not hasattr(base, "_spam")) |
---|
109 | n/a | base.spam = 20 |
---|
110 | n/a | self.assertEqual(base.spam, 20) |
---|
111 | n/a | self.assertEqual(base._spam, 20) |
---|
112 | n/a | |
---|
113 | n/a | def test_property_decorator_subclass(self): |
---|
114 | n/a | # see #1620 |
---|
115 | n/a | sub = SubClass() |
---|
116 | n/a | self.assertRaises(PropertyGet, getattr, sub, "spam") |
---|
117 | n/a | self.assertRaises(PropertySet, setattr, sub, "spam", None) |
---|
118 | n/a | self.assertRaises(PropertyDel, delattr, sub, "spam") |
---|
119 | n/a | |
---|
120 | n/a | @unittest.skipIf(sys.flags.optimize >= 2, |
---|
121 | n/a | "Docstrings are omitted with -O2 and above") |
---|
122 | n/a | def test_property_decorator_subclass_doc(self): |
---|
123 | n/a | sub = SubClass() |
---|
124 | n/a | self.assertEqual(sub.__class__.__dict__['spam'].__doc__, "SubClass.getter") |
---|
125 | n/a | |
---|
126 | n/a | @unittest.skipIf(sys.flags.optimize >= 2, |
---|
127 | n/a | "Docstrings are omitted with -O2 and above") |
---|
128 | n/a | def test_property_decorator_baseclass_doc(self): |
---|
129 | n/a | base = BaseClass() |
---|
130 | n/a | self.assertEqual(base.__class__.__dict__['spam'].__doc__, "BaseClass.getter") |
---|
131 | n/a | |
---|
132 | n/a | def test_property_decorator_doc(self): |
---|
133 | n/a | base = PropertyDocBase() |
---|
134 | n/a | sub = PropertyDocSub() |
---|
135 | n/a | self.assertEqual(base.__class__.__dict__['spam'].__doc__, "spam spam spam") |
---|
136 | n/a | self.assertEqual(sub.__class__.__dict__['spam'].__doc__, "spam spam spam") |
---|
137 | n/a | |
---|
138 | n/a | @unittest.skipIf(sys.flags.optimize >= 2, |
---|
139 | n/a | "Docstrings are omitted with -O2 and above") |
---|
140 | n/a | def test_property_getter_doc_override(self): |
---|
141 | n/a | newgettersub = PropertySubNewGetter() |
---|
142 | n/a | self.assertEqual(newgettersub.spam, 5) |
---|
143 | n/a | self.assertEqual(newgettersub.__class__.__dict__['spam'].__doc__, "new docstring") |
---|
144 | n/a | newgetter = PropertyNewGetter() |
---|
145 | n/a | self.assertEqual(newgetter.spam, 8) |
---|
146 | n/a | self.assertEqual(newgetter.__class__.__dict__['spam'].__doc__, "new docstring") |
---|
147 | n/a | |
---|
148 | n/a | def test_property___isabstractmethod__descriptor(self): |
---|
149 | n/a | for val in (True, False, [], [1], '', '1'): |
---|
150 | n/a | class C(object): |
---|
151 | n/a | def foo(self): |
---|
152 | n/a | pass |
---|
153 | n/a | foo.__isabstractmethod__ = val |
---|
154 | n/a | foo = DynamicClassAttribute(foo) |
---|
155 | n/a | self.assertIs(C.__dict__['foo'].__isabstractmethod__, bool(val)) |
---|
156 | n/a | |
---|
157 | n/a | # check that the DynamicClassAttribute's __isabstractmethod__ descriptor does the |
---|
158 | n/a | # right thing when presented with a value that fails truth testing: |
---|
159 | n/a | class NotBool(object): |
---|
160 | n/a | def __bool__(self): |
---|
161 | n/a | raise ValueError() |
---|
162 | n/a | __len__ = __bool__ |
---|
163 | n/a | with self.assertRaises(ValueError): |
---|
164 | n/a | class C(object): |
---|
165 | n/a | def foo(self): |
---|
166 | n/a | pass |
---|
167 | n/a | foo.__isabstractmethod__ = NotBool() |
---|
168 | n/a | foo = DynamicClassAttribute(foo) |
---|
169 | n/a | |
---|
170 | n/a | def test_abstract_virtual(self): |
---|
171 | n/a | self.assertRaises(TypeError, ClassWithAbstractVirtualProperty) |
---|
172 | n/a | self.assertRaises(TypeError, ClassWithPropertyAbstractVirtual) |
---|
173 | n/a | class APV(ClassWithPropertyAbstractVirtual): |
---|
174 | n/a | pass |
---|
175 | n/a | self.assertRaises(TypeError, APV) |
---|
176 | n/a | class AVP(ClassWithAbstractVirtualProperty): |
---|
177 | n/a | pass |
---|
178 | n/a | self.assertRaises(TypeError, AVP) |
---|
179 | n/a | class Okay1(ClassWithAbstractVirtualProperty): |
---|
180 | n/a | @DynamicClassAttribute |
---|
181 | n/a | def color(self): |
---|
182 | n/a | return self._color |
---|
183 | n/a | def __init__(self): |
---|
184 | n/a | self._color = 'cyan' |
---|
185 | n/a | with self.assertRaises(AttributeError): |
---|
186 | n/a | Okay1.color |
---|
187 | n/a | self.assertEqual(Okay1().color, 'cyan') |
---|
188 | n/a | class Okay2(ClassWithAbstractVirtualProperty): |
---|
189 | n/a | @DynamicClassAttribute |
---|
190 | n/a | def color(self): |
---|
191 | n/a | return self._color |
---|
192 | n/a | def __init__(self): |
---|
193 | n/a | self._color = 'magenta' |
---|
194 | n/a | with self.assertRaises(AttributeError): |
---|
195 | n/a | Okay2.color |
---|
196 | n/a | self.assertEqual(Okay2().color, 'magenta') |
---|
197 | n/a | |
---|
198 | n/a | |
---|
199 | n/a | # Issue 5890: subclasses of DynamicClassAttribute do not preserve method __doc__ strings |
---|
200 | n/a | class PropertySub(DynamicClassAttribute): |
---|
201 | n/a | """This is a subclass of DynamicClassAttribute""" |
---|
202 | n/a | |
---|
203 | n/a | class PropertySubSlots(DynamicClassAttribute): |
---|
204 | n/a | """This is a subclass of DynamicClassAttribute that defines __slots__""" |
---|
205 | n/a | __slots__ = () |
---|
206 | n/a | |
---|
207 | n/a | class PropertySubclassTests(unittest.TestCase): |
---|
208 | n/a | |
---|
209 | n/a | @unittest.skipIf(hasattr(PropertySubSlots, '__doc__'), |
---|
210 | n/a | "__doc__ is already present, __slots__ will have no effect") |
---|
211 | n/a | def test_slots_docstring_copy_exception(self): |
---|
212 | n/a | try: |
---|
213 | n/a | class Foo(object): |
---|
214 | n/a | @PropertySubSlots |
---|
215 | n/a | def spam(self): |
---|
216 | n/a | """Trying to copy this docstring will raise an exception""" |
---|
217 | n/a | return 1 |
---|
218 | n/a | print('\n',spam.__doc__) |
---|
219 | n/a | except AttributeError: |
---|
220 | n/a | pass |
---|
221 | n/a | else: |
---|
222 | n/a | raise Exception("AttributeError not raised") |
---|
223 | n/a | |
---|
224 | n/a | @unittest.skipIf(sys.flags.optimize >= 2, |
---|
225 | n/a | "Docstrings are omitted with -O2 and above") |
---|
226 | n/a | def test_docstring_copy(self): |
---|
227 | n/a | class Foo(object): |
---|
228 | n/a | @PropertySub |
---|
229 | n/a | def spam(self): |
---|
230 | n/a | """spam wrapped in DynamicClassAttribute subclass""" |
---|
231 | n/a | return 1 |
---|
232 | n/a | self.assertEqual( |
---|
233 | n/a | Foo.__dict__['spam'].__doc__, |
---|
234 | n/a | "spam wrapped in DynamicClassAttribute subclass") |
---|
235 | n/a | |
---|
236 | n/a | @unittest.skipIf(sys.flags.optimize >= 2, |
---|
237 | n/a | "Docstrings are omitted with -O2 and above") |
---|
238 | n/a | def test_property_setter_copies_getter_docstring(self): |
---|
239 | n/a | class Foo(object): |
---|
240 | n/a | def __init__(self): self._spam = 1 |
---|
241 | n/a | @PropertySub |
---|
242 | n/a | def spam(self): |
---|
243 | n/a | """spam wrapped in DynamicClassAttribute subclass""" |
---|
244 | n/a | return self._spam |
---|
245 | n/a | @spam.setter |
---|
246 | n/a | def spam(self, value): |
---|
247 | n/a | """this docstring is ignored""" |
---|
248 | n/a | self._spam = value |
---|
249 | n/a | foo = Foo() |
---|
250 | n/a | self.assertEqual(foo.spam, 1) |
---|
251 | n/a | foo.spam = 2 |
---|
252 | n/a | self.assertEqual(foo.spam, 2) |
---|
253 | n/a | self.assertEqual( |
---|
254 | n/a | Foo.__dict__['spam'].__doc__, |
---|
255 | n/a | "spam wrapped in DynamicClassAttribute subclass") |
---|
256 | n/a | class FooSub(Foo): |
---|
257 | n/a | spam = Foo.__dict__['spam'] |
---|
258 | n/a | @spam.setter |
---|
259 | n/a | def spam(self, value): |
---|
260 | n/a | """another ignored docstring""" |
---|
261 | n/a | self._spam = 'eggs' |
---|
262 | n/a | foosub = FooSub() |
---|
263 | n/a | self.assertEqual(foosub.spam, 1) |
---|
264 | n/a | foosub.spam = 7 |
---|
265 | n/a | self.assertEqual(foosub.spam, 'eggs') |
---|
266 | n/a | self.assertEqual( |
---|
267 | n/a | FooSub.__dict__['spam'].__doc__, |
---|
268 | n/a | "spam wrapped in DynamicClassAttribute subclass") |
---|
269 | n/a | |
---|
270 | n/a | @unittest.skipIf(sys.flags.optimize >= 2, |
---|
271 | n/a | "Docstrings are omitted with -O2 and above") |
---|
272 | n/a | def test_property_new_getter_new_docstring(self): |
---|
273 | n/a | |
---|
274 | n/a | class Foo(object): |
---|
275 | n/a | @PropertySub |
---|
276 | n/a | def spam(self): |
---|
277 | n/a | """a docstring""" |
---|
278 | n/a | return 1 |
---|
279 | n/a | @spam.getter |
---|
280 | n/a | def spam(self): |
---|
281 | n/a | """a new docstring""" |
---|
282 | n/a | return 2 |
---|
283 | n/a | self.assertEqual(Foo.__dict__['spam'].__doc__, "a new docstring") |
---|
284 | n/a | class FooBase(object): |
---|
285 | n/a | @PropertySub |
---|
286 | n/a | def spam(self): |
---|
287 | n/a | """a docstring""" |
---|
288 | n/a | return 1 |
---|
289 | n/a | class Foo2(FooBase): |
---|
290 | n/a | spam = FooBase.__dict__['spam'] |
---|
291 | n/a | @spam.getter |
---|
292 | n/a | def spam(self): |
---|
293 | n/a | """a new docstring""" |
---|
294 | n/a | return 2 |
---|
295 | n/a | self.assertEqual(Foo.__dict__['spam'].__doc__, "a new docstring") |
---|
296 | n/a | |
---|
297 | n/a | |
---|
298 | n/a | |
---|
299 | n/a | if __name__ == '__main__': |
---|
300 | n/a | unittest.main() |
---|