Include --unstable in cache key (#4466)

Fixes #4465
This commit is contained in:
Jelle Zijlstra 2024-09-27 13:41:29 -07:00 committed by GitHub
parent 8d9d18c033
commit f1a2f92bba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 42 additions and 1 deletions

View File

@ -30,6 +30,10 @@
- Fix type annotation spacing between * and more complex type variable tuple (i.e. `def - Fix type annotation spacing between * and more complex type variable tuple (i.e. `def
fn(*args: *tuple[*Ts, T]) -> None: pass`) (#4440) fn(*args: *tuple[*Ts, T]) -> None: pass`) (#4440)
### Caching
- Fix bug where the cache was shared between runs with and without `--unstable` (#4466)
### Configuration ### Configuration
<!-- Changes to how Black can be configured --> <!-- Changes to how Black can be configured -->

View File

@ -290,6 +290,7 @@ def get_cache_key(self) -> str:
str(int(self.skip_source_first_line)), str(int(self.skip_source_first_line)),
str(int(self.magic_trailing_comma)), str(int(self.magic_trailing_comma)),
str(int(self.preview)), str(int(self.preview)),
str(int(self.unstable)),
features_and_magics, features_and_magics,
] ]
return ".".join(parts) return ".".join(parts)

View File

@ -12,7 +12,7 @@
import types import types
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
from contextlib import contextmanager, redirect_stderr from contextlib import contextmanager, redirect_stderr
from dataclasses import replace from dataclasses import fields, replace
from io import BytesIO from io import BytesIO
from pathlib import Path, WindowsPath from pathlib import Path, WindowsPath
from platform import system from platform import system
@ -2347,6 +2347,36 @@ def test_read_cache_line_lengths(self) -> None:
two = black.Cache.read(short_mode) two = black.Cache.read(short_mode)
assert two.is_changed(path) assert two.is_changed(path)
def test_cache_key(self) -> None:
# Test that all members of the mode enum affect the cache key.
for field in fields(Mode):
values: List[Any]
if field.name == "target_versions":
values = [
{TargetVersion.PY312},
{TargetVersion.PY313},
]
elif field.name == "python_cell_magics":
values = [{"magic1"}, {"magic2"}]
elif field.name == "enabled_features":
# If you are looking to remove one of these features, just
# replace it with any other feature.
values = [
{Preview.docstring_check_for_newline},
{Preview.hex_codes_in_unicode_sequences},
]
elif field.type is bool:
values = [True, False]
elif field.type is int:
values = [1, 2]
else:
raise AssertionError(
f"Unhandled field type: {field.type} for field {field.name}"
)
modes = [replace(DEFAULT_MODE, **{field.name: value}) for value in values]
keys = [mode.get_cache_key() for mode in modes]
assert len(set(keys)) == len(modes)
def assert_collected_sources( def assert_collected_sources(
src: Sequence[Union[str, Path]], src: Sequence[Union[str, Path]],

View File

@ -1,3 +1,4 @@
import gc
import re import re
from unittest.mock import patch from unittest.mock import patch
@ -17,6 +18,11 @@
@pytest.mark.blackd @pytest.mark.blackd
class BlackDTestCase(AioHTTPTestCase): class BlackDTestCase(AioHTTPTestCase):
def tearDown(self) -> None:
# Work around https://github.com/python/cpython/issues/124706
gc.collect()
super().tearDown()
def test_blackd_main(self) -> None: def test_blackd_main(self) -> None:
with patch("blackd.web.run_app"): with patch("blackd.web.run_app"):
result = CliRunner().invoke(blackd.main, []) result = CliRunner().invoke(blackd.main, [])