From d4131a5a56210cbd863201351994b536a5788333 Mon Sep 17 00:00:00 2001 From: harshith-coder Date: Mon, 18 May 2026 11:29:19 +0530 Subject: [PATCH 1/2] Fix #2528: Handle isort: off/on comments with and without spaces This fix addresses issue #2528 where isort: off was not properly recognized when written without spaces (e.g., '#isort: off' or '# isort:off'). Changes: - Updated core.py to use flexible string matching for 'isort: off' and 'isort: on' - Now handles: '# isort: off', '#isort: off', and '# isort:off' variations - Added comprehensive regression tests for both import and from import statements - Verified that isort: off honors both plain imports and from imports equally Fixes: #2528 --- isort/core.py | 12 ++- tests/unit/test_action_comments.py | 155 +++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+), 5 deletions(-) diff --git a/isort/core.py b/isort/core.py index 663dd669f..13d7f28cd 100644 --- a/isort/core.py +++ b/isort/core.py @@ -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 @@ -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 = [] @@ -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 diff --git a/tests/unit/test_action_comments.py b/tests/unit/test_action_comments.py index 149639dc4..c5fb63097 100644 --- a/tests/unit/test_action_comments.py +++ b/tests/unit/test_action_comments.py @@ -46,3 +46,158 @@ 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 and from import statements. + + 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 +""" + ) From 5e48ff1268384e895d38825fda6539e1aa4cec68 Mon Sep 17 00:00:00 2001 From: harshith-coder Date: Tue, 19 May 2026 06:43:19 +0530 Subject: [PATCH 2/2] Fix: Adjust test docstring to fit line length requirements --- tests/unit/test_action_comments.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/unit/test_action_comments.py b/tests/unit/test_action_comments.py index c5fb63097..cc09e4243 100644 --- a/tests/unit/test_action_comments.py +++ b/tests/unit/test_action_comments.py @@ -49,10 +49,11 @@ def test_isort_off_and_on(): def test_isort_off_honors_import_and_from_imports_issue_2528(): - """Regression test for issue #2528: ensure isort: off honors both import and from import statements. - - 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. + """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 (