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 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 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 c70e705b4..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,<24.2 # upper limit until incompatibility fixed + 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,<24.2 # 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 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/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..99086988a --- /dev/null +++ b/src/darker/files.py @@ -0,0 +1,27 @@ +"""Helper functions for working with files and directories.""" + +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, ...]) -> Optional[str]: + """Find the absolute filepath to a pyproject.toml if it exists""" + 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) + + 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 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: diff --git a/src/darker/tests/test_files.py b/src/darker/tests/test_files.py new file mode 100644 index 000000000..227e03f11 --- /dev/null +++ b/src/darker/tests/test_files.py @@ -0,0 +1,22 @@ +"""Test for the `darker.files` module.""" + +import io +from contextlib import redirect_stderr +from pathlib import Path +from unittest.mock import MagicMock, patch + +from darker import files + + +@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 + + 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