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

handle FileSkipComment #230

Merged
merged 9 commits into from Oct 31, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Expand Up @@ -33,6 +33,7 @@ Fixed
- Relative paths are now resolved correctly when using the ``--stdout`` option
- Downgrade to Flake8 version 3.x for Pytest compatibility.
See `tholo/pytest-flake8#81`__
- Handle isort file skip comment ``#isort:file_skip`` without an exception

__ https://github.com/tholo/pytest-flake8/issues/81

Expand Down
9 changes: 8 additions & 1 deletion src/darker/import_sorting.py
Expand Up @@ -72,8 +72,15 @@ def apply_isort(
", ".join(f"{k}={v!r}" for k, v in isort_args.items())
)
)

code = content.string
try:
code = isort_code(code=code, **isort_args)
except isort.exceptions.FileSkipComment:
pass

return TextDocument.from_str(
isort_code(code=content.string, **isort_args),
code,
encoding=content.encoding,
mtime=content.mtime,
)
12 changes: 12 additions & 0 deletions src/darker/tests/test_import_sorting.py
Expand Up @@ -96,3 +96,15 @@ def test_isort_config(monkeypatch, tmpdir, line_length, settings_file, expect):
TextDocument.from_str(content), Path("test1.py"), config
)
assert actual.string == expect


def test_isort_file_skip_comment():
"""``apply_isort()`` handles ``FileSkipComment`` exception correctly"""
# Avoid https://github.com/PyCQA/isort/pull/1833 by splitting the skip string
content = "# iso" + "rt:skip_file"

actual = darker.import_sorting.apply_isort(
TextDocument.from_str(content), Path("test1.py")
)

assert actual.string == content