From 5626713a386b0310f07332ad95514bea1ac79d59 Mon Sep 17 00:00:00 2001 From: simpleqt <89645338+simpleqt@users.noreply.github.com> Date: Sat, 24 Oct 2026 00:33:33 +0800 Subject: [PATCH] Keep two blank lines when a form feed precedes a declaration A whitespace-only form feed line after the import block leaves isort non-idempotent: the first run keeps the two blank lines PEP 8 asks for and hoists the form feed in front of the declaration, the second run no longer sees that line as a declaration and deletes one of the blank lines, so `isort --check` rejects the file isort just wrote. Ignore the hoisted form feed when detecting a statement declaration: form feeds are whitespace that resets the indentation column, so `\fdef f():` is still a top level declaration. Introduced by #2599, whose regression test only covers the non-declaration shape (`...\fprint("!")`), where `lines_after_imports` is 1 on both runs. Found by running every built-in profile twice over the CPython 3.11 stdlib (`Lib/email/*` changes on the second run). --- isort/output.py | 8 +++++++- tests/unit/test_regressions.py | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/isort/output.py b/isort/output.py index 1be048b12..ea22115b5 100644 --- a/isort/output.py +++ b/isort/output.py @@ -173,11 +173,17 @@ def sorted_imports( next_construct = line break + # A form feed may have been hoisted in front of this line from the blank + # lines above it (see ``overflow_form_feeds`` below). Form feeds are + # whitespace that resets the indentation column, so they do not stop the + # line from being a statement declaration. if config.lines_after_imports != -1: lines_after_imports = config.lines_after_imports if config.profile == "black" and extension == "pyi": # special case for black lines_after_imports = 1 - elif extension != "pyi" and next_construct.startswith(STATEMENT_DECLARATIONS): + elif extension != "pyi" and next_construct.lstrip("\f").startswith( + STATEMENT_DECLARATIONS + ): lines_after_imports = 2 else: lines_after_imports = 1 diff --git a/tests/unit/test_regressions.py b/tests/unit/test_regressions.py index 1d21c9c41..5f070f365 100644 --- a/tests/unit/test_regressions.py +++ b/tests/unit/test_regressions.py @@ -252,6 +252,22 @@ def test_form_feed_blank_line_not_removed_issue_2562() -> None: assert isort.code(test_input, lines_after_imports=2) == test_input +@pytest.mark.parametrize( + "declaration", ["def f():\n pass", "class C:\n pass", "async def f():\n pass"] +) +def test_form_feed_hidden_declaration_keeps_two_blank_lines_issue_2562(declaration: str) -> None: + """A form feed hoisted in front of a top level declaration must not hide it. + + ``\\f`` is whitespace and resets the indentation column, so ``\\fdef f():`` is still a + statement declaration: it keeps ``lines_after_imports`` at 2 and the output stays + stable. See: https://github.com/pycqa/isort/issues/2562 + """ + test_input = f"import sys\n\n\n\f\n{declaration}\n" + expected = f"import sys\n\n\n\f{declaration}\n" + assert isort.code(test_input) == expected + assert isort.code(expected) == expected + + def test_force_single_line_shouldnt_remove_preceding_comment_lines_issue_1296() -> None: """Tests to ensure force_single_line setting doesn't result in lost comments. See: https://github.com/pycqa/isort/issues/1296