1 # Test case for property
2 # more tests are in test_descr
5 from test
.test_support
import run_unittest
7 class PropertyBase(Exception):
10 class PropertyGet(PropertyBase
):
13 class PropertySet(PropertyBase
):
16 class PropertyDel(PropertyBase
):
19 class BaseClass(object):
25 """BaseClass.getter"""
29 def spam(self
, value
):
36 class SubClass(BaseClass
):
38 @BaseClass.spam
.getter
41 raise PropertyGet(self
._spam
)
44 def spam(self
, value
):
45 raise PropertySet(self
._spam
)
49 raise PropertyDel(self
._spam
)
51 class PropertyDocBase(object):
55 spam
= property(_get_spam
, doc
="spam spam spam")
57 class PropertyDocSub(PropertyDocBase
):
58 @PropertyDocBase.spam
.getter
60 """The decorator does not use this doc string"""
63 class PropertySubNewGetter(BaseClass
):
64 @BaseClass.spam
.getter
69 class PropertyNewGetter(object):
72 """original docstring"""
79 class PropertyTests(unittest
.TestCase
):
80 def test_property_decorator_baseclass(self
):
83 self
.assertEqual(base
.spam
, 5)
84 self
.assertEqual(base
._spam
, 5)
86 self
.assertEqual(base
.spam
, 10)
87 self
.assertEqual(base
._spam
, 10)
89 self
.assertTrue(not hasattr(base
, "spam"))
90 self
.assertTrue(not hasattr(base
, "_spam"))
92 self
.assertEqual(base
.spam
, 20)
93 self
.assertEqual(base
._spam
, 20)
94 self
.assertEqual(base
.__class
__.spam
.__doc
__, "BaseClass.getter")
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")
102 self
.assertEqual(sub
.__class
__.spam
.__doc
__, "SubClass.getter")
104 def test_property_decorator_doc(self
):
105 base
= PropertyDocBase()
106 sub
= PropertyDocSub()
107 self
.assertEqual(base
.__class
__.spam
.__doc
__, "spam spam spam")
108 self
.assertEqual(sub
.__class
__.spam
.__doc
__, "spam spam spam")
110 def test_property_getter_doc_override(self
):
111 newgettersub
= PropertySubNewGetter()
112 self
.assertEqual(newgettersub
.spam
, 5)
113 self
.assertEqual(newgettersub
.__class
__.spam
.__doc
__, "new docstring")
114 newgetter
= PropertyNewGetter()
115 self
.assertEqual(newgetter
.spam
, 8)
116 self
.assertEqual(newgetter
.__class
__.spam
.__doc
__, "new docstring")
119 # Issue 5890: subclasses of property do not preserve method __doc__ strings
120 class PropertySub(property):
121 """This is a subclass of property"""
123 class PropertySubSlots(property):
124 """This is a subclass of property that defines __slots__"""
127 class PropertySubclassTests(unittest
.TestCase
):
129 def test_docstring_copy(self
):
133 """spam wrapped in property subclass"""
137 "spam wrapped in property subclass")
139 def test_slots_docstring_copy_exception(self
):
144 """Trying to copy this docstring will raise an exception"""
146 except AttributeError:
149 raise Exception("AttributeError not raised")
151 def test_property_setter_copies_getter_docstring(self
):
153 def __init__(self
): self
._spam
= 1
156 """spam wrapped in property subclass"""
159 def spam(self
, value
):
160 """this docstring is ignored"""
163 self
.assertEqual(foo
.spam
, 1)
165 self
.assertEqual(foo
.spam
, 2)
168 "spam wrapped in property subclass")
171 def spam(self
, value
):
172 """another ignored docstring"""
175 self
.assertEqual(foosub
.spam
, 1)
177 self
.assertEqual(foosub
.spam
, 'eggs')
180 "spam wrapped in property subclass")
182 def test_property_new_getter_new_docstring(self
):
191 """a new docstring"""
193 self
.assertEqual(Foo
.spam
.__doc
__, "a new docstring")
194 class FooBase(object):
202 """a new docstring"""
204 self
.assertEqual(Foo
.spam
.__doc
__, "a new docstring")
209 run_unittest(PropertyTests
, PropertySubclassTests
)
211 if __name__
== '__main__':