1 # Test case for property
2 # more tests are in test_descr
6 from test
.test_support
import run_unittest
8 class PropertyBase(Exception):
11 class PropertyGet(PropertyBase
):
14 class PropertySet(PropertyBase
):
17 class PropertyDel(PropertyBase
):
20 class BaseClass(object):
26 """BaseClass.getter"""
30 def spam(self
, value
):
37 class SubClass(BaseClass
):
39 @BaseClass.spam
.getter
42 raise PropertyGet(self
._spam
)
45 def spam(self
, value
):
46 raise PropertySet(self
._spam
)
50 raise PropertyDel(self
._spam
)
52 class PropertyDocBase(object):
56 spam
= property(_get_spam
, doc
="spam spam spam")
58 class PropertyDocSub(PropertyDocBase
):
59 @PropertyDocBase.spam
.getter
61 """The decorator does not use this doc string"""
64 class PropertySubNewGetter(BaseClass
):
65 @BaseClass.spam
.getter
70 class PropertyNewGetter(object):
73 """original docstring"""
80 class PropertyTests(unittest
.TestCase
):
81 def test_property_decorator_baseclass(self
):
84 self
.assertEqual(base
.spam
, 5)
85 self
.assertEqual(base
._spam
, 5)
87 self
.assertEqual(base
.spam
, 10)
88 self
.assertEqual(base
._spam
, 10)
90 self
.assertTrue(not hasattr(base
, "spam"))
91 self
.assertTrue(not hasattr(base
, "_spam"))
93 self
.assertEqual(base
.spam
, 20)
94 self
.assertEqual(base
._spam
, 20)
96 def test_property_decorator_subclass(self
):
99 self
.assertRaises(PropertyGet
, getattr, sub
, "spam")
100 self
.assertRaises(PropertySet
, setattr, sub
, "spam", None)
101 self
.assertRaises(PropertyDel
, delattr, sub
, "spam")
103 @unittest.skipIf(sys
.flags
.optimize
>= 2,
104 "Docstrings are omitted with -O2 and above")
105 def test_property_decorator_subclass_doc(self
):
107 self
.assertEqual(sub
.__class
__.spam
.__doc
__, "SubClass.getter")
109 @unittest.skipIf(sys
.flags
.optimize
>= 2,
110 "Docstrings are omitted with -O2 and above")
111 def test_property_decorator_baseclass_doc(self
):
113 self
.assertEqual(base
.__class
__.spam
.__doc
__, "BaseClass.getter")
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")
121 @unittest.skipIf(sys
.flags
.optimize
>= 2,
122 "Docstrings are omitted with -O2 and above")
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")
132 # Issue 5890: subclasses of property do not preserve method __doc__ strings
133 class PropertySub(property):
134 """This is a subclass of property"""
136 class PropertySubSlots(property):
137 """This is a subclass of property that defines __slots__"""
140 class PropertySubclassTests(unittest
.TestCase
):
142 def test_slots_docstring_copy_exception(self
):
147 """Trying to copy this docstring will raise an exception"""
149 except AttributeError:
152 raise Exception("AttributeError not raised")
154 @unittest.skipIf(sys
.flags
.optimize
>= 2,
155 "Docstrings are omitted with -O2 and above")
156 def test_docstring_copy(self
):
160 """spam wrapped in property subclass"""
164 "spam wrapped in property subclass")
166 @unittest.skipIf(sys
.flags
.optimize
<= 2,
167 "Docstrings are omitted with -O2 and above")
168 def test_property_setter_copies_getter_docstring(self
):
170 def __init__(self
): self
._spam
= 1
173 """spam wrapped in property subclass"""
176 def spam(self
, value
):
177 """this docstring is ignored"""
180 self
.assertEqual(foo
.spam
, 1)
182 self
.assertEqual(foo
.spam
, 2)
185 "spam wrapped in property subclass")
188 def spam(self
, value
):
189 """another ignored docstring"""
192 self
.assertEqual(foosub
.spam
, 1)
194 self
.assertEqual(foosub
.spam
, 'eggs')
197 "spam wrapped in property subclass")
199 @unittest.skipIf(sys
.flags
.optimize
<= 2,
200 "Docstrings are omitted with -O2 and above")
201 def test_property_new_getter_new_docstring(self
):
210 """a new docstring"""
212 self
.assertEqual(Foo
.spam
.__doc
__, "a new docstring")
213 class FooBase(object):
221 """a new docstring"""
223 self
.assertEqual(Foo
.spam
.__doc
__, "a new docstring")
228 run_unittest(PropertyTests
, PropertySubclassTests
)
230 if __name__
== '__main__':