diff --git a/CHANGES.rst b/CHANGES.rst index 691a1fc4a..db1762355 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 diff --git a/src/darker/import_sorting.py b/src/darker/import_sorting.py index 46f4141db..7c7596b61 100644 --- a/src/darker/import_sorting.py +++ b/src/darker/import_sorting.py @@ -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, ) diff --git a/src/darker/tests/test_import_sorting.py b/src/darker/tests/test_import_sorting.py index a00d003db..3504f73a6 100644 --- a/src/darker/tests/test_import_sorting.py +++ b/src/darker/tests/test_import_sorting.py @@ -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