@@ -2260,3 +2260,32 @@ def test_noqa_added_to_long_combined_straight_imports_with_bare_comment_issue_20
22602260 to_sort , multi_line_output = 7 , combine_straight_imports = True , line_length = 40
22612261 )
22622262 assert first_pass == ("import a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p # # NOQA\n " )
2263+
2264+
2265+ def test_isort_skip_is_honored_with_future_import_issue_2092 ():
2266+ """A per-line ``isort: skip`` must be honored even when a ``__future__`` import is present.
2267+
2268+ ``__future__`` imports are always floated to the very top, which used to make isort splice
2269+ the sorted import block ahead of a following ``# isort: skip`` line and relocate the skipped
2270+ import below the block - silently violating the skip directive. The skipped import must stay
2271+ exactly where it is, and the result must be stable across re-runs. See issue #2092.
2272+ """
2273+ to_sort = (
2274+ "from __future__ import annotations\n "
2275+ "\n "
2276+ "from foo import bar # isort: skip\n "
2277+ "from bar import baz\n "
2278+ )
2279+
2280+ first_pass = isort .code (to_sort )
2281+ assert first_pass == to_sort
2282+ assert isort .check_code (to_sort , show_diff = True )
2283+
2284+ # An interleaved skip (between two regular imports) must keep its position rather than
2285+ # being sorted to the bottom of the block, and must be idempotent.
2286+ interleaved = "import aaa\n from foo import bar # isort: skip\n import ccc\n "
2287+ sorted_interleaved = isort .code (interleaved )
2288+ lines = sorted_interleaved .splitlines ()
2289+ skip_index = next (i for i , line in enumerate (lines ) if "# isort: skip" in line )
2290+ assert lines .index ("import ccc" ) > skip_index # skip not relocated below the block
2291+ assert isort .code (sorted_interleaved ) == sorted_interleaved
0 commit comments