Skip to content

Fix inline comment duplication across merged from X import lines#2499

Merged
DanielNoord merged 2 commits into
mainfrom
copilot/fix-issue-2282
Mar 24, 2026
Merged

Fix inline comment duplication across merged from X import lines#2499
DanielNoord merged 2 commits into
mainfrom
copilot/fix-issue-2282

Conversation

Copilot AI commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

When multiple from foo import statements for the same module are merged and as-imports are present, an inline comment from the first regular-import group leaks into subsequent groups.

# Input
from foo import a, b  # comment
from foo import c as d
from foo import e

# Before fix
from foo import a, b  # comment
from foo import c as d
from foo import e  # comment  ← incorrectly duplicated

# After fix
from foo import a, b  # comment
from foo import c as d
from foo import e

Root cause

In _with_from_imports (output.py), comments is populated once from categorized_comments["from"][module] before the while from_imports: loop. When as-imports split the loop into multiple iterations, comments was never cleared after being consumed in the first non-as-import group, causing it to be re-applied to subsequent groups.

Changes

  • isort/output.py: Add comments = None at the end of the else branch in the while from_imports: loop, after all reformatting logic — mirroring the existing reset already present in the force_single_line branch.
  • tests/unit/test_ticketed_features.py: Add regression test for this case, including idempotency check.

Co-authored-by: DanielNoord <13665637+DanielNoord@users.noreply.github.com>
Agent-Logs-Url: https://github.com/PyCQA/isort/sessions/a161028f-48c2-4abc-a92a-c888c96ad5cf
@DanielNoord DanielNoord enabled auto-merge March 24, 2026 21:50
@codecov

codecov Bot commented Mar 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.16%. Comparing base (52e3a4f) to head (19570ec).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2499   +/-   ##
=======================================
  Coverage   99.16%   99.16%           
=======================================
  Files          40       40           
  Lines        3096     3097    +1     
  Branches      674      674           
=======================================
+ Hits         3070     3071    +1     
  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.

@DanielNoord DanielNoord disabled auto-merge March 24, 2026 21:51
@DanielNoord DanielNoord enabled auto-merge March 24, 2026 21:51
@DanielNoord DanielNoord disabled auto-merge March 24, 2026 21:52
@DanielNoord DanielNoord enabled auto-merge March 24, 2026 21:52
@DanielNoord DanielNoord force-pushed the copilot/fix-issue-2282 branch from 404c66f to 19570ec Compare March 24, 2026 21:54
@DanielNoord DanielNoord disabled auto-merge March 24, 2026 21:55
@DanielNoord DanielNoord enabled auto-merge March 24, 2026 21:55
@DanielNoord DanielNoord added this pull request to the merge queue Mar 24, 2026
Merged via the queue into main with commit 93751b6 Mar 24, 2026
22 of 23 checks passed
@DanielNoord DanielNoord deleted the copilot/fix-issue-2282 branch March 24, 2026 21:58
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.

2 participants