Skip to content

Commit

Permalink
added method to forcibly clear the cach of a mtime_cached_property
Browse files Browse the repository at this point in the history
  • Loading branch information
tclose committed Sep 4, 2024
1 parent 2ec11a8 commit df5bb4a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
13 changes: 13 additions & 0 deletions fileformats/core/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions fileformats/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down

0 comments on commit df5bb4a

Please sign in to comment.