Skip to content

Fix opening-line comment moving to alias attribute line on wrapped imports#2491

Merged
DanielNoord merged 8 commits intomainfrom
copilot/fix-comments-moving-bug
Mar 12, 2026
Merged

Fix opening-line comment moving to alias attribute line on wrapped imports#2491
DanielNoord merged 8 commits intomainfrom
copilot/fix-comments-moving-bug

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 12, 2026

When a from … import with an alias is too long for one line and the opening import ( line carries a comment (e.g. # type: ignore[attr-defined]), isort was incorrectly moving that comment inside the parentheses onto the alias line — breaking mypy and similar tools that require the comment on the statement's first physical line.

# Input
from a_long_name_to_enforce.splitting_across.two_lines import (  # type: ignore[attr-defined]
    a_random_attribute as renamed_random_attribute,
)

# Bug output (black profile)
from a_long_name_to_enforce.splitting_across.two_lines import (
    a_random_attribute as renamed_random_attribute,  # type: ignore[attr-defined]
)

Root cause

In output.py, the alias loop passed all from_comments (opening-line comments from categorized_comments["straight"]) through with_comments() into the attribute import string, then called wrap.line(). Because wrap.line() places comments at the end of the last continuation line, the comment ended up inside the parentheses on the alias attribute line.

Changes

  • isort/output.py — In the alias import loop, snapshot opening_line_comments (from categorized_comments["straight"]) before the attribute-level specific_comment (from categorized_comments["nested"]) is appended. When use_parentheses=True and wrapping produces multiple lines, inject the opening-line comments onto the first import ( line instead of embedding them in the attribute string. Attribute-line comments are unaffected. Added detailed inline documentation explaining the two comment categories and the placement strategy.

  • tests/unit/test_regressions.py — Expanded the regression test to cover: attribute-line comment staying on the alias line, both comment types coexisting, non-alias opening-line comments, short imports collapsing to one line, and non-Black wrap modes (multi_line_output=3, use_parentheses=True).

  • pyproject.toml / example_isort_formatting_plugin/pyproject.toml — Reverted unnecessary black minimum-version bumps back to their original upstream values (>=25.9.0 and >=24.3.0 respectively).

Original prompt

This section details on the original issue you should resolve

<issue_title>Comments moving bug when using import aliases</issue_title>
<issue_description>I'm seeing a bug(?) where if a from import has an alias, is too long for a single line, and has a comment after the import (for instance a mypy comment) then the comment is getting incorrectly sorted onto the wrong line. See the example below:

Input: test.py

from a_long_name_to_enforce.splitting_across.two_lines import (  # type: ignore[attr-defined]
    a_random_attribute as renamed_random_attribute,
)

Expected
No change; comment stays on line as this is the actual line that mypy complains about.

Observed

from a_long_name_to_enforce.splitting_across.two_lines import (
    a_random_attribute as renamed_random_attribute,  # type: ignore[attr-defined]
)

Comment is shifted down onto the alias line instead.</issue_description>

Comments on the Issue (you are @copilot in this section)


📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 12, 2026

Codecov Report

❌ Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 99.16%. Comparing base (ae91dad) to head (02fc113).
⚠️ Report is 17 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2491      +/-   ##
==========================================
- Coverage   99.18%   99.16%   -0.03%     
==========================================
  Files          40       40              
  Lines        3085     3096      +11     
  Branches      672      674       +2     
==========================================
+ Hits         3060     3070      +10     
  Misses         14       14              
- Partials       11       12       +1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI changed the title [WIP] Fix comments moving bug when using import aliases Fix comment on opening import ( line moving to alias attribute line Mar 12, 2026
Copilot AI requested a review from DanielNoord March 12, 2026 22:05
Copilot AI and others added 3 commits March 12, 2026 23:06
Co-authored-by: DanielNoord <13665637+DanielNoord@users.noreply.github.com>
…erability

Co-authored-by: DanielNoord <13665637+DanielNoord@users.noreply.github.com>
…trary file write vulnerability

Co-authored-by: DanielNoord <13665637+DanielNoord@users.noreply.github.com>
…r issue #2392

Co-authored-by: DanielNoord <13665637+DanielNoord@users.noreply.github.com>
Copilot AI changed the title Fix comment on opening import ( line moving to alias attribute line Fix opening-line comment moving to alias attribute line on wrapped imports Mar 12, 2026
@DanielNoord DanielNoord marked this pull request as ready for review March 12, 2026 22:50
@DanielNoord DanielNoord enabled auto-merge March 12, 2026 22:54
@DanielNoord DanielNoord disabled auto-merge March 12, 2026 23:02
@DanielNoord DanielNoord enabled auto-merge March 12, 2026 23:02
@DanielNoord DanielNoord added this pull request to the merge queue Mar 12, 2026
Merged via the queue into main with commit 9d19240 Mar 12, 2026
21 of 23 checks passed
@DanielNoord DanielNoord deleted the copilot/fix-comments-moving-bug branch March 12, 2026 23:06
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.

Comments moving bug when using import aliases

2 participants