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)