Repository navigation
fix(output): keep two blank lines before a declaration preceded by a form feed - #2745
Merged
DanielNoord merged 1 commit intoOct 25, 2026
Merged
Conversation
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).
Codecov Report✅ All modified and coverable lines are covered by tests. 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:
|
DanielNoord
approved these changes
Oct 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
isort is not idempotent when a whitespace-only form-feed line sits between the import
block and the first top-level declaration, so
isortrejects its own output:In API terms:
Impact measured on real code: on
maina sweep over 584 CPython 3.11 standard-library files× 11 configurations leaves 6 files unstable (
Lib/email/encoders.pyandLib/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:180decides whether the construct after the import block is a declarationwith
next_construct.startswith(STATEMENT_DECLARATIONS). Lines 186-196 hoist the form feedsof overflow trailing blank lines onto that line, so on the second run the line starts with
\u000cand the check fails:lines_after_importsdrops from 2 to 1 and the file changesagain. 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 formfeeds when checking for a declaration:
lstrip("\f")rather thanlstrip(), so an indenteddef/class(whichlstrip()wouldpromote to top level) still is not treated as a declaration.
Bisected to #2599 (
dd46bd79); stable atdd46bd79~1, where the form feed was deletedinstead.
Testing
New regression test in
tests/unit/test_regressions.py(
test_form_feed_hidden_declaration_keeps_two_blank_lines_issue_2562) with adef, aclassand anasync def, asserting both the output and that applying isort to theexpected result is a no-op.
isort/output.pyreverted tomain:3 failed, 1 passed4 passedmain1 failed, 711 passed, 5 skipped; fixed 1 failed, 714 passed,5 skipped — the failure is the pre-existing
test_importable, which also fails onmainand 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.0reports the same 6 pre-existing errors as
main, none in the touched files.