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