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 1 commit
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
4 changes: 3 additions & 1 deletion src/darker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,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
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
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