Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'ticket19872' of https://github.com/oinopion/django

  • Loading branch information...
commit ef1e6ef1eb5495b168f9fc642eca235e71e51dde 2 parents 2b48fcc + b88abd6
Honza Král authored February 23, 2013
4  django/utils/functional.py
@@ -39,7 +39,9 @@ class cached_property(object):
39 39
     def __init__(self, func):
40 40
         self.func = func
41 41
 
42  
-    def __get__(self, instance, type):
  42
+    def __get__(self, instance, type=None):
  43
+        if instance is None:
  44
+            return self
43 45
         res = instance.__dict__[self.func.__name__] = self.func(instance)
44 46
         return res
45 47
 
29  tests/regressiontests/utils/functional.py
... ...
@@ -1,5 +1,5 @@
1 1
 from django.utils import unittest
2  
-from django.utils.functional import lazy, lazy_property
  2
+from django.utils.functional import lazy, lazy_property, cached_property
3 3
 
4 4
 
5 5
 class FunctionalTestCase(unittest.TestCase):
@@ -37,3 +37,30 @@ def _get_do(self):
37 37
 
38 38
         self.assertRaises(NotImplementedError, lambda: A().do)
39 39
         self.assertEqual(B().do, 'DO IT')
  40
+
  41
+    def test_cached_property(self):
  42
+        """
  43
+        Test that cached_property caches its value,
  44
+        and that it behaves like a property
  45
+        """
  46
+
  47
+        class A(object):
  48
+
  49
+            @cached_property
  50
+            def value(self):
  51
+                return 1, object()
  52
+
  53
+        a = A()
  54
+
  55
+        # check that it is cached
  56
+        self.assertEqual(a.value, a.value)
  57
+
  58
+        # check that it returns the right thing
  59
+        self.assertEqual(a.value[0], 1)
  60
+
  61
+        # check that state isn't shared between instances
  62
+        a2 = A()
  63
+        self.assertNotEqual(a.value, a2.value)
  64
+
  65
+        # check that it behaves like a property when there's no instance
  66
+        self.assertIsInstance(A.value, cached_property)

0 notes on commit ef1e6ef

Please sign in to comment.
Something went wrong with that request. Please try again.