Skip to content

Preserve trailing comments for aliased imports - #2648

Open
SemTiOne wants to merge 1 commit into
PyCQA:mainfrom
SemTiOne:issue/2094
Open

Preserve trailing comments for aliased imports#2648
SemTiOne wants to merge 1 commit 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

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.

@SemTiOne

SemTiOne commented Sep 1, 2026

Copy link
Copy Markdown
Author

Thanks again for the review and your engagement, what AI model do you use? (just asking)

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

2 participants