From 5181d0421351d19265d3195576cf9a6d5cb5b55f Mon Sep 17 00:00:00 2001 From: Antti Kaihola <13725+akaihola@users.noreply.github.com> Date: Fri, 8 Mar 2024 12:35:56 +0200 Subject: [PATCH 1/9] Local implementation of `find_pyproject_toml()` This ensures we use `find_project_root()` from Darkgraylib, avoiding incompatibilities with different Black versions. --- src/darker/black_diff.py | 2 +- src/darker/files.py | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 src/darker/files.py diff --git a/src/darker/black_diff.py b/src/darker/black_diff.py index 8bf5a0c04..dbb9ed644 100644 --- a/src/darker/black_diff.py +++ b/src/darker/black_diff.py @@ -41,7 +41,6 @@ from black import FileMode as Mode from black import ( TargetVersion, - find_pyproject_toml, format_str, parse_pyproject_toml, re_compile_maybe_verbose, @@ -53,6 +52,7 @@ from black.files import gen_python_files from black.report import Report +from darker.files import find_pyproject_toml from darkgraylib.config import ConfigurationError from darkgraylib.utils import TextDocument diff --git a/src/darker/files.py b/src/darker/files.py new file mode 100644 index 000000000..ea42418a5 --- /dev/null +++ b/src/darker/files.py @@ -0,0 +1,27 @@ +from typing import Optional, Tuple + +from black import err, find_user_pyproject_toml + +from darkgraylib.files import find_project_root + + +def find_pyproject_toml( + path_search_start: Tuple[str, ...], stdin_filename: Optional[str] = None +) -> Optional[str]: + """Find the absolute filepath to a pyproject.toml if it exists""" + path_project_root, _ = find_project_root(path_search_start, stdin_filename) + path_pyproject_toml = path_project_root / "pyproject.toml" + if path_pyproject_toml.is_file(): + return str(path_pyproject_toml) + + try: + path_user_pyproject_toml = find_user_pyproject_toml() + return ( + str(path_user_pyproject_toml) + if path_user_pyproject_toml.is_file() + else None + ) + except (PermissionError, RuntimeError) as e: + # We do not have access to the user-level config directory, so ignore it. + err(f"Ignoring user configuration directory due to {e!r}") + return None From b1e4435882caa93e179318bc4d976995a0d2d70c Mon Sep 17 00:00:00 2001 From: Antti Kaihola <13725+akaihola@users.noreply.github.com> Date: Fri, 8 Mar 2024 12:44:53 +0200 Subject: [PATCH 2/9] Adapt to new `darkgraylib.find_project_root()` - needs hashable input sequence of paths - doesn't return method of finding the root --- src/darker/__main__.py | 4 ++-- src/darker/files.py | 6 ++---- src/darker/import_sorting.py | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/darker/__main__.py b/src/darker/__main__.py index a894cfc39..3af26d35a 100644 --- a/src/darker/__main__.py +++ b/src/darker/__main__.py @@ -34,8 +34,8 @@ from darker.import_sorting import apply_isort, isort from darker.utils import debug_dump, glob_any from darker.verification import ASTVerifier, BinarySearch, NotEquivalentError -from darkgraylib.black_compat import find_project_root from darkgraylib.config import show_config_if_debug +from darkgraylib.files import find_project_root from darkgraylib.git import ( PRE_COMMIT_FROM_TO_REFS, STDIN, @@ -564,7 +564,7 @@ def main( # pylint: disable=too-many-locals,too-many-branches,too-many-statemen # In other modes, only reformat files which have been modified. if git_is_repository(root): # Get the modified files only. - repo_root = find_project_root([str(root)]) + repo_root = find_project_root((str(root),)) changed_files = { (repo_root / file).relative_to(root) for file in git_get_modified_python_files(paths, revrange, repo_root) diff --git a/src/darker/files.py b/src/darker/files.py index ea42418a5..cc00f7fec 100644 --- a/src/darker/files.py +++ b/src/darker/files.py @@ -5,11 +5,9 @@ from darkgraylib.files import find_project_root -def find_pyproject_toml( - path_search_start: Tuple[str, ...], stdin_filename: Optional[str] = None -) -> Optional[str]: +def find_pyproject_toml(path_search_start: Tuple[str, ...]) -> Optional[str]: """Find the absolute filepath to a pyproject.toml if it exists""" - path_project_root, _ = find_project_root(path_search_start, stdin_filename) + path_project_root = find_project_root(path_search_start) path_pyproject_toml = path_project_root / "pyproject.toml" if path_pyproject_toml.is_file(): return str(path_pyproject_toml) diff --git a/src/darker/import_sorting.py b/src/darker/import_sorting.py index 7239dd3ec..bcf284a60 100644 --- a/src/darker/import_sorting.py +++ b/src/darker/import_sorting.py @@ -8,7 +8,7 @@ from darker.exceptions import IncompatiblePackageError, MissingPackageError from darker.git import EditedLinenumsDiffer from darker.utils import glob_any -from darkgraylib.black_compat import find_project_root +from darkgraylib.files import find_project_root from darkgraylib.utils import DiffChunk, TextDocument try: From 9834caf71d3efd2cbc619ce5d4f29d0f466305e3 Mon Sep 17 00:00:00 2001 From: Antti Kaihola <13725+akaihola@users.noreply.github.com> Date: Fri, 8 Mar 2024 13:00:09 +0200 Subject: [PATCH 3/9] Update the change log --- CHANGES.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 9142dab5b..0ab6c49c0 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -8,6 +8,8 @@ Added Fixed ----- +- Black 24.2 compatibility by using the new `darkgraylib.files.find_project_root` + instead of the implementation in Black. 1.7.3_ - 2024-02-27 From 217cd54cb53ef256d10090ed1f3d6fb953ee50fe Mon Sep 17 00:00:00 2001 From: Antti Kaihola <13725+akaihola@users.noreply.github.com> Date: Fri, 8 Mar 2024 17:14:00 +0200 Subject: [PATCH 4/9] Copy Black unit tests for `find_pyproject_toml()` --- src/darker/tests/test_files.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/darker/tests/test_files.py diff --git a/src/darker/tests/test_files.py b/src/darker/tests/test_files.py new file mode 100644 index 000000000..f6851f6a4 --- /dev/null +++ b/src/darker/tests/test_files.py @@ -0,0 +1,22 @@ +import io +from contextlib import redirect_stderr +from pathlib import Path +from unittest import TestCase +from unittest.mock import MagicMock, patch + +from darker import files + + +class BlackTestCase(TestCase): + @patch("darker.files.find_user_pyproject_toml") + def test_find_pyproject_toml(self, find_user_pyproject_toml: MagicMock) -> None: + find_user_pyproject_toml.side_effect = RuntimeError() + + with redirect_stderr(io.StringIO()) as stderr: + result = files.find_pyproject_toml( + path_search_start=(str(Path.cwd().root),) + ) + + assert result is None + err = stderr.getvalue() + assert "Ignoring user configuration" in err From 3f6f5c0782e95a3e098fb7c8648a86914f7f3a2d Mon Sep 17 00:00:00 2001 From: Antti Kaihola <13725+akaihola@users.noreply.github.com> Date: Fri, 8 Mar 2024 17:18:42 +0200 Subject: [PATCH 5/9] Add module docstrings --- src/darker/files.py | 2 ++ src/darker/tests/test_files.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/darker/files.py b/src/darker/files.py index cc00f7fec..99086988a 100644 --- a/src/darker/files.py +++ b/src/darker/files.py @@ -1,3 +1,5 @@ +"""Helper functions for working with files and directories.""" + from typing import Optional, Tuple from black import err, find_user_pyproject_toml diff --git a/src/darker/tests/test_files.py b/src/darker/tests/test_files.py index f6851f6a4..b68752e1a 100644 --- a/src/darker/tests/test_files.py +++ b/src/darker/tests/test_files.py @@ -1,3 +1,5 @@ +"""Test for the `darker.files` module.""" + import io from contextlib import redirect_stderr from pathlib import Path From 02c49bf06687674632f8943187d6a52d3f6f2f3f Mon Sep 17 00:00:00 2001 From: Antti Kaihola <13725+akaihola@users.noreply.github.com> Date: Fri, 8 Mar 2024 17:18:52 +0200 Subject: [PATCH 6/9] Turn `TestCase` into a Pytest style test function --- src/darker/tests/test_files.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/darker/tests/test_files.py b/src/darker/tests/test_files.py index b68752e1a..227e03f11 100644 --- a/src/darker/tests/test_files.py +++ b/src/darker/tests/test_files.py @@ -3,22 +3,20 @@ import io from contextlib import redirect_stderr from pathlib import Path -from unittest import TestCase from unittest.mock import MagicMock, patch from darker import files -class BlackTestCase(TestCase): - @patch("darker.files.find_user_pyproject_toml") - def test_find_pyproject_toml(self, find_user_pyproject_toml: MagicMock) -> None: - find_user_pyproject_toml.side_effect = RuntimeError() +@patch("darker.files.find_user_pyproject_toml") +def test_find_pyproject_toml(find_user_pyproject_toml: MagicMock) -> None: + """Test `files.find_pyproject_toml` with no user home directory.""" + find_user_pyproject_toml.side_effect = RuntimeError() + with redirect_stderr(io.StringIO()) as stderr: + # end of test setup - with redirect_stderr(io.StringIO()) as stderr: - result = files.find_pyproject_toml( - path_search_start=(str(Path.cwd().root),) - ) + result = files.find_pyproject_toml(path_search_start=(str(Path.cwd().root),)) - assert result is None - err = stderr.getvalue() - assert "Ignoring user configuration" in err + assert result is None + err = stderr.getvalue() + assert "Ignoring user configuration" in err From b26bdb9e51c0d75ae281b8fdbf4b4a8f0475ea12 Mon Sep 17 00:00:00 2001 From: Antti Kaihola <13725+akaihola@users.noreply.github.com> Date: Fri, 8 Mar 2024 17:39:46 +0200 Subject: [PATCH 7/9] Revert blocking of Black 24.2 upgrade --- setup.cfg | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.cfg b/setup.cfg index c70e705b4..258ffb905 100644 --- a/setup.cfg +++ b/setup.cfg @@ -27,7 +27,7 @@ package_dir = packages = find: install_requires = # NOTE: remember to keep `constraints-oldest.txt` in sync with these - black>=21.5b1,<24.2 # upper limit until incompatibility fixed + black>=21.5b1 darkgraylib @ git+https://github.com/akaihola/darkgraylib.git@main graylint @ git+https://github.com/akaihola/graylint.git@main toml>=0.10.0 @@ -52,7 +52,7 @@ color = test = # NOTE: remember to keep `constraints-oldest.txt` in sync with these airium>=0.2.3 - black>=21.7b1,<24.2 # prevent Mypy error about `gen_python_files`, see issue #189 + black>=21.7b1 # to prevent Mypy error about `gen_python_files`, see issue #189 cryptography>=3.3.2 # through twine, fixes CVE-2020-36242 defusedxml>=0.7.1 flynt>=0.76,<0.78 From 9589768dc4c74533ea49941f519f54b7b29640a9 Mon Sep 17 00:00:00 2001 From: Antti Kaihola <13725+akaihola@users.noreply.github.com> Date: Fri, 8 Mar 2024 18:12:49 +0200 Subject: [PATCH 8/9] Require black>=22.3.0 This simplifies our local implementation of `find_pyproject_toml()`. --- constraints-oldest.txt | 2 +- setup.cfg | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/constraints-oldest.txt b/constraints-oldest.txt index ef36c8431..051607bfb 100644 --- a/constraints-oldest.txt +++ b/constraints-oldest.txt @@ -4,7 +4,7 @@ # interpreter and Python ependencies. Keep this up-to-date with minimum # versions in `setup.cfg`. airium==0.2.3 -black==21.8b0 +black==22.3.0 defusedxml==0.7.1 flake8-2020==1.6.1 flake8-bugbear==22.1.11 diff --git a/setup.cfg b/setup.cfg index 258ffb905..1641d6fbd 100644 --- a/setup.cfg +++ b/setup.cfg @@ -27,7 +27,7 @@ package_dir = packages = find: install_requires = # NOTE: remember to keep `constraints-oldest.txt` in sync with these - black>=21.5b1 + black>=22.3.0 darkgraylib @ git+https://github.com/akaihola/darkgraylib.git@main graylint @ git+https://github.com/akaihola/graylint.git@main toml>=0.10.0 @@ -52,7 +52,7 @@ color = test = # NOTE: remember to keep `constraints-oldest.txt` in sync with these airium>=0.2.3 - black>=21.7b1 # to prevent Mypy error about `gen_python_files`, see issue #189 + black>=22.3.0 cryptography>=3.3.2 # through twine, fixes CVE-2020-36242 defusedxml>=0.7.1 flynt>=0.76,<0.78 From ef616b08a2bd9a014e04601da8466965c3b4b2a9 Mon Sep 17 00:00:00 2001 From: Antti Kaihola <13725+akaihola@users.noreply.github.com> Date: Fri, 8 Mar 2024 18:14:10 +0200 Subject: [PATCH 9/9] Test with newest Black --- .github/workflows/python-package.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 6f18d75a2..dd42c4718 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -91,7 +91,7 @@ jobs: - '3.10' - '3.11' - '3.12-dev' - constraints: ['black==22.12.0'] + constraints: [''] post_install: [''] include: - os: ubuntu-latest