Skip to content

Commit

Permalink
Revert "Delay cache read"
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p committed Aug 15, 2023
1 parent 85b4a91 commit 088ea2f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 40 deletions.
3 changes: 1 addition & 2 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,9 +775,8 @@ def reformat_one(
if format_stdin_to_stdout(fast=fast, write_back=write_back, mode=mode):
changed = Changed.YES
else:
cache = Cache(mode)
cache = Cache.read(mode)
if write_back not in (WriteBack.DIFF, WriteBack.COLOR_DIFF):
cache.read()
if not cache.is_changed(src):
changed = Changed.CACHED
if changed is not Changed.CACHED and format_file_in_place(
Expand Down
21 changes: 10 additions & 11 deletions src/black/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,27 @@ def get_cache_file(mode: Mode) -> Path:
@dataclass
class Cache:
mode: Mode
cache_file: Path = field(init=False)
cache_file: Path
file_data: Dict[str, FileData] = field(default_factory=dict)

def __post_init__(self) -> None:
self.cache_file = get_cache_file(self.mode)

def read(self) -> Self:
@classmethod
def read(cls, mode: Mode) -> Self:
"""Read the cache if it exists and is well formed.
If it is not well formed, the call to write later should
resolve the issue.
"""
if not self.cache_file.exists():
return self
cache_file = get_cache_file(mode)
if not cache_file.exists():
return cls(mode, cache_file)

with self.cache_file.open("rb") as fobj:
with cache_file.open("rb") as fobj:
try:
self.file_data = pickle.load(fobj)
file_data: Dict[str, FileData] = pickle.load(fobj)
except (pickle.UnpicklingError, ValueError, IndexError):
pass
return cls(mode, cache_file)

return self
return cls(mode, cache_file, file_data)

@staticmethod
def hash_digest(path: Path) -> str:
Expand Down
3 changes: 1 addition & 2 deletions src/black/concurrency.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,8 @@ async def schedule_formatting(
`write_back`, `fast`, and `mode` options are passed to
:func:`format_file_in_place`.
"""
cache = Cache(mode)
cache = Cache.read(mode)
if write_back not in (WriteBack.DIFF, WriteBack.COLOR_DIFF):
cache.read()
sources, cached = cache.filtered_cached(sources)
for src in sorted(cached):
report.done(src, Changed.CACHED)
Expand Down
50 changes: 25 additions & 25 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -1121,9 +1121,9 @@ def test_single_file_force_pyi(self) -> None:
self.invokeBlack([str(path), "--pyi"])
actual = path.read_text(encoding="utf-8")
# verify cache with --pyi is separate
pyi_cache = black.Cache(pyi_mode).read()
pyi_cache = black.Cache.read(pyi_mode)
assert not pyi_cache.is_changed(path)
normal_cache = black.Cache(DEFAULT_MODE).read()
normal_cache = black.Cache.read(DEFAULT_MODE)
assert normal_cache.is_changed(path)
self.assertFormatEqual(expected, actual)
black.assert_equivalent(contents, actual)
Expand All @@ -1146,8 +1146,8 @@ def test_multi_file_force_pyi(self) -> None:
actual = path.read_text(encoding="utf-8")
self.assertEqual(actual, expected)
# verify cache with --pyi is separate
pyi_cache = black.Cache(pyi_mode).read()
normal_cache = black.Cache(reg_mode).read()
pyi_cache = black.Cache.read(pyi_mode)
normal_cache = black.Cache.read(reg_mode)
for path in paths:
assert not pyi_cache.is_changed(path)
assert normal_cache.is_changed(path)
Expand All @@ -1171,9 +1171,9 @@ def test_single_file_force_py36(self) -> None:
self.invokeBlack([str(path), *PY36_ARGS])
actual = path.read_text(encoding="utf-8")
# verify cache with --target-version is separate
py36_cache = black.Cache(py36_mode).read()
py36_cache = black.Cache.read(py36_mode)
assert not py36_cache.is_changed(path)
normal_cache = black.Cache(reg_mode).read()
normal_cache = black.Cache.read(reg_mode)
assert normal_cache.is_changed(path)
self.assertEqual(actual, expected)

Expand All @@ -1194,8 +1194,8 @@ def test_multi_file_force_py36(self) -> None:
actual = path.read_text(encoding="utf-8")
self.assertEqual(actual, expected)
# verify cache with --target-version is separate
pyi_cache = black.Cache(py36_mode).read()
normal_cache = black.Cache(reg_mode).read()
pyi_cache = black.Cache.read(py36_mode)
normal_cache = black.Cache.read(reg_mode)
for path in paths:
assert not pyi_cache.is_changed(path)
assert normal_cache.is_changed(path)
Expand Down Expand Up @@ -1953,19 +1953,19 @@ def test_cache_broken_file(self) -> None:
with cache_dir() as workspace:
cache_file = get_cache_file(mode)
cache_file.write_text("this is not a pickle", encoding="utf-8")
assert black.Cache(mode).read().file_data == {}
assert black.Cache.read(mode).file_data == {}
src = (workspace / "test.py").resolve()
src.write_text("print('hello')", encoding="utf-8")
invokeBlack([str(src)])
cache = black.Cache(mode).read()
cache = black.Cache.read(mode)
assert not cache.is_changed(src)

def test_cache_single_file_already_cached(self) -> None:
mode = DEFAULT_MODE
with cache_dir() as workspace:
src = (workspace / "test.py").resolve()
src.write_text("print('hello')", encoding="utf-8")
cache = black.Cache(mode).read()
cache = black.Cache.read(mode)
cache.write([src])
invokeBlack([str(src)])
assert src.read_text(encoding="utf-8") == "print('hello')"
Expand All @@ -1980,12 +1980,12 @@ def test_cache_multiple_files(self) -> None:
one.write_text("print('hello')", encoding="utf-8")
two = (workspace / "two.py").resolve()
two.write_text("print('hello')", encoding="utf-8")
cache = black.Cache(mode).read()
cache = black.Cache.read(mode)
cache.write([one])
invokeBlack([str(workspace)])
assert one.read_text(encoding="utf-8") == "print('hello')"
assert two.read_text(encoding="utf-8") == 'print("hello")\n'
cache = black.Cache(mode).read()
cache = black.Cache.read(mode)
assert not cache.is_changed(one)
assert not cache.is_changed(two)

Expand All @@ -2004,8 +2004,8 @@ def test_no_cache_when_writeback_diff(self, color: bool) -> None:
invokeBlack(cmd)
cache_file = get_cache_file(mode)
assert cache_file.exists() is False
read_cache.assert_called_once()
write_cache.assert_not_called()
read_cache.assert_not_called()

@pytest.mark.parametrize("color", [False, True], ids=["no-color", "with-color"])
@event_loop()
Expand Down Expand Up @@ -2038,16 +2038,16 @@ def test_no_cache_when_stdin(self) -> None:
def test_read_cache_no_cachefile(self) -> None:
mode = DEFAULT_MODE
with cache_dir():
assert black.Cache(mode).read().file_data == {}
assert black.Cache.read(mode).file_data == {}

def test_write_cache_read_cache(self) -> None:
mode = DEFAULT_MODE
with cache_dir() as workspace:
src = (workspace / "test.py").resolve()
src.touch()
write_cache = black.Cache(mode).read()
write_cache = black.Cache.read(mode)
write_cache.write([src])
read_cache = black.Cache(mode).read()
read_cache = black.Cache.read(mode)
assert not read_cache.is_changed(src)

def test_filter_cached(self) -> None:
Expand All @@ -2059,7 +2059,7 @@ def test_filter_cached(self) -> None:
uncached.touch()
cached.touch()
cached_but_changed.touch()
cache = black.Cache(DEFAULT_MODE).read()
cache = black.Cache.read(DEFAULT_MODE)

orig_func = black.Cache.get_file_data

Expand All @@ -2082,7 +2082,7 @@ def test_filter_cached_hash(self) -> None:
src = (path / "test.py").resolve()
src.write_text("print('hello')", encoding="utf-8")
st = src.stat()
cache = black.Cache(DEFAULT_MODE).read()
cache = black.Cache.read(DEFAULT_MODE)
cache.write([src])
cached_file_data = cache.file_data[str(src)]

Expand Down Expand Up @@ -2118,7 +2118,7 @@ def test_write_cache_creates_directory_if_needed(self) -> None:
mode = DEFAULT_MODE
with cache_dir(exists=False) as workspace:
assert not workspace.exists()
cache = black.Cache(mode).read()
cache = black.Cache.read(mode)
cache.write([])
assert workspace.exists()

Expand All @@ -2133,14 +2133,14 @@ def test_failed_formatting_does_not_get_cached(self) -> None:
clean = (workspace / "clean.py").resolve()
clean.write_text('print("hello")\n', encoding="utf-8")
invokeBlack([str(workspace)], exit_code=123)
cache = black.Cache(mode).read()
cache = black.Cache.read(mode)
assert cache.is_changed(failing)
assert not cache.is_changed(clean)

def test_write_cache_write_fail(self) -> None:
mode = DEFAULT_MODE
with cache_dir():
cache = black.Cache(mode).read()
cache = black.Cache.read(mode)
with patch.object(Path, "open") as mock:
mock.side_effect = OSError
cache.write([])
Expand All @@ -2151,11 +2151,11 @@ def test_read_cache_line_lengths(self) -> None:
with cache_dir() as workspace:
path = (workspace / "file.py").resolve()
path.touch()
cache = black.Cache(mode).read()
cache = black.Cache.read(mode)
cache.write([path])
one = black.Cache(mode).read()
one = black.Cache.read(mode)
assert not one.is_changed(path)
two = black.Cache(short_mode).read()
two = black.Cache.read(short_mode)
assert two.is_changed(path)


Expand Down

0 comments on commit 088ea2f

Please sign in to comment.