Fix corrupted output for backslash-continued imports with trailing semicolons#2508
Draft
Fix corrupted output for backslash-continued imports with trailing semicolons#2508
Conversation
Agent-Logs-Url: https://github.com/PyCQA/isort/sessions/2299f790-3e0b-4eb8-8c69-7ddd12b28d5a Co-authored-by: DanielNoord <13665637+DanielNoord@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix corruption of multi-line imports with semicolon
Fix corrupted output for backslash-continued imports with trailing semicolons
Mar 31, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2508 +/- ##
=======================================
Coverage 99.15% 99.15%
=======================================
Files 41 41
Lines 3068 3073 +5
Branches 665 666 +1
=======================================
+ Hits 3042 3047 +5
Misses 14 14
Partials 12 12 🚀 New features to boost your workflow:
|
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.
When a backslash-continued import had a semicolon in the continuation line, isort would corrupt the output by treating the post-semicolon code as additional import names.
Root cause
collect_import_continuationmerges continuation lines wholesale intoimport_string. For a backslash-continued import, this produced"from os import name; print(name)". Sincestrip_syntaxstrips\,(,),,but not;,just_importsended up as['name;', 'printname']— garbage import names.Fix
isort/parse.py: Aftercollect_import_continuationreturns, check whether a semicolon was introduced by the continuation (present inimport_stringbut absent from the originalstatement). If so, output the original lines unchanged viain_lines[statement_index - 1 : index], preserving indentation. This matches the existingskip_line()behaviour for the single-line equivalentfrom os import name; print(name).tests/unit/test_regressions.py: Added regression test.