Skip to content

Commit

Permalink
feat: verbose error if Black not found
Browse files Browse the repository at this point in the history
  • Loading branch information
akaihola committed Oct 28, 2024
1 parent 9542309 commit 7820bd6
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/darker/formatters/black_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@
from __future__ import annotations

import logging
import sys

Check failure on line 40 in src/darker/formatters/black_formatter.py

View workflow job for this annotation

GitHub Actions / flake8

'sys' imported but unused

Check failure on line 40 in src/darker/formatters/black_formatter.py

View workflow job for this annotation

GitHub Actions / Pylint

src/darker/formatters/black_formatter.py#L40

Unused import sys (unused-import, W0611)
from typing import TYPE_CHECKING, TypedDict

from darker.exceptions import DependencyError
from darker.files import find_pyproject_toml
from darker.formatters.base_formatter import BaseFormatter
from darkgraylib.command_line import EXIT_CODE_DEPENDENCY

Check failure on line 46 in src/darker/formatters/black_formatter.py

View workflow job for this annotation

GitHub Actions / flake8

'darkgraylib.command_line.EXIT_CODE_DEPENDENCY' imported but unused

Check failure on line 46 in src/darker/formatters/black_formatter.py

View workflow job for this annotation

GitHub Actions / Pylint

src/darker/formatters/black_formatter.py#L46

Unused EXIT_CODE_DEPENDENCY imported from darkgraylib.command_line (unused-import, W0611)
from darkgraylib.config import ConfigurationError
from darkgraylib.utils import TextDocument

Expand Down Expand Up @@ -90,11 +93,26 @@ def read_config(self, src: tuple[str, ...], args: Namespace) -> None:
self._read_cli_args(args)

def _read_config_file(self, config_path: str) -> None: # noqa: C901
# Local import so Darker can be run without Black installed
from black import ( # pylint: disable=import-outside-toplevel
parse_pyproject_toml,
re_compile_maybe_verbose,
)
# Local import so Darker can be run without Black installed.
# Do error handling here. This is the first Black importing method being hit.
try:
from black import ( # pylint: disable=import-outside-toplevel
parse_pyproject_toml,
re_compile_maybe_verbose,
)
except ImportError as exc:
logger.warning(
"To re-format code using Black, install it using e.g."
" `pip install 'darker[black]'` or"
" `pip install black`"
)
logger.warning(
"To use a different formatter or no formatter, select it on the"
" command line (e.g. `--formatter=none`) or configuration"
" (e.g. `formatter=none`)"
)
message = "Can't find the Black package"
raise DependencyError(message) from exc

raw_config = parse_pyproject_toml(config_path)
if "line_length" in raw_config:
Expand Down Expand Up @@ -153,7 +171,8 @@ def run(self, content: TextDocument) -> TextDocument:
:return: The reformatted content
"""
# Local import so Darker can be run without Black installed
# Local import so Darker can be run without Black installed.
# No need for error handling, already done in `BlackFormatter.read_config`.
from black import format_str # pylint: disable=import-outside-toplevel

contents_for_black = content.string_with_newline("\n")
Expand All @@ -177,7 +196,8 @@ def _make_black_options(self) -> Mode:
# pass them to Black's ``format_str()``. File exclusion options aren't needed
# since at this point we already have a single file's content to work on.

# Local import so Darker can be run without Black installed
# Local import so Darker can be run without Black installed.
# No need for error handling, already done in `BlackFormatter.read_config`.
from black import FileMode as Mode # pylint: disable=import-outside-toplevel
from black import TargetVersion # pylint: disable=import-outside-toplevel

Expand Down

0 comments on commit 7820bd6

Please sign in to comment.