From 6dfcdece430112596b7528a311d316b9723abac0 Mon Sep 17 00:00:00 2001 From: George Sakkis Date: Sun, 19 Apr 2015 22:38:14 +0300 Subject: [PATCH] Allow del statement to work for cached properties with ttl --- cached_property.py | 6 ++++++ tests/test_cached_property_ttl.py | 10 ++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/cached_property.py b/cached_property.py index 2d5d50c..25ffe47 100644 --- a/cached_property.py +++ b/cached_property.py @@ -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 diff --git a/tests/test_cached_property_ttl.py b/tests/test_cached_property_ttl.py index 5802516..343ca2e 100644 --- a/tests/test_cached_property_ttl.py +++ b/tests/test_cached_property_ttl.py @@ -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) -- 2.30.2