Skip to content

fix: keep comment-only lines inside from-import groups#2588

Open
l46983284-cpu wants to merge 11 commits into
PyCQA:mainfrom
l46983284-cpu:issue/1852-preserve-comment-only-import-lines
Open

fix: keep comment-only lines inside from-import groups#2588
l46983284-cpu wants to merge 11 commits into
PyCQA:mainfrom
l46983284-cpu:issue/1852-preserve-comment-only-import-lines

Conversation

@l46983284-cpu

Copy link
Copy Markdown

Summary

Comment-only lines inside a parenthesised from … import ( group were collapsed onto the opening import line as one semicolon-joined comment. With the black profile that rewrote intentional WIP comments and produced lines that fail line-length checkers.

This change tracks those body comments separately. When parentheses are used, they are re-emitted as their own indented comment lines inside the group. Non-parenthesised wrap modes keep the historical collapse behaviour so existing cases such as issue 1396 stay stable.

Test plan

  • pytest tests/unit/test_regressions.py::test_comment_only_lines_in_from_import_group_issue_1852
  • pytest tests/unit/test_regressions.py::test_incorrect_grouping_when_comments_issue_1396
  • pytest tests/unit/test_regressions.py::test_comment_on_opening_line_of_aliased_import_does_not_move
  • pytest tests/unit/test_isort.py tests/unit/test_regressions.py tests/unit/test_comments.py (331 passed, 1 skipped)

Fixes #1852

@l46983284-cpu

Copy link
Copy Markdown
Author

DeepSource was failing because the existing skipcq: PY-R1000 comment landed on the small helper I added instead of _with_from_imports.

Moved the skip back onto _with_from_imports (same pattern as before). No product change; regression for #1852 still passes.

@staticdev
staticdev force-pushed the issue/1852-preserve-comment-only-import-lines branch from ee1bc23 to 468d670 Compare July 18, 2026 15:04
@codecov

codecov Bot commented Jul 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.91525% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.22%. Comparing base (bae1e55) to head (0e3cd7e).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2588      +/-   ##
==========================================
- Coverage   99.30%   99.22%   -0.09%     
==========================================
  Files          41       41              
  Lines        3153     3207      +54     
  Branches      681      696      +15     
==========================================
+ Hits         3131     3182      +51     
- Misses         14       15       +1     
- Partials        8       10       +2     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@l46983284-cpu

Copy link
Copy Markdown
Author

Fixed flake8 E303 by removing one extra blank line before the #1852 regression test. No product change.

@l46983284-cpu

Copy link
Copy Markdown
Author

Lint Attempt #2 failed on ruff format --check for tests/unit/test_regressions.py (not flake8 E303). Applied ruff format to the #1852 regression test only; product behavior unchanged. Focused pytest still passes.

@l46983284-cpu

Copy link
Copy Markdown
Author

Residual loop closeout (multi-stage A/C/D/B1/B2 + mechanical gate PASS):

No further product thrash planned; remaining secondary nits (as-only / all-comment-only) disclosed as ALLOW_WITH_NITS scope.

l46983284-cpu added a commit to l46983284-cpu/isort that referenced this pull request Jul 19, 2026
Route comment-only members onto the first single-line import and fold
them into combine_star star comments so PyCQA#1852 does not silently drop
body comments on secondary emit paths.

Fixes residual findings from multi-stage review on PyCQA#2588.

Signed-off-by: Alex Chen <l46983284@gmail.com>
@l46983284-cpu

Copy link
Copy Markdown
Author

Quick update without the process noise from my last note:

I fixed two edge cases on the current head: force_single_line was dropping body comments, and combine_star left orphan indented comments. Both have regression tests, and the original #1852 black-profile case still passes.

No further product thrash planned from me. Happy to adjust if review wants a different default.

@DanielNoord DanielNoord left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please ensure the full patch has coverage :)

For the tests I'd prefer the style of the first test so making explicit what the final output looks like instead of using "..." in out. However, if the output is the same as the input please use something like assert isort.code(to_sort, profile="black", line_length=100) == to_sort

@l46983284-cpu

Copy link
Copy Markdown
Author

Thanks — I rewrote the #1852 tests to use explicit expected output (including the force_single_line case) and added coverage for the remaining from_body / inject branches.

@DanielNoord DanielNoord left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you fix CI to pass? I'll have a look at this afterwards :)

@DanielNoord DanielNoord left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

CI still isn't fixed.

Comment-only members of a parenthesised from-import were collapsed onto
the opening import line as a single semicolon-joined comment. With the
black profile that rewrote intentional WIP comments and produced lines
that fail line-length checkers.

Track those body comments separately and re-emit them as indented lines
inside parenthesised wrapping; preserve the historical collapse for
non-parenthesised wrap modes.

Fixes PyCQA#1852

Signed-off-by: Alex Chen <l46983284@gmail.com>
Signed-off-by: Alex Chen <l46983284@gmail.com>
CI lint fails on `ruff format --check` for the new regression test
strings (and blank-line spacing). Apply ruff format only; product
behavior unchanged.

Signed-off-by: Alex Chen <l46983284@gmail.com>
Route comment-only members onto the first single-line import and fold
them into combine_star star comments so PyCQA#1852 does not silently drop
body comments on secondary emit paths.

Fixes residual findings from multi-stage review on PyCQA#2588.

Signed-off-by: Alex Chen <l46983284@gmail.com>
Signed-off-by: Alex Chen <l46983284@gmail.com>
mypy attr-defined failed because isort.output only aliases
add_to_line as with_comments and does not re-export it.

Signed-off-by: Alex Chen <l46983284@gmail.com>
isort --profile hug --check failed on tests/unit/test_regressions.py
because isort.comments must sort before isort.main.

Signed-off-by: Alex Chen <l46983284@gmail.com>
Signed-off-by: Alex Chen <l46983284@gmail.com>
Fold single-line body comments via with_comments instead of orphan
indented lines, and drain remaining body comments onto the last emitted
statement when no grouped import_statement is built.

Signed-off-by: Alex Chen <l46983284@gmail.com>
Pending opening/body comments now attach to the first emitted
force_single line (plain or as). Single-line inject merges existing
nested trailing comments instead of clobbering them.

Signed-off-by: Alex Chen <l46983284@gmail.com>
@l46983284-cpu
l46983284-cpu force-pushed the issue/1852-preserve-comment-only-import-lines branch from 7e0c2ce to 0e3cd7e Compare July 25, 2026 11:52
@l46983284-cpu

Copy link
Copy Markdown
Author

Rebased onto main (kept the new alias-comment regression), fixed the lint/format gap, and closed the from_body drops on as-only / force_single emit paths. Local unit suite + mypy/flake8/ruff/hug check are green on this tip.

@DanielNoord DanielNoord left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We're missing coverage again.

Add ignore_comments and nested-merge cases for
_inject_from_body_comments, plus force_single_line plain-before-as
coverage so the pending-comment drain path is exercised.

Signed-off-by: Alex Chen <l46983284@gmail.com>
@l46983284-cpu

Copy link
Copy Markdown
Author

Thanks — added coverage for the remaining from_body inject branches (ignore_comments, nested-merge fold, and force_single plain-before-as). No product change on this tip.

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 in group imports are moved to a single line causing line too long violation (black, flake8)

2 participants