diff --git a/isort/core.py b/isort/core.py index 46bd77f17..5b10fd141 100644 --- a/isort/core.py +++ b/isort/core.py @@ -30,6 +30,12 @@ "# isort: assignments", ) LITERAL_TYPE_MAPPING = {"(": "tuple", "[": "list", "{": "set"} +SKIP_IMPORT_COMMENTS = ("isort:skip", "isort: skip") + + +def _has_skip_comment(import_statement: str) -> bool: + """Return whether an import statement carries a per-line ``isort: skip`` directive.""" + return any(comment in import_statement for comment in SKIP_IMPORT_COMMENTS) # Ignore DeepSource cyclomatic complexity check for this function. @@ -325,10 +331,17 @@ def process( stripped_line = line.strip().split("#")[0] import_statement += line + # The second clause keeps a per-line ``isort: skip`` import exactly + # where it is: when earlier imports have already been collected into + # the current section, the skipped statement is treated as a section + # boundary so those imports can't be sorted above it. Without it, a + # preceding import (most commonly a ``__future__`` import, which is + # always floated to the top) makes isort splice the sorted block + # ahead of the skipped line and relocate it below the block. See #2092. if ( import_statement.lstrip().startswith("from") and "import" not in import_statement - ): + ) or (contains_imports and _has_skip_comment(import_statement)): line = import_statement not_imports = True else: diff --git a/tests/unit/test_regressions.py b/tests/unit/test_regressions.py index 52b2df052..10e781100 100644 --- a/tests/unit/test_regressions.py +++ b/tests/unit/test_regressions.py @@ -2260,3 +2260,32 @@ def test_noqa_added_to_long_combined_straight_imports_with_bare_comment_issue_20 to_sort, multi_line_output=7, combine_straight_imports=True, line_length=40 ) assert first_pass == ("import a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p # # NOQA\n") + + +def test_isort_skip_is_honored_with_future_import_issue_2092(): + """A per-line ``isort: skip`` must be honored even when a ``__future__`` import is present. + + ``__future__`` imports are always floated to the very top, which used to make isort splice + the sorted import block ahead of a following ``# isort: skip`` line and relocate the skipped + import below the block - silently violating the skip directive. The skipped import must stay + exactly where it is, and the result must be stable across re-runs. See issue #2092. + """ + to_sort = ( + "from __future__ import annotations\n" + "\n" + "from foo import bar # isort: skip\n" + "from bar import baz\n" + ) + + first_pass = isort.code(to_sort) + assert first_pass == to_sort + assert isort.check_code(to_sort, show_diff=True) + + # An interleaved skip (between two regular imports) must keep its position rather than + # being sorted to the bottom of the block, and must be idempotent. + interleaved = "import aaa\nfrom foo import bar # isort: skip\nimport ccc\n" + sorted_interleaved = isort.code(interleaved) + lines = sorted_interleaved.splitlines() + skip_index = next(i for i, line in enumerate(lines) if "# isort: skip" in line) + assert lines.index("import ccc") > skip_index # skip not relocated below the block + assert isort.code(sorted_interleaved) == sorted_interleaved