Skip to content
Closed
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
12 changes: 7 additions & 5 deletions isort/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ def process(
isort_off = False
for line in chain(input_stream, (None,)):
if isort_off and line is not None:
if line == "# isort: on\n":
if "isort: on" in line or "isort:on" in line:
isort_off = False
new_input += line
elif line in ("# isort: split\n", "# isort: off\n", None) or str(line).endswith(
"# isort: split\n"
):
if line == "# isort: off\n":
) or ("isort: off" in str(line) or "isort:off" in str(line)):
if "isort: off" in str(line) or "isort:off" in str(line):
isort_off = True
if current:
before = current
Expand Down Expand Up @@ -177,7 +177,7 @@ def process(
skip_file = True

if not in_quote:
if stripped_line == "# isort: off":
if "isort: off" in stripped_line or "isort:off" in stripped_line:
isort_off = True
elif stripped_line.startswith("# isort: dont-add-imports"):
add_imports = []
Expand Down Expand Up @@ -233,7 +233,9 @@ def process(
not_imports = bool(in_quote) or was_in_quote or in_top_comment or isort_off
if not (in_quote or was_in_quote or in_top_comment):
if isort_off:
if not skip_file and stripped_line == "# isort: on":
if not skip_file and (
"isort: on" in stripped_line or "isort:on" in stripped_line
):
isort_off = False
elif stripped_line.endswith("# isort: split"):
not_imports = True
Expand Down
156 changes: 156 additions & 0 deletions tests/unit/test_action_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,159 @@ def test_isort_off_and_on():
import a
"""
)


def test_isort_off_honors_import_and_from_imports_issue_2528():
"""Regression test for issue #2528: ensure isort: off honors both import types.

Previously, there was a differential behavior where isort: off would respect
from imports but not plain import statements. This test ensures both import
types are properly handled.
"""
# Test plain imports with isort: off
assert (
isort.code(
"""# isort: off
import z
import a
import b

# isort: on
import c
import d
"""
)
== """# isort: off
import z
import a
import b

# isort: on
import c
import d
"""
)

# Test from imports with isort: off
assert (
isort.code(
"""# isort: off
from z import foo
from a import bar
from b import baz

# isort: on
from c import qux
from d import quux
"""
)
== """# isort: off
from z import foo
from a import bar
from b import baz

# isort: on
from c import qux
from d import quux
"""
)

# Test mixed imports with isort: off - both types should be preserved
assert (
isort.code(
"""# isort: off
import z
from a import foo
import b
from c import bar

# isort: on
import e
from f import baz
"""
)
== """# isort: off
import z
from a import foo
import b
from c import bar

# isort: on
import e
from f import baz
"""
)

# Test when isort: off appears mid-file between imports
assert (
isort.code(
"""import a
import c

# isort: off
import z
from b import foo
import b

# isort: on
import e
from f import baz
"""
)
== """import a
import c

# isort: off
import z
from b import foo
import b

# isort: on
import e
from f import baz
"""
)

# Test isort: off without space after # (e.g., #isort: off)
assert (
isort.code(
"""#isort: off
import z
import a
import b

#isort: on
import c
import d
"""
)
== """#isort: off
import z
import a
import b

#isort: on
import c
import d
"""
)

# Test isort: off without space after isort (e.g., # isort:off)
assert (
isort.code(
"""# isort:off
import z
from a import foo

# isort:on
import b
"""
)
== """# isort:off
import z
from a import foo

# isort:on
import b
"""
)