From 671b1a89d4835f2885c45c344391b8093f223770 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sun, 31 Oct 2021 19:23:54 +0200 Subject: [PATCH 1/9] handle FileSkipComment --- src/darker/import_sorting.py | 10 +++++++++- src/darker/tests/test_import_sorting.py | 13 +++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/darker/import_sorting.py b/src/darker/import_sorting.py index 46f4141db..48c5800c2 100644 --- a/src/darker/import_sorting.py +++ b/src/darker/import_sorting.py @@ -16,6 +16,7 @@ try: import isort + from isort.exceptions import FileSkipComment # Work around Mypy problem # https://github.com/python/mypy/issues/7030#issuecomment-504128883 @@ -72,8 +73,15 @@ def apply_isort( ", ".join(f"{k}={v!r}" for k, v in isort_args.items()) ) ) + + message = "" + try: + message = isort_code(code=content.string, **isort_args) + except FileSkipComment as exception: + message = exception.message + return TextDocument.from_str( - isort_code(code=content.string, **isort_args), + message, 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..b1dc2656c 100644 --- a/src/darker/tests/test_import_sorting.py +++ b/src/darker/tests/test_import_sorting.py @@ -96,3 +96,16 @@ 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""" + # arrange + content = "# isort:skip_file" + + # act + actual = darker.import_sorting.apply_isort( + TextDocument.from_str(content), Path("test1.py") + ) + + # assert + assert actual.string == "Passed in content contains a file skip comment and was skipped." \ No newline at end of file From e3ee858c5ae64f43883a07f3398939be2dba1285 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sun, 31 Oct 2021 19:23:54 +0200 Subject: [PATCH 2/9] just pass --- src/darker/import_sorting.py | 17 +++++++---------- src/darker/tests/test_import_sorting.py | 4 +++- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/darker/import_sorting.py b/src/darker/import_sorting.py index 48c5800c2..3e7dc8f92 100644 --- a/src/darker/import_sorting.py +++ b/src/darker/import_sorting.py @@ -74,14 +74,11 @@ def apply_isort( ) ) - message = "" try: - message = isort_code(code=content.string, **isort_args) - except FileSkipComment as exception: - message = exception.message - - return TextDocument.from_str( - message, - encoding=content.encoding, - mtime=content.mtime, - ) + return TextDocument.from_str( + isort_code(code=content.string, **isort_args), + encoding=content.encoding, + mtime=content.mtime, + ) + except FileSkipComment: + pass diff --git a/src/darker/tests/test_import_sorting.py b/src/darker/tests/test_import_sorting.py index b1dc2656c..621c6e416 100644 --- a/src/darker/tests/test_import_sorting.py +++ b/src/darker/tests/test_import_sorting.py @@ -5,7 +5,9 @@ from textwrap import dedent import pytest +from pytest import raises from black import find_project_root +from isort.exceptions import FileSkipComment import darker.import_sorting from darker.tests.helpers import isort_present @@ -108,4 +110,4 @@ def test_isort_file_skip_comment(): ) # assert - assert actual.string == "Passed in content contains a file skip comment and was skipped." \ No newline at end of file + assert actual == None From e28ad90db827107ae29dcf7c32410eccba42d3ab Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sun, 31 Oct 2021 19:23:54 +0200 Subject: [PATCH 3/9] maybe just tell me what is expected --- src/darker/import_sorting.py | 13 ++++++++----- src/darker/tests/test_import_sorting.py | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/darker/import_sorting.py b/src/darker/import_sorting.py index 3e7dc8f92..77dcda5a0 100644 --- a/src/darker/import_sorting.py +++ b/src/darker/import_sorting.py @@ -74,11 +74,14 @@ def apply_isort( ) ) + code = content.string try: - return TextDocument.from_str( - isort_code(code=content.string, **isort_args), - encoding=content.encoding, - mtime=content.mtime, - ) + code = isort_code(code=code, **isort_args), except FileSkipComment: pass + + return TextDocument.from_str( + code, + encoding=content.encoding, + mtime=content.mtime, + ) \ No newline at end of file diff --git a/src/darker/tests/test_import_sorting.py b/src/darker/tests/test_import_sorting.py index 621c6e416..f71257e3c 100644 --- a/src/darker/tests/test_import_sorting.py +++ b/src/darker/tests/test_import_sorting.py @@ -110,4 +110,4 @@ def test_isort_file_skip_comment(): ) # assert - assert actual == None + assert actual.string == content From 6bbce51b983a500e35fed28432e0d368f4eb0f2f Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sun, 31 Oct 2021 19:23:54 +0200 Subject: [PATCH 4/9] missing line --- src/darker/import_sorting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/darker/import_sorting.py b/src/darker/import_sorting.py index 77dcda5a0..8abd200da 100644 --- a/src/darker/import_sorting.py +++ b/src/darker/import_sorting.py @@ -84,4 +84,4 @@ def apply_isort( code, encoding=content.encoding, mtime=content.mtime, - ) \ No newline at end of file + ) From 84d8a2ef51c2cd25c3d95e44f781322b05211adf Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sun, 31 Oct 2021 19:23:54 +0200 Subject: [PATCH 5/9] add suggestions --- src/darker/import_sorting.py | 5 ++--- src/darker/tests/test_import_sorting.py | 5 ----- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/darker/import_sorting.py b/src/darker/import_sorting.py index 8abd200da..7c7596b61 100644 --- a/src/darker/import_sorting.py +++ b/src/darker/import_sorting.py @@ -16,7 +16,6 @@ try: import isort - from isort.exceptions import FileSkipComment # Work around Mypy problem # https://github.com/python/mypy/issues/7030#issuecomment-504128883 @@ -76,8 +75,8 @@ def apply_isort( code = content.string try: - code = isort_code(code=code, **isort_args), - except FileSkipComment: + code = isort_code(code=code, **isort_args) + except isort.exceptions.FileSkipComment: pass return TextDocument.from_str( diff --git a/src/darker/tests/test_import_sorting.py b/src/darker/tests/test_import_sorting.py index f71257e3c..279a94285 100644 --- a/src/darker/tests/test_import_sorting.py +++ b/src/darker/tests/test_import_sorting.py @@ -5,9 +5,7 @@ from textwrap import dedent import pytest -from pytest import raises from black import find_project_root -from isort.exceptions import FileSkipComment import darker.import_sorting from darker.tests.helpers import isort_present @@ -101,13 +99,10 @@ def test_isort_config(monkeypatch, tmpdir, line_length, settings_file, expect): def test_isort_file_skip_comment(): """``apply_isort()`` handles ``FileSkipComment`` exception correctly""" - # arrange content = "# isort:skip_file" - # act actual = darker.import_sorting.apply_isort( TextDocument.from_str(content), Path("test1.py") ) - # assert assert actual.string == content From 78fd76c1e07008e747c4c9659f61102b7ce7001f Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sun, 31 Oct 2021 19:23:54 +0200 Subject: [PATCH 6/9] add empty line --- src/darker/tests/test_import_sorting.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/darker/tests/test_import_sorting.py b/src/darker/tests/test_import_sorting.py index 279a94285..b614459b6 100644 --- a/src/darker/tests/test_import_sorting.py +++ b/src/darker/tests/test_import_sorting.py @@ -97,6 +97,7 @@ def test_isort_config(monkeypatch, tmpdir, line_length, settings_file, expect): ) assert actual.string == expect + def test_isort_file_skip_comment(): """``apply_isort()`` handles ``FileSkipComment`` exception correctly""" content = "# isort:skip_file" From 47e64deb0c67e28ebac1eeb5b9a2f2fdfef09105 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sun, 31 Oct 2021 19:23:54 +0200 Subject: [PATCH 7/9] Update src/darker/tests/test_import_sorting.py Co-authored-by: Antti Kaihola <13725+akaihola@users.noreply.github.com> --- src/darker/tests/test_import_sorting.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/darker/tests/test_import_sorting.py b/src/darker/tests/test_import_sorting.py index b614459b6..3504f73a6 100644 --- a/src/darker/tests/test_import_sorting.py +++ b/src/darker/tests/test_import_sorting.py @@ -100,7 +100,8 @@ def test_isort_config(monkeypatch, tmpdir, line_length, settings_file, expect): def test_isort_file_skip_comment(): """``apply_isort()`` handles ``FileSkipComment`` exception correctly""" - content = "# isort:skip_file" + # 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") From 2fc878b79b163b436839cf8cf16844717376e5dc Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sun, 31 Oct 2021 19:23:54 +0200 Subject: [PATCH 8/9] Update CHANGES.rst --- CHANGES.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.rst b/CHANGES.rst index 691a1fc4a..726a6ca8d 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 ``FileSkipComment`` to use isort extension with the file skip comment ``#isort:file_skip`` __ https://github.com/tholo/pytest-flake8/issues/81 From 6daf2aa3c7b2823742726c0be64bc8358334745c Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sun, 31 Oct 2021 19:23:54 +0200 Subject: [PATCH 9/9] Update CHANGES.rst --- CHANGES.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 726a6ca8d..db1762355 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -33,7 +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 ``FileSkipComment`` to use isort extension with the file skip comment ``#isort:file_skip`` +- Handle isort file skip comment ``#isort:file_skip`` without an exception __ https://github.com/tholo/pytest-flake8/issues/81