Ensure cachedproperty descriptor picks up docstrings
Closes-Bug: #1325677 Change-Id: I6366dea5458adec30e945a9834e15da946813461
This commit is contained in:
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
import collections
|
import collections
|
||||||
import functools
|
import functools
|
||||||
|
import inspect
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
@@ -370,6 +371,25 @@ class CachedPropertyTest(test.TestCase):
|
|||||||
self.assertRaises(AttributeError, try_set, a)
|
self.assertRaises(AttributeError, try_set, a)
|
||||||
self.assertEqual('b', a.b)
|
self.assertEqual('b', a.b)
|
||||||
|
|
||||||
|
def test_documented_property(self):
|
||||||
|
|
||||||
|
class A(object):
|
||||||
|
@misc.cachedproperty
|
||||||
|
def b(self):
|
||||||
|
"""I like bees."""
|
||||||
|
return 'b'
|
||||||
|
|
||||||
|
self.assertEqual("I like bees.", inspect.getdoc(A.b))
|
||||||
|
|
||||||
|
def test_undocumented_property(self):
|
||||||
|
|
||||||
|
class A(object):
|
||||||
|
@misc.cachedproperty
|
||||||
|
def b(self):
|
||||||
|
return 'b'
|
||||||
|
|
||||||
|
self.assertEqual(None, inspect.getdoc(A.b))
|
||||||
|
|
||||||
|
|
||||||
class AttrDictTest(test.TestCase):
|
class AttrDictTest(test.TestCase):
|
||||||
def test_ok_create(self):
|
def test_ok_create(self):
|
||||||
|
|||||||
@@ -187,15 +187,18 @@ class cachedproperty(object):
|
|||||||
if inspect.isfunction(fget):
|
if inspect.isfunction(fget):
|
||||||
self._fget = fget
|
self._fget = fget
|
||||||
self._attr_name = "_%s" % (fget.__name__)
|
self._attr_name = "_%s" % (fget.__name__)
|
||||||
|
self.__doc__ = getattr(fget, '__doc__', None)
|
||||||
else:
|
else:
|
||||||
self._attr_name = fget
|
self._attr_name = fget
|
||||||
self._fget = None
|
self._fget = None
|
||||||
|
self.__doc__ = None
|
||||||
|
|
||||||
def __call__(self, fget):
|
def __call__(self, fget):
|
||||||
# If __init__ received a string then this will be the function to be
|
# If __init__ received a string then this will be the function to be
|
||||||
# wrapped as a property (if __init__ got a function then this will not
|
# wrapped as a property (if __init__ got a function then this will not
|
||||||
# be called).
|
# be called).
|
||||||
self._fget = fget
|
self._fget = fget
|
||||||
|
self.__doc__ = getattr(fget, '__doc__', None)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __set__(self, instance, value):
|
def __set__(self, instance, value):
|
||||||
@@ -205,6 +208,8 @@ class cachedproperty(object):
|
|||||||
raise AttributeError("can't delete attribute")
|
raise AttributeError("can't delete attribute")
|
||||||
|
|
||||||
def __get__(self, instance, owner):
|
def __get__(self, instance, owner):
|
||||||
|
if instance is None:
|
||||||
|
return self
|
||||||
try:
|
try:
|
||||||
return getattr(instance, self._attr_name)
|
return getattr(instance, self._attr_name)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
|
|||||||
Reference in New Issue
Block a user