Skip to content

Commit 123e7d3

Browse files
committed
fix: preserve from_body comments on force_single_line and combine_star
Route comment-only members onto the first single-line import and fold them into combine_star star comments so #1852 does not silently drop body comments on secondary emit paths. Fixes residual findings from multi-stage review on #2588. Signed-off-by: Alex Chen <l46983284@gmail.com>
1 parent 739a811 commit 123e7d3

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

isort/output.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,14 @@ def _with_from_imports(
400400
above_comments = None
401401

402402
if "*" in from_imports and config.combine_star:
403+
# Fold from_body comments onto the star statement (main-compatible).
404+
star_comments = list(comments or ())
405+
if body_comments and not config.ignore_comments:
406+
star_comments = star_comments + body_comments
407+
body_comments = []
403408
import_statement = wrap.line(
404409
with_comments(
405-
_with_star_comments(parsed, module, list(comments or ())),
410+
_with_star_comments(parsed, module, star_comments),
406411
f"{import_start}*",
407412
removed=config.ignore_comments,
408413
comment_prefix=config.comment_prefix,
@@ -416,6 +421,11 @@ def _with_from_imports(
416421
only_show_as_imports = True
417422
elif config.force_single_line and module not in config.single_line_exclusions:
418423
import_statement = ""
424+
# Preserve comment-only members on the first single-line import
425+
# (matches pre-#1852 main behaviour for force_single_line).
426+
if body_comments and not config.ignore_comments:
427+
comments = list(comments or []) + body_comments
428+
body_comments = []
419429
while from_imports:
420430
from_import = from_imports.pop(0)
421431
single_import_line = with_comments(

tests/unit/test_regressions.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,3 +2430,26 @@ def test_comment_only_lines_in_from_import_group_issue_1852():
24302430
mixed_out = isort.code(mixed, profile="black")
24312431
assert mixed_out == mixed_expected
24322432
assert isort.code(mixed_out, profile="black") == mixed_out
2433+
2434+
2435+
def test_force_single_line_preserves_body_comments_issue_1852():
2436+
"""force_single_line must not drop comment-only from-import members.
2437+
2438+
Regression against silent loss after from_body routing (issue #1852).
2439+
"""
2440+
mixed = "from foo import (\n zeta,\n # disabled\n alpha,\n)\n"
2441+
out = isort.code(mixed, profile="black", force_single_line=True)
2442+
assert "# disabled" in out
2443+
assert "from foo import alpha" in out
2444+
assert "from foo import zeta" in out
2445+
# main-compatible: comment attached to first emitted line
2446+
assert "from foo import alpha # disabled" in out
2447+
assert isort.code(out, profile="black", force_single_line=True) == out
2448+
2449+
2450+
def test_combine_star_folds_body_comments_issue_1852():
2451+
"""combine_star should fold body comments onto the star statement."""
2452+
src = "from foo import (\n # disabled\n *\n)\n"
2453+
out = isort.code(src, combine_star=True)
2454+
assert out == "from foo import * # disabled\n"
2455+
assert isort.code(out, combine_star=True) == out

0 commit comments

Comments
 (0)