Skip to content

Preserve trailing comments for aliased imports - #2648

Open
SemTiOne wants to merge 13 commits into
PyCQA:mainfrom
SemTiOne:issue/2094
Open

SemTiOne wants to merge 13 commits into
PyCQA:mainfrom
SemTiOne:issue/2094

Conversation

@SemTiOne

@SemTiOne SemTiOne commented Sep 1, 2026

Copy link
Copy Markdown

Trailing comment removed from as imports with combine_as_imports = true and force_single_line = true.

@SemTiOne
SemTiOne marked this pull request as ready for review September 1, 2026 07:14

@Manny7717 Manny7717 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified locally on head 3a04399 (Py3.14, repo venv) against base 131f4ad.

Bug confirmed on base — brute-force across 112 shapes (combine_as_imports × force_single_line / remove_redundant_aliases / multi_line_output / include_trailing_comma / force_grid_wrap × single/multi-alias, plain-import aliases, submodules, mixed alias+plain): 87 shapes drop the trailing comment on base, including the exact issue #2094 scenario (all three comment types lost on the aliased lines).

Head behavior: comments preserved in 109/112 shapes. The 3 remaining "losses" are duplicate-comment collapse when two IDENTICAL comments (# C + # C) merge onto one combined line — identical on base, not a regression. Two DIFFERENT comments merge correctly (# C1; C2), a single comment on either alias survives the combine, and force_single_line keeps one comment per alias line.

Regression test is revert-proven: copied the PR's test into the base worktree → fails (assertion 2: from some_module import the_function as some_function loses # type: ignore); passes on head. Second-pass idempotency asserted by the test and verified manually on all probe shapes.

No regressions: full unit suite head 624 passed / 2 failed vs base 623 passed / 2 failed — identical failure set (pre-existing FileNotFoundError env noise issue_909/938), +1 = the new test.

Mechanism review: parse.py now keys alias comments per-base in straight["<module>.<base>"] (previously a single from["<module>"].__combined_as__ bucket); output.py pops per-base in both the force_single_line and combined paths, and the removed nested_module = None line had no remaining readers (grep-verified). The # C1; C2 join is the existing with_comments separator behavior, not new logic. Non-alias comments (straight imports, non-aliased from-imports) and import m as x # C plain-import aliases are byte-identical to base.

Clean minimal fix; APPROVE.

@DanielNoord
DanielNoord requested a balanced review from Copilot September 21, 2026 15:13
@codecov

codecov Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.44%. Comparing base (f292a60) to head (1ad379e).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2648   +/-   ##
=======================================
  Coverage   99.44%   99.44%           
=======================================
  Files          41       41           
  Lines        3217     3233   +16     
  Branches      691      695    +4     
=======================================
+ Hits         3199     3215   +16     
  Misses         11       11           
  Partials        7        7           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@DanielNoord
DanielNoord requested a balanced review from Copilot and removed request for Copilot September 21, 2026 15:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Alias comments share a non-unique key, causing comments to merge or disappear for multiple aliases of the same import.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Preserves trailing comments on aliased imports when combining aliases into single-line imports.

Changes:

  • Stores and retrieves comments for combined aliases.
  • Adds regression and idempotence coverage for issue #2094.
File Description
isort/​parse.py Changes alias-comment categorization.
isort/​output.py Restores comments when rendering aliases.
tests/​unit/​test_regressions.py Adds regression tests.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread isort/parse.py Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Two comment-routing paths can misassign or duplicate comments between imports.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
Resolved since last review (1)

Comment thread isort/output.py Outdated
Comment thread isort/output.py Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Opening-line comments can move onto an attribute when the same symbol has multiple aliases.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
Resolved since last review (1)

Comment thread isort/output.py Outdated
@DanielNoord

Copy link
Copy Markdown
Member

Closing due to inactivity of the AI bot.

@SemTiOne

Copy link
Copy Markdown
Author

Wait I’m still interested in working on this. I was away for a while, but I’d like to pick this back up.

@DanielNoord DanielNoord reopened this Oct 18, 2026

This branch has not been deployed

No deployments
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.

Trailing comment removed from as imports with combine_as_imports = true and force_single_line = true

4 participants