Skip to content

Fix # isort: off not preserving preceding import section#2552

Closed
skshmjn wants to merge 1 commit into
PyCQA:mainfrom
skshmjn:fix/issue-2528-isort-off-preserves-imports
Closed

Fix # isort: off not preserving preceding import section#2552
skshmjn wants to merge 1 commit into
PyCQA:mainfrom
skshmjn:fix/issue-2528-isort-off-preserves-imports

Conversation

@skshmjn
Copy link
Copy Markdown

@skshmjn skshmjn commented May 30, 2026

Problem: When # isort: off appears after an import section, isort was still sorting/reformatting/deduplicating the imports before the directive — affecting both import and from ... import statements.

Fix: In core.py, when stripped_line == "# isort: off" triggers the import section flush, write raw_import_section (the original unmodified text) instead of the sorted output.

Test: Updated the existing test_isort_off_and_on assertion to reflect corrected behavior (duplicates before # isort: off are now preserved).

Fixes #2528

When # isort: off triggered flushing the accumulated import section,
the code always sorted/reformatted it before writing. Now it writes
raw_import_section instead, preserving the original ordering,
formatting, and duplicates.

Fixes PyCQA#2528
@DanielNoord
Copy link
Copy Markdown
Member

Fixed in #2553

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.

# isort: off does not honor import statements but does honor import from statements

2 participants