Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide compatibility with Black 24.2 #550

Merged
merged 9 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ jobs:
- '3.10'
- '3.11'
- '3.12-dev'
constraints: ['black==22.12.0']
constraints: ['']
post_install: ['']
include:
- os: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion constraints-oldest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/darker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/darker/black_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down
27 changes: 27 additions & 0 deletions src/darker/files.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/darker/import_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 22 additions & 0 deletions src/darker/tests/test_files.py
Original file line number Diff line number Diff line change
@@ -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
Loading