Preserve trailing comments for aliased imports - #2648
Conversation
Manny7717
left a comment
There was a problem hiding this comment.
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.
|
Thanks again for the review and your engagement, what AI model do you use? (just asking) |
asimports withcombine_as_imports = trueandforce_single_line = true#2094Trailing comment removed from
asimports withcombine_as_imports = trueandforce_single_line = true.