Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Find out more about isort's release policy [here](docs/major_releases/release_po

### Unreleased

- Preserve form feed characters when they appear on otherwise blank lines (#2562)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please remove this, we don't keep the changes here


### 8.0.0 February 19 2026

- Removed `--old-finders` and `--magic-placement` flags and `old_finders` configuration option. The legacy finder logic that relied on environment introspection has been removed (#2445) @joao-faria-dev
Expand Down
10 changes: 7 additions & 3 deletions isort/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ def sorted_imports(

if output:
imports_tail = output_at + len(output)
while [
character.strip() for character in formatted_output[imports_tail : imports_tail + 1]
] == [""]:
while formatted_output[imports_tail : imports_tail + 1] and _is_removable_blank_line(
formatted_output[imports_tail]
):
formatted_output.pop(imports_tail)

if config.lines_before_imports != -1:
Expand Down Expand Up @@ -191,6 +191,10 @@ def sorted_imports(
return _output_as_string(formatted_output, parsed.line_separator)


def _is_removable_blank_line(line: str) -> bool:
return "\f" not in line and line.strip() == ""
Comment on lines +194 to +195

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why can't we use strip() here?



# Ignore DeepSource cyclomatic complexity check for this function.
# skipcq: PY-R1000
def _build_import_group(
Expand Down
7 changes: 4 additions & 3 deletions isort/parse.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Defines parsing functions used by isort for parsing import definitions"""

import re
from collections import OrderedDict, defaultdict
from functools import partial
from itertools import chain
Expand All @@ -19,6 +20,8 @@
from .exceptions import MissingSection
from .settings import DEFAULT_CONFIG, Config

NEWLINE_RE = re.compile(r"\r\n|\r|\n")

if TYPE_CHECKING:
CommentsAboveDict = TypedDict(
"CommentsAboveDict", {"straight": dict[str, list[str]], "from": dict[str, list[str]]}
Expand Down Expand Up @@ -77,9 +80,7 @@ class ParsedContent(NamedTuple):
def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedContent:
"""Parses a python file taking out and categorizing imports."""
line_separator: str = config.line_ending or _infer_line_separator(contents)
in_lines = contents.splitlines()
if contents and contents[-1] in ("\n", "\r"):
in_lines.append("")
in_lines = NEWLINE_RE.split(contents) if contents else []

out_lines = []
original_line_count = len(in_lines)
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ def test_blank_lined_removed_issue_1283():
assert isort.code(test_input) == test_input


def test_form_feed_blank_line_not_removed_issue_2562():
"""Ensure isort preserves form feed as a valid blank line."""
test_input = 'import os\nimport sys\n\n\f\nprint("Hello, world!")\n'
assert isort.code(test_input) == test_input


def test_extra_blank_line_added_nested_imports_issue_1290():
"""Ensure isort doesn't add unnecessary blank lines above nested imports.
See: https://github.com/pycqa/isort/issues/1290
Expand Down
Loading