From 73245e942de97fe1a4a2d968bbde3c168a97e64c Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Wed, 3 Nov 2021 00:27:55 -0700 Subject: [PATCH] Fixes #1838: don't append imports where no imports are present --- isort/core.py | 2 +- tests/unit/test_ticketed_features.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/isort/core.py b/isort/core.py index d91aec5f8..78e0a1c03 100644 --- a/isort/core.py +++ b/isort/core.py @@ -355,7 +355,7 @@ def process( next_import_section = "" if import_section: - if add_imports and not indent: + if add_imports and (contains_imports or not config.append_only) and not indent: import_section = ( line_separator.join(add_imports) + line_separator + import_section ) diff --git a/tests/unit/test_ticketed_features.py b/tests/unit/test_ticketed_features.py index dde08be6c..ca39c0f95 100644 --- a/tests/unit/test_ticketed_features.py +++ b/tests/unit/test_ticketed_features.py @@ -213,6 +213,19 @@ def test_isort_supports_append_only_imports_issue_727(): """ ) + # issue 1838: don't append in middle of class + assert isort.check_code( + '''class C: + """a + + """ + # comment +''', + append_only=True, + add_imports=["from __future__ import annotations"], + show_diff=True, + ) + def test_isort_supports_shared_profiles_issue_970(): """Test to ensure isort provides a way to use shared profiles.