Skip to content

fix(output): keep two blank lines before a declaration preceded by a form feed - #2745

Merged
DanielNoord merged 1 commit into
PyCQA:mainfrom
simpleqt:fix/form-feed-declaration-idempotence
Oct 25, 2026
Merged

DanielNoord merged 1 commit into
PyCQA:mainfrom
simpleqt:fix/form-feed-declaration-idempotence

Conversation

@simpleqt

Copy link
Copy Markdown
Contributor

isort is not idempotent when a whitespace-only form-feed line sits between the import
block and the first top-level declaration, so isort rejects its own output:

$ cat f.py
import sys


<FF>
def f():
    pass

$ isort --profile black f.py && isort --profile black --check f.py
ERROR: .../f.py Imports are incorrectly sorted and/or formatted.   # exit 1

In API terms:

src = 'import sys\n\n\n\x0c\ndef f():\n    pass\n'
isort.code(src)             # 'import sys\n\n\n\x0cdef f():\n    pass\n'   2 blank lines kept
isort.code(isort.code(src)) # 'import sys\n\n\x0cdef f():\n    pass\n'     one silently deleted

Impact measured on real code: on main a sweep over 584 CPython 3.11 standard-library files
× 11 configurations leaves 6 files unstable (Lib/email/encoders.py and Lib/email/mime/*);
with the fix, 0. A wider corpus (1773 files × 8 configurations) goes from 47 unstable to 5,
with no file newly unstable.

Root cause

isort/output.py:180 decides whether the construct after the import block is a declaration
with next_construct.startswith(STATEMENT_DECLARATIONS). Lines 186-196 hoist the form feeds
of overflow trailing blank lines onto that line, so on the second run the line starts with
\u000c and the check fails: lines_after_imports drops from 2 to 1 and the file changes
again. The hoisting itself is intended behaviour (merged #2599 pinned it for the
non-declaration shape, e.g. ...\fprint("!")), so this keeps it and only ignores the form
feeds when checking for a declaration:

if next_construct.lstrip("\f").startswith(STATEMENT_DECLARATIONS):

lstrip("\f") rather than lstrip(), so an indented def/class (which lstrip() would
promote to top level) still is not treated as a declaration.

Bisected to #2599 (dd46bd79); stable at dd46bd79~1, where the form feed was deleted
instead.

Testing

New regression test in tests/unit/test_regressions.py
(test_form_feed_hidden_declaration_keeps_two_blank_lines_issue_2562) with a def, a
class and an async def, asserting both the output and that applying isort to the
expected result is a no-op.

  • with only isort/output.py reverted to main: 3 failed, 1 passed
  • with the fix: 4 passed
  • full unit suite: main 1 failed, 711 passed, 5 skipped; fixed 1 failed, 714 passed,
    5 skipped — the failure is the pre-existing test_importable, which also fails on main
    and passes in isolation

Lint at the versions pinned in uv.lock: flake8 7.4.1, ruff 0.17.1 (All checks passed!),
ruff format --check, bandit 1.9.4, isort --profile hug --check — all clean; mypy 2.4.0
reports the same 6 pre-existing errors as main, none in the touched files.

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 PyCQA#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).
Copilot AI balanced review requested due to automatic review settings October 23, 2026 16:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@codecov

codecov Bot commented Oct 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.38%. Comparing base (a514c71) to head (5626713).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2745   +/-   ##
=======================================
  Coverage   99.38%   99.38%           
=======================================
  Files          40       40           
  Lines        3274     3274           
  Branches      706      706           
=======================================
  Hits         3254     3254           
  Misses         12       12           
  Partials        8        8           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approved

The narrowly scoped fix preserves existing spacing rules, includes targeted regression coverage, and has no unresolved findings.

No open issues 🎉

Review effort: Balanced

@DanielNoord
DanielNoord added this pull request to the merge queue Oct 25, 2026
Merged via the queue into PyCQA:main with commit 4baebff Oct 25, 2026
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants