Skip to content

Commit 300e42f

Browse files
authored
Merge pull request #153 from LilSpazJoekp/fix-cache
Fix import issue when loading cache in certain environments
2 parents 9567618 + 777da12 commit 300e42f

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Unreleased
2929
4. Default of 88
3030
- Image and Unicode substitution definitions were not being formatted correctly.
3131
- Files specified in pyproject are now parsed correctly.
32+
- Fixed issue when loading cache in certain environments.
3233

3334
**Removed**
3435

docstrfmt/util.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def _get_cache_filename(self) -> Path:
8282
/ f"cache.{f'{docstring_trailing_line}_{format_python_code_blocks}_{include_txt}_{line_length}_{mode}'}.pickle"
8383
)
8484

85-
def _read_cache(self) -> dict[Path, tuple[float, int]]:
85+
def _read_cache(self) -> dict[str, tuple[float, int]]:
8686
"""Read the cache file."""
8787
cache_file = self._get_cache_filename()
8888
if not cache_file.exists():
@@ -101,9 +101,12 @@ def gen_todo_list(self, files: list[str]) -> tuple[set[Path], set[Path]]:
101101
"""Generate the list of files to process."""
102102
todo, done = set(), set()
103103
for file in (Path(f).resolve() for f in files):
104-
if self.cache.get(file) != self._get_file_info(file) or self.ignore_cache:
104+
if (
105+
self.cache.get(str(file)) != self._get_file_info(file)
106+
or self.ignore_cache
107+
):
105108
todo.add(file)
106-
else: # pragma: no cover
109+
else:
107110
done.add(file)
108111
return todo, done
109112

@@ -114,7 +117,7 @@ def write_cache(self, files: list[Path]) -> None:
114117
self.cache_dir.mkdir(parents=True, exist_ok=True)
115118
new_cache = {
116119
**self.cache,
117-
**{file.resolve(): self._get_file_info(file) for file in files},
120+
**{str(file.resolve()): self._get_file_info(file) for file in files},
118121
}
119122
with tempfile.NamedTemporaryFile(
120123
dir=str(cache_file.parent), delete=False

tests/test_main.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,26 @@ def test_globbing(runner, file):
675675
assert result.output.endswith("were reformatted.\nDone! 🎉\n")
676676

677677

678+
@pytest.mark.parametrize("file", test_files)
679+
def test_cache(runner, file):
680+
args = [
681+
"-e",
682+
"tests/test_files/error_files/",
683+
"-e",
684+
"tests/test_files/test_encoding.rst",
685+
"-l",
686+
80,
687+
file,
688+
]
689+
result = runner.invoke(main, args=args)
690+
assert result.exit_code == 0
691+
assert result.output.endswith("were reformatted.\nDone! 🎉\n")
692+
693+
result = runner.invoke(main, args=args)
694+
assert result.exit_code == 0
695+
assert result.output.endswith("was checked.\nDone! 🎉\n")
696+
697+
678698
def test_comment_preserve_single_line(runner):
679699
file = ".. A comment in a single line is not placed on the next one.\n"
680700
fixed = ".. A comment in a single line is not placed on the next one.\n"

0 commit comments

Comments
 (0)