From df5bb4a0e34bf23fca8a4af4a7a61bdcc4dda7d1 Mon Sep 17 00:00:00 2001 From: Tom Close Date: Wed, 4 Sep 2024 15:27:02 +1000 Subject: [PATCH] added method to forcibly clear the cach of a mtime_cached_property --- fileformats/core/tests/test_utils.py | 13 +++++++++++++ fileformats/core/utils.py | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/fileformats/core/tests/test_utils.py b/fileformats/core/tests/test_utils.py index 531075c..521f000 100644 --- a/fileformats/core/tests/test_utils.py +++ b/fileformats/core/tests/test_utils.py @@ -391,3 +391,16 @@ def test_mtime_cached_property(tmp_path: Path): time.sleep(2) fspath.write_text("world") assert file.cached_prop == 1 + + +def test_mtime_cached_property_force_clear(tmp_path: Path): + fspath = tmp_path / "file_1.txt" + fspath.write_text("hello") + + file = MtimeTestFile(fspath) + + file.flag = 0 + assert file.cached_prop == 0 + file.flag = 1 + MtimeTestFile.cached_prop.clear(file) + assert file.cached_prop == 1 diff --git a/fileformats/core/utils.py b/fileformats/core/utils.py index 95184d3..45a42d2 100644 --- a/fileformats/core/utils.py +++ b/fileformats/core/utils.py @@ -266,6 +266,10 @@ def __init__(self, func: ty.Callable[..., ty.Any]): def _cache_name(self) -> str: return f"_{self.func.__name__}_mtime_cache" + def clear(self, instance: "fileformats.core.FileSet") -> None: + """Forcibly clear the cache""" + del instance.__dict__[self._cache_name] + def __get__( self, instance: ty.Optional["fileformats.core.FileSet"],