From fa1bdca8520b6cfa052b1c9cf293628e68c185c3 Mon Sep 17 00:00:00 2001 From: Rouven Bauer Date: Sun, 21 Aug 2022 21:27:15 +0200 Subject: [PATCH 1/2] Fix #1964: `lines_before_import` sometimes ignored The config option `lines_before_import` was ignored if the file only contained imports + optionally a leading comment. --- isort/core.py | 2 +- isort/output.py | 7 ++++--- tests/unit/test_isort.py | 13 +++++++------ 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/isort/core.py b/isort/core.py index 9dfaf70f0..771de5928 100644 --- a/isort/core.py +++ b/isort/core.py @@ -359,7 +359,7 @@ def process( if not_imports: if not was_in_quote and config.lines_before_imports > -1: - if line.strip() == "": + if line.strip() == "" and not end_of_file: lines_before += line continue if not import_section: diff --git a/isort/output.py b/isort/output.py index c59be936d..4f8d12b57 100644 --- a/isort/output.py +++ b/isort/output.py @@ -183,6 +183,10 @@ def sorted_imports( ] == [""]: formatted_output.pop(imports_tail) + if config.lines_before_imports != -1: + formatted_output[:0] = ["" for line in range(config.lines_before_imports)] + imports_tail += config.lines_before_imports + if len(formatted_output) > imports_tail: next_construct = "" tail = formatted_output[imports_tail:] @@ -217,9 +221,6 @@ def sorted_imports( else: formatted_output[imports_tail:0] = [""] - if config.lines_before_imports != -1: - formatted_output[:0] = ["" for line in range(config.lines_before_imports)] - if parsed.place_imports: new_out_lines = [] for index, line in enumerate(formatted_output): diff --git a/tests/unit/test_isort.py b/tests/unit/test_isort.py index 73acca0da..1f9786f42 100644 --- a/tests/unit/test_isort.py +++ b/tests/unit/test_isort.py @@ -1714,12 +1714,13 @@ def test_order_by_type() -> None: ) -def test_custom_lines_before_import_section() -> None: - """Test the case where the number of lines to output after imports has been explicitly set.""" - test_input = """from a import b - -foo = 'bar' -""" +@pytest.mark.parametrize("has_body", [True, False]) +# @pytest.mark.parametrize("has_body", [False]) +def test_custom_lines_before_import_section(has_body) -> None: + """Test the case where the number of lines to output before imports has been explicitly set.""" + test_input = "from a import b\n" + if has_body: + test_input += "\nfoo = 'bar'\n" ln = "\n" From 64f7e0d8fab306ed2cb7d585e5b2d9a95c7a83a1 Mon Sep 17 00:00:00 2001 From: Rouven Bauer Date: Sun, 21 Aug 2022 23:09:14 +0200 Subject: [PATCH 2/2] Clean-up: remove forgotten comment --- tests/unit/test_isort.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/test_isort.py b/tests/unit/test_isort.py index 1f9786f42..67b5993d6 100644 --- a/tests/unit/test_isort.py +++ b/tests/unit/test_isort.py @@ -1715,7 +1715,6 @@ def test_order_by_type() -> None: @pytest.mark.parametrize("has_body", [True, False]) -# @pytest.mark.parametrize("has_body", [False]) def test_custom_lines_before_import_section(has_body) -> None: """Test the case where the number of lines to output before imports has been explicitly set.""" test_input = "from a import b\n"