Allow del statement to work for cached properties with ttl
authorGeorge Sakkis <george.sakkis@gmail.com>
Sun, 19 Apr 2015 19:38:14 +0000 (22:38 +0300)
committerGeorge Sakkis <george.sakkis@gmail.com>
Sun, 19 Apr 2015 19:38:14 +0000 (22:38 +0300)
cached_property.py
tests/test_cached_property_ttl.py

index 2d5d50cccebff57e62836ed6f7c8a7de781fa91c..25ffe47e5a3ccec278decf87fe7acfe59bab2624 100644 (file)
@@ -94,6 +94,12 @@ class cached_property_with_ttl(object):
 
         return value
 
+    def __delete__(self, obj):
+        try:
+            del obj._cache[self.__name__]
+        except (KeyError, AttributeError):
+            pass
+
 # Aliases to make cached_property_with_ttl easier to use
 cached_property_ttl = cached_property_with_ttl
 timed_cached_property = cached_property_with_ttl
index 5802516d70851a41bcf87e41f0d5e9b5feee2fed..343ca2e8278c6963544c7f13229999d58916a576 100644 (file)
@@ -71,12 +71,15 @@ class TestCachedPropertyWithTTL(unittest.TestCase):
 
         c = Check()
 
+        # Resetting the cache before it is set is a no-op
+        del c.add_cached
+
         # Run standard cache assertion
         self.assertEqual(c.add_cached, 1)
         self.assertEqual(c.add_cached, 1)
 
         # Reset the cache.
-        del c._cache['add_cached']
+        del c.add_cached
         self.assertEqual(c.add_cached, 2)
         self.assertEqual(c.add_cached, 2)
 
@@ -205,12 +208,15 @@ class TestThreadedCachedPropertyWithTTL(unittest.TestCase):
 
         c = Check()
 
+        # Resetting the cache before it is set is a no-op
+        del c.add_cached
+
         # Run standard cache assertion
         self.assertEqual(c.add_cached, 1)
         self.assertEqual(c.add_cached, 1)
 
         # Reset the cache.
-        del c._cache['add_cached']
+        del c.add_cached
         self.assertEqual(c.add_cached, 2)
         self.assertEqual(c.add_cached, 2)