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