mirror of
https://github.com/python/cpython.git
synced 2025-11-01 18:51:43 +00:00
Fixed bug #1620: New @spam.getter property syntax modifies the property in place.
I added also the feature that a @prop.getter decorator does not overwrite the doc string of the property if it was given as an argument to property().
This commit is contained in:
parent
52729ac856
commit
90e10e79ea
3 changed files with 171 additions and 33 deletions
98
Lib/test/test_property.py
Normal file
98
Lib/test/test_property.py
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
# Test case for property
|
||||
# more tests are in test_descr
|
||||
|
||||
import unittest
|
||||
from test.test_support import run_unittest
|
||||
|
||||
class PropertyBase(Exception):
|
||||
pass
|
||||
|
||||
class PropertyGet(PropertyBase):
|
||||
pass
|
||||
|
||||
class PropertySet(PropertyBase):
|
||||
pass
|
||||
|
||||
class PropertyDel(PropertyBase):
|
||||
pass
|
||||
|
||||
class BaseClass(object):
|
||||
def __init__(self):
|
||||
self._spam = 5
|
||||
|
||||
@property
|
||||
def spam(self):
|
||||
"""BaseClass.getter"""
|
||||
return self._spam
|
||||
|
||||
@spam.setter
|
||||
def spam(self, value):
|
||||
self._spam = value
|
||||
|
||||
@spam.deleter
|
||||
def spam(self):
|
||||
del self._spam
|
||||
|
||||
class SubClass(BaseClass):
|
||||
|
||||
@BaseClass.spam.getter
|
||||
def spam(self):
|
||||
"""SubClass.getter"""
|
||||
raise PropertyGet(self._spam)
|
||||
|
||||
@spam.setter
|
||||
def spam(self, value):
|
||||
raise PropertySet(self._spam)
|
||||
|
||||
@spam.deleter
|
||||
def spam(self):
|
||||
raise PropertyDel(self._spam)
|
||||
|
||||
class PropertyDocBase(object):
|
||||
_spam = 1
|
||||
def _get_spam(self):
|
||||
return self._spam
|
||||
spam = property(_get_spam, doc="spam spam spam")
|
||||
|
||||
class PropertyDocSub(PropertyDocBase):
|
||||
@PropertyDocBase.spam.getter
|
||||
def spam(self):
|
||||
"""The decorator does not use this doc string"""
|
||||
return self._spam
|
||||
|
||||
class PropertyTests(unittest.TestCase):
|
||||
def test_property_decorator_baseclass(self):
|
||||
# see #1620
|
||||
base = BaseClass()
|
||||
self.assertEqual(base.spam, 5)
|
||||
self.assertEqual(base._spam, 5)
|
||||
base.spam = 10
|
||||
self.assertEqual(base.spam, 10)
|
||||
self.assertEqual(base._spam, 10)
|
||||
delattr(base, "spam")
|
||||
self.assert_(not hasattr(base, "spam"))
|
||||
self.assert_(not hasattr(base, "_spam"))
|
||||
base.spam = 20
|
||||
self.assertEqual(base.spam, 20)
|
||||
self.assertEqual(base._spam, 20)
|
||||
self.assertEqual(base.__class__.spam.__doc__, "BaseClass.getter")
|
||||
|
||||
def test_property_decorator_subclass(self):
|
||||
# see #1620
|
||||
sub = SubClass()
|
||||
self.assertRaises(PropertyGet, getattr, sub, "spam")
|
||||
self.assertRaises(PropertySet, setattr, sub, "spam", None)
|
||||
self.assertRaises(PropertyDel, delattr, sub, "spam")
|
||||
self.assertEqual(sub.__class__.spam.__doc__, "SubClass.getter")
|
||||
|
||||
def test_property_decorator_doc(self):
|
||||
base = PropertyDocBase()
|
||||
sub = PropertyDocSub()
|
||||
self.assertEqual(base.__class__.spam.__doc__, "spam spam spam")
|
||||
self.assertEqual(sub.__class__.spam.__doc__, "spam spam spam")
|
||||
|
||||
def test_main():
|
||||
run_unittest(PropertyTests)
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue