Skip to content

Commit f1a2f92

Browse files
Include --unstable in cache key (#4466)
Fixes #4465
1 parent 8d9d18c commit f1a2f92

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

CHANGES.md

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
- Fix type annotation spacing between * and more complex type variable tuple (i.e. `def
3131
fn(*args: *tuple[*Ts, T]) -> None: pass`) (#4440)
3232

33+
### Caching
34+
35+
- Fix bug where the cache was shared between runs with and without `--unstable` (#4466)
36+
3337
### Configuration
3438

3539
<!-- Changes to how Black can be configured -->

src/black/mode.py

+1
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ def get_cache_key(self) -> str:
290290
str(int(self.skip_source_first_line)),
291291
str(int(self.magic_trailing_comma)),
292292
str(int(self.preview)),
293+
str(int(self.unstable)),
293294
features_and_magics,
294295
]
295296
return ".".join(parts)

tests/test_black.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import types
1313
from concurrent.futures import ThreadPoolExecutor
1414
from contextlib import contextmanager, redirect_stderr
15-
from dataclasses import replace
15+
from dataclasses import fields, replace
1616
from io import BytesIO
1717
from pathlib import Path, WindowsPath
1818
from platform import system
@@ -2347,6 +2347,36 @@ def test_read_cache_line_lengths(self) -> None:
23472347
two = black.Cache.read(short_mode)
23482348
assert two.is_changed(path)
23492349

2350+
def test_cache_key(self) -> None:
2351+
# Test that all members of the mode enum affect the cache key.
2352+
for field in fields(Mode):
2353+
values: List[Any]
2354+
if field.name == "target_versions":
2355+
values = [
2356+
{TargetVersion.PY312},
2357+
{TargetVersion.PY313},
2358+
]
2359+
elif field.name == "python_cell_magics":
2360+
values = [{"magic1"}, {"magic2"}]
2361+
elif field.name == "enabled_features":
2362+
# If you are looking to remove one of these features, just
2363+
# replace it with any other feature.
2364+
values = [
2365+
{Preview.docstring_check_for_newline},
2366+
{Preview.hex_codes_in_unicode_sequences},
2367+
]
2368+
elif field.type is bool:
2369+
values = [True, False]
2370+
elif field.type is int:
2371+
values = [1, 2]
2372+
else:
2373+
raise AssertionError(
2374+
f"Unhandled field type: {field.type} for field {field.name}"
2375+
)
2376+
modes = [replace(DEFAULT_MODE, **{field.name: value}) for value in values]
2377+
keys = [mode.get_cache_key() for mode in modes]
2378+
assert len(set(keys)) == len(modes)
2379+
23502380

23512381
def assert_collected_sources(
23522382
src: Sequence[Union[str, Path]],

tests/test_blackd.py

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import gc
12
import re
23
from unittest.mock import patch
34

@@ -17,6 +18,11 @@
1718

1819
@pytest.mark.blackd
1920
class BlackDTestCase(AioHTTPTestCase):
21+
def tearDown(self) -> None:
22+
# Work around https://github.com/python/cpython/issues/124706
23+
gc.collect()
24+
super().tearDown()
25+
2026
def test_blackd_main(self) -> None:
2127
with patch("blackd.web.run_app"):
2228
result = CliRunner().invoke(blackd.main, [])

0 commit comments

Comments
 (0)