Skip to content

Fix corrupted output for backslash-continued imports with trailing semicolons#2508

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-input-semi-colon-issue
Draft

Fix corrupted output for backslash-continued imports with trailing semicolons#2508
Copilot wants to merge 2 commits intomainfrom
copilot/fix-input-semi-colon-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 31, 2026

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.

# Input
from os import \
    name; print(name)

# Before: corrupted output
from os import name, name;, print

# After: left as-is (mirrors skip_line() behavior for single-line semicolons)
from os import \
    name; print(name)

Root cause

collect_import_continuation merges continuation lines wholesale into import_string. For a backslash-continued import, this produced "from os import name; print(name)". Since strip_syntax strips \, (, ), , but not ;, just_imports ended up as ['name;', 'printname'] — garbage import names.

Fix

  • isort/parse.py: After collect_import_continuation returns, check whether a semicolon was introduced by the continuation (present in import_string but absent from the original statement). If so, output the original lines unchanged via in_lines[statement_index - 1 : index], preserving indentation. This matches the existing skip_line() behaviour for the single-line equivalent from os import name; print(name).
  • tests/unit/test_regressions.py: Added regression test.

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
Copilot AI requested a review from DanielNoord March 31, 2026 19:22
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.15%. Comparing base (5382895) to head (4223059).
⚠️ Report is 6 commits behind head on main.

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

Input with semicolon after multi-line import is corrupted

2 participants