From 32b78cdb062e70bab96f8e679b1688a49338e950 Mon Sep 17 00:00:00 2001 From: Radiumatic <71447191+radiumatic@users.noreply.github.com> Date: Thu, 31 Jul 2025 23:40:45 +0330 Subject: [PATCH] Implement __bool__ for diskcache.Cache to make it Truthy (#1) Python first checks __bool__ and then __len__ to determine whether an object is Truthy. Given the fact that an empty cache returns 0 for the length, it breaks checks that rely on `if cache` instead of `if cache is not None` to detect whether it's initialized. This commit fixes the issue. --- diskcache/core.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/diskcache/core.py b/diskcache/core.py index 7a3d23b..cd75423 100644 --- a/diskcache/core.py +++ b/diskcache/core.py @@ -2364,6 +2364,10 @@ def __len__(self): """Count of items in cache including expired items.""" return self.reset('count') + def __bool__(self): + """Mark Cache objects as Truthy""" + return True + def __getstate__(self): return (self.directory, self.timeout, type(self.disk))