From 6530e5243d5b8ef9f42fc92c5374084728a6569f Mon Sep 17 00:00:00 2001 From: Tatamis <80774326+Tatamis@users.noreply.github.com> Date: Thu, 17 Sep 2026 18:16:58 +0300 Subject: [PATCH] fix: raise AttributeError instead of AssertionError in FanoutCache.__getattr__ hasattr() only treats AttributeError as "attribute not found"; any other exception propagates. FanoutCache.__getattr__ used a bare assert for an unknown attribute name, so hasattr(cache, name) raised instead of returning False for anything outside the known settings, breaking tools that probe attributes with hasattr() (e.g. pympler's asizeof). Fixes #353 --- diskcache/fanout.py | 5 ++++- tests/test_fanout.py | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/diskcache/fanout.py b/diskcache/fanout.py index 9822ee4..bb48f1e 100644 --- a/diskcache/fanout.py +++ b/diskcache/fanout.py @@ -63,7 +63,10 @@ def directory(self): def __getattr__(self, name): safe_names = {'timeout', 'disk'} valid_name = name in DEFAULT_SETTINGS or name in safe_names - assert valid_name, 'cannot access {} in cache shard'.format(name) + if not valid_name: + raise AttributeError( + 'cannot access {} in cache shard'.format(name) + ) return getattr(self._shards[0], name) @cl.contextmanager diff --git a/tests/test_fanout.py b/tests/test_fanout.py index af221b6..9b38a05 100644 --- a/tests/test_fanout.py +++ b/tests/test_fanout.py @@ -45,6 +45,16 @@ def test_init(cache): cache.check() +def test_getattr_invalid_name_raises_attribute_error(cache): + # Regression test: __getattr__ used to raise AssertionError for an + # unknown attribute, which breaks hasattr() (it only treats + # AttributeError as "attribute not found"). + with pytest.raises(AttributeError): + cache.__slots__ + + assert not hasattr(cache, '__slots__') + + def test_init_path(cache): path = pathlib.Path(cache.directory) other = dc.FanoutCache(path)