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

Allow to configure isort through pyproject.toml #18

Merged
merged 6 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Feature: Add support for ``-l``/``--line-length`` and ``-S``/``--skip-string-normalization``
- Feature: ``--diff`` outputs a diff for each file on standard output
- Feature: Require ``isort`` >= 5.0.1 and be compatible with it.
- Feature: Allow to configure ``isort`` through pyproject.toml


0.2.0 / 2020-03-11
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ include_trailing_comma = true
force_grid_wrap = 0
use_parentheses = true
line_length = 88
known_third_party = ["pytest"]
akaihola marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ package_dir =
packages = find:
install_requires =
black
typing-extensions ; python_version < "3.8"
python_requires = >=3.6

[options.packages.find]
Expand Down
15 changes: 7 additions & 8 deletions src/darker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import sys
from difflib import unified_diff
from pathlib import Path
from typing import Dict, Iterable, List, Union
from typing import Iterable, List

from darker.black_diff import run_black
from darker.black_diff import BlackArgs, run_black
from darker.chooser import choose_lines
from darker.command_line import ISORT_INSTRUCTION, parse_command_line
from darker.diff import (
Expand All @@ -27,10 +27,7 @@


def format_edited_parts(
srcs: Iterable[Path],
isort: bool,
black_args: Dict[str, Union[bool, int]],
print_diff: bool,
srcs: Iterable[Path], isort: bool, black_args: BlackArgs, print_diff: bool,
) -> None:
"""Black (and optional isort) formatting for chunks with edits since the last commit

Expand Down Expand Up @@ -64,8 +61,10 @@ def format_edited_parts(

# 1. run isort
if isort:
config = black_args.get("config")
line_length = black_args.get("line_length")
edited_srcs = {
src: apply_isort(edited_content)
src: apply_isort(edited_content, src, config, line_length)
for src, edited_content in worktree_srcs.items()
}
else:
Expand Down Expand Up @@ -176,7 +175,7 @@ def main(argv: List[str] = None) -> None:
logger.error(f"{ISORT_INSTRUCTION} to use the `--isort` option.")
exit(1)

black_args = {}
black_args = BlackArgs()
if args.config:
black_args["config"] = args.config
if args.line_length:
Expand Down
33 changes: 23 additions & 10 deletions src/darker/black_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,30 @@
"""

import logging
import sys
from functools import lru_cache
from pathlib import Path
from typing import Dict, List, Optional, Tuple, Union
from typing import List, Optional, cast

if sys.version_info >= (3, 8):
from typing import TypedDict
else:
from typing_extensions import TypedDict

from black import FileMode, format_str, read_pyproject_toml
from click import Command, Context, Option

logger = logging.getLogger(__name__)


class BlackArgs(TypedDict, total=False):
config: str
line_length: int
skip_string_normalization: bool


@lru_cache(maxsize=1)
def read_black_config(src: Path, value: Optional[str]) -> Dict[str, Union[bool, int]]:
def read_black_config(src: Path, value: Optional[str]) -> BlackArgs:
"""Read the black configuration from pyproject.toml"""
command = Command("main")

Expand All @@ -55,16 +67,17 @@ def read_black_config(src: Path, value: Optional[str]) -> Dict[str, Union[bool,

read_pyproject_toml(context, parameter, value)

return {
key: value
for key, value in (context.default_map or {}).items()
if key in ["line_length", "skip_string_normalization"]
}
return cast(
BlackArgs,
{
key: value
for key, value in (context.default_map or {}).items()
if key in ["line_length", "skip_string_normalization"]
},
)


def run_black(
src: Path, src_contents: str, black_args: Dict[str, Union[bool, int]]
) -> List[str]:
def run_black(src: Path, src_contents: str, black_args: BlackArgs) -> List[str]:
"""Run the black formatter for the Python source code given as a string

Return lines of the original file as well as the formatted content.
Expand Down
44 changes: 33 additions & 11 deletions src/darker/import_sorting.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import logging
import sys
from pathlib import Path
from typing import Optional

from black import find_project_root

if sys.version_info >= (3, 8):
from typing import TypedDict
else:
from typing_extensions import TypedDict


try:
import isort
Expand All @@ -8,19 +19,30 @@
logger = logging.getLogger(__name__)


def apply_isort(content: str) -> str:
isort_config_kwargs = dict(
multi_line_output=3,
include_trailing_comma=True,
force_grid_wrap=0,
use_parentheses=True,
line_length=88,
quiet=True,
)
class IsortArgs(TypedDict, total=False):
line_length: int
settings_file: str
settings_path: str


def apply_isort(
content: str,
src: Optional[Path] = None,
config: Optional[str] = None,
line_length: Optional[int] = None,
) -> str:
isort_args = IsortArgs()
if src and not config:
isort_args["settings_path"] = str(find_project_root((str(src),)))
if config:
isort_args["settings_file"] = config
Mystic-Mirage marked this conversation as resolved.
Show resolved Hide resolved
if line_length:
isort_args["line_length"] = line_length

logger.debug(
"isort.code(code=..., {})".format(
", ".join(f"{k}={v!r}" for k, v in isort_config_kwargs.items())
", ".join(f"{k}={v!r}" for k, v in isort_args.items())
)
)
result: str = isort.code(code=content, **isort_config_kwargs)
result: str = isort.code(code=content, **isort_args)
return result
4 changes: 2 additions & 2 deletions src/darker/tests/test_black_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from darker.black_diff import read_black_config, run_black
from darker.black_diff import BlackArgs, read_black_config, run_black


@pytest.mark.parametrize(
Expand Down Expand Up @@ -39,6 +39,6 @@ def test_black_config(tmpdir, config_path, config_lines, expect):
def test_run_black(tmpdir):
src_contents = "print ( '42' )\n"

result = run_black(Path(tmpdir / "src.py"), src_contents, {})
result = run_black(Path(tmpdir / "src.py"), src_contents, BlackArgs())

assert result == ['print("42")']
30 changes: 30 additions & 0 deletions src/darker/tests/test_import_sorting.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from pathlib import Path
from textwrap import dedent

import pytest
Mystic-Mirage marked this conversation as resolved.
Show resolved Hide resolved

from darker.import_sorting import apply_isort

ORIGINAL_SOURCE = "import sys\nimport os\n"
Expand All @@ -8,3 +13,28 @@ def test_apply_isort():
result = apply_isort(ORIGINAL_SOURCE)

assert result == ISORTED_SOURCE


@pytest.mark.parametrize("settings_file", [None, "pyproject.toml"])
@pytest.mark.parametrize("line_length", [20, 60])
def test_isort_config(monkeypatch, tmpdir, line_length, settings_file):
from black import find_project_root

find_project_root.cache_clear()
monkeypatch.chdir(tmpdir)
(tmpdir / 'pyproject.toml').write(
dedent(
f"""\
[tool.isort]
line_length = {line_length}
"""
)
)

content = "from module import ab, cd, ef, gh, ij, kl, mn, op, qr, st, uv, wx, yz"
src = Path(tmpdir / "test1.py") if not settings_file else None
config = str(tmpdir / settings_file) if settings_file else None

actual = apply_isort(content, src, config)
expected = apply_isort(content, line_length=line_length)
assert actual == expected
Mystic-Mirage marked this conversation as resolved.
Show resolved Hide resolved
24 changes: 14 additions & 10 deletions src/darker/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@ def test_isort_option_without_isort(tmpdir, without_isort, caplog):


@pytest.fixture
def run_isort(git_repo, monkeypatch, caplog):
def run_isort(git_repo, monkeypatch, caplog, request):
from black import find_project_root
Mystic-Mirage marked this conversation as resolved.
Show resolved Hide resolved

find_project_root.cache_clear()

monkeypatch.chdir(git_repo.root)
paths = git_repo.add({'test1.py': 'original'}, commit='Initial commit')
paths['test1.py'].write('changed')
args = getattr(request, "param", ())
with patch.multiple(
darker.__main__, run_black=Mock(return_value=[]), verify_ast_unchanged=Mock(),
), patch("darker.import_sorting.isort.code"):
darker.__main__.main(["--isort", "./test1.py"])
darker.__main__.main(["--isort", "./test1.py", *args])
return SimpleNamespace(
isort_code=darker.import_sorting.isort.code, caplog=caplog
)
Expand All @@ -39,15 +44,14 @@ def test_isort_option_with_isort(run_isort):
assert "Please run" not in run_isort.caplog.text


def test_isort_option_with_isort_calls_sortimports(run_isort):
@pytest.mark.parametrize(
"run_isort,isort_args",
Mystic-Mirage marked this conversation as resolved.
Show resolved Hide resolved
[((), {}), (("--line-length", "120"), {"line_length": 120})],
indirect=["run_isort"],
)
def test_isort_option_with_isort_calls_sortimports(tmpdir, run_isort, isort_args):
run_isort.isort_code.assert_called_once_with(
code="changed",
force_grid_wrap=0,
include_trailing_comma=True,
line_length=88,
multi_line_output=3,
use_parentheses=True,
quiet=True,
code="changed", settings_path=str(tmpdir), **isort_args
)


Expand Down