From afd7c42696d0dc1e6d1fe241a5101da18598c83e Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Fri, 27 Sep 2024 11:26:19 -0700 Subject: [PATCH 1/4] Include --unstable in cache key Fixes #4465 --- CHANGES.md | 5 +++++ src/black/mode.py | 1 + tests/test_black.py | 32 +++++++++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index a83a4df0023..a58ae2c21ac 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -19,6 +19,11 @@ - Fix type annotation spacing between * and more complex type variable tuple (i.e. `def fn(*args: *tuple[*Ts, T]) -> None: pass`) (#4440) +### Caching + +- Fix bug where the cache was shared between runs with and without + `--unstable` (#4466) + ### Configuration diff --git a/src/black/mode.py b/src/black/mode.py index 43c60049cba..07c32f9a7a6 100644 --- a/src/black/mode.py +++ b/src/black/mode.py @@ -286,6 +286,7 @@ def get_cache_key(self) -> str: str(int(self.skip_source_first_line)), str(int(self.magic_trailing_comma)), str(int(self.preview)), + str(int(self.unstable)), features_and_magics, ] return ".".join(parts) diff --git a/tests/test_black.py b/tests/test_black.py index 6fd725208d2..8aae60bc183 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -12,7 +12,7 @@ import types from concurrent.futures import ThreadPoolExecutor from contextlib import contextmanager, redirect_stderr -from dataclasses import replace +from dataclasses import fields, replace from io import BytesIO from pathlib import Path, WindowsPath from platform import system @@ -2344,6 +2344,36 @@ def test_read_cache_line_lengths(self) -> None: two = black.Cache.read(short_mode) 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( src: Sequence[Union[str, Path]], From d9ff03eea05391059e9916d15cf69c57f9f024c6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 27 Sep 2024 18:27:53 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- CHANGES.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index a58ae2c21ac..1efc6d3d688 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -21,8 +21,7 @@ ### Caching -- Fix bug where the cache was shared between runs with and without - `--unstable` (#4466) +- Fix bug where the cache was shared between runs with and without `--unstable` (#4466) ### Configuration From 807aa9d92625376e2d894cb882e110fd36754bf7 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Fri, 27 Sep 2024 12:47:38 -0700 Subject: [PATCH 3/4] try this --- tests/test_blackd.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_blackd.py b/tests/test_blackd.py index 59703036dc0..b350a29679d 100644 --- a/tests/test_blackd.py +++ b/tests/test_blackd.py @@ -1,3 +1,4 @@ +import gc import re from typing import TYPE_CHECKING, Any, Callable, TypeVar from unittest.mock import patch @@ -32,6 +33,10 @@ def unittest_run_loop(func, *args, **kwargs): @pytest.mark.blackd class BlackDTestCase(AioHTTPTestCase): + def tearDown(self) -> None: + gc.collect() + super().tearDown() + def test_blackd_main(self) -> None: with patch("blackd.web.run_app"): result = CliRunner().invoke(blackd.main, []) From fb3a44d3c45c389e32266b16d01678c1701de668 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Fri, 27 Sep 2024 13:10:07 -0700 Subject: [PATCH 4/4] comment --- tests/test_blackd.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_blackd.py b/tests/test_blackd.py index b350a29679d..741d363e114 100644 --- a/tests/test_blackd.py +++ b/tests/test_blackd.py @@ -34,6 +34,7 @@ def unittest_run_loop(func, *args, **kwargs): @pytest.mark.blackd class BlackDTestCase(AioHTTPTestCase): def tearDown(self) -> None: + # Work around https://github.com/python/cpython/issues/124706 gc.collect() super().tearDown()