Skip to content

Commit

Permalink
try this again
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra committed Sep 9, 2023
1 parent b1674e1 commit d08f8ed
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ sources = ["src"]
enable-by-default = false
dependencies = [
"hatch-mypyc>=0.16.0",
"mypy==1.3.0",
"mypy==1.5.1",
"click==8.1.3", # avoid https://github.com/pallets/click/issues/2558
]
require-runtime-dependencies = true
Expand Down
17 changes: 8 additions & 9 deletions src/black/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import tempfile
from dataclasses import dataclass, field
from pathlib import Path
from typing import Dict, Iterable, NamedTuple, Set, Tuple
from typing import Dict, Iterable, Set, Tuple

from platformdirs import user_cache_dir

Expand All @@ -19,10 +19,9 @@
from typing_extensions import Self


class FileData(NamedTuple):
st_mtime: float
st_size: int
hash: str
# We'd like to use a NamedTuple (st_mtime, st_size, hash) here, but
# that breaks mypyc.
FileData = Tuple[float, int, str]


def get_cache_dir() -> Path:
Expand Down Expand Up @@ -86,7 +85,7 @@ def get_file_data(path: Path) -> FileData:

stat = path.stat()
hash = Cache.hash_digest(path)
return FileData(stat.st_mtime, stat.st_size, hash)
return (stat.st_mtime, stat.st_size, hash)

def is_changed(self, source: Path) -> bool:
"""Check if source has changed compared to cached version."""
Expand All @@ -96,11 +95,11 @@ def is_changed(self, source: Path) -> bool:
return True

st = res_src.stat()
if st.st_size != old.st_size:
if st.st_size != old[1]:
return True
if int(st.st_mtime) != int(old.st_mtime):
if int(st.st_mtime) != int(old[0]):
new_hash = Cache.hash_digest(res_src)
if new_hash != old.hash:
if new_hash != old[2]:
return True
return False

Expand Down
2 changes: 1 addition & 1 deletion src/black/handle_ipynb_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def mask_cell(src: str) -> Tuple[str, List[Replacement]]:

from IPython.core.inputtransformer2 import TransformerManager

transformer_manager = TransformerManager()
transformer_manager = TransformerManager() # type: ignore[no-untyped-call]
transformed = transformer_manager.transform_cell(src)
transformed, cell_magic_replacements = replace_cell_magics(transformed)
replacements += cell_magic_replacements
Expand Down
24 changes: 12 additions & 12 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -2063,7 +2063,7 @@ def wrapped_func(path: Path) -> FileData:
if path == cached:
return orig_func(path)
if path == cached_but_changed:
return FileData(0.0, 0, "")
return (0.0, 0, "")
raise AssertionError

with patch.object(black.Cache, "get_file_data", side_effect=wrapped_func):
Expand All @@ -2085,30 +2085,30 @@ def test_filter_cached_hash(self) -> None:
todo, done = cache.filtered_cached([src])
assert todo == set()
assert done == {src}
assert cached_file_data.st_mtime == st.st_mtime
assert cached_file_data[0] == st.st_mtime

# Modify st_mtime
cached_file_data = cache.file_data[str(src)] = FileData(
cached_file_data.st_mtime - 1,
cached_file_data.st_size,
cached_file_data.hash,
cached_file_data = cache.file_data[str(src)] = (
cached_file_data[0] - 1,
cached_file_data[1],
cached_file_data[2],
)
todo, done = cache.filtered_cached([src])
assert todo == set()
assert done == {src}
assert cached_file_data.st_mtime < st.st_mtime
assert cached_file_data.st_size == st.st_size
assert cached_file_data.hash == black.Cache.hash_digest(src)
assert cached_file_data[0] < st.st_mtime
assert cached_file_data[1] == st.st_size
assert cached_file_data[2] == black.Cache.hash_digest(src)

# Modify contents
src.write_text("print('hello world')", encoding="utf-8")
new_st = src.stat()
todo, done = cache.filtered_cached([src])
assert todo == {src}
assert done == set()
assert cached_file_data.st_mtime < new_st.st_mtime
assert cached_file_data.st_size != new_st.st_size
assert cached_file_data.hash != black.Cache.hash_digest(src)
assert cached_file_data[0] < new_st.st_mtime
assert cached_file_data[1] != new_st.st_size
assert cached_file_data[2] != black.Cache.hash_digest(src)

def test_write_cache_creates_directory_if_needed(self) -> None:
mode = DEFAULT_MODE
Expand Down

0 comments on commit d08f8ed

Please sign in to comment.