Skip to content

Fix multi_line_output=3/5 ignored when wrapping single imports with inline comments#2474

Merged
DanielNoord merged 3 commits into
mainfrom
copilot/fix-multi-line-output-handling
Mar 12, 2026
Merged

Fix multi_line_output=3/5 ignored when wrapping single imports with inline comments#2474
DanielNoord merged 3 commits into
mainfrom
copilot/fix-multi-line-output-handling

Conversation

Copilot AI commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

When a single import line with an inline comment exceeded line_length, wrap.line() fell through to backslash continuation regardless of multi_line_output mode, because the parentheses-vs-backslash decision was gated solely on config.use_parentheses (default False).

Root cause: VERTICAL_HANGING_INDENT (mode 3) and VERTICAL_GRID_GROUPED (mode 5) are inherently parenthesis-based, but wrap.line() only respected that when use_parentheses=True was explicitly set.

Changes:

  • isort/wrap.py — Introduced _is_vertical_mode to identify modes 3/5, and _hoist_comment_to_paren to unify the logic for placing a comment on the opening paren line. When use_parentheses=False but a vertical mode is active, comments are now hoisted to the opening paren and parentheses replace the backslash.
  • tests/unit/test_ticketed_features.py — Added test_combine_as_with_comments_and_vertical_hang_2316.
# Before (multi_line_output=3, include_trailing_comma=True)
from pythonosc.udp_client import \
    SimpleUDPClient  # type: ignore[import-untyped]

# After
from pythonosc.udp_client import (  # type: ignore[import-untyped]
    SimpleUDPClient,
)

Existing behavior is unchanged when use_parentheses=True (e.g. the black profile).

Original prompt

This section details on the original issue you should resolve

<issue_title>multi_line_output ignored when wrapping with comment</issue_title>
<issue_description>Given:
setup.cfg:

[isort]
atomic = True
balanced_wrapping = True
combine_as_imports = True
include_trailing_comma = True
multi_line_output = 3
order_by_type = False
float_to_top = True

extra_standard_library = typing_extensions
default_section = THIRDPARTY
sections = FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER

foo.py:

from pythonosc.udp_client import SimpleUDPClient  # type: ignore[import-untyped]

Expected:

from pythonosc.udp_client import (  # type: ignore[import-untyped]
    SimpleUDPClient,
)

Actual:

from pythonosc.udp_client import \
    SimpleUDPClient  # type: ignore[import-untyped]

</issue_description>

<agent_instructions>Use the test case given in the issue</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@DanielNoord A test would be: ```diff diff --git a/tests/unit/test_ticketed_features.py b/tests/unit/test_ticketed_features.py index 829cae5a..69f41046 100644 --- a/tests/unit/test_ticketed_features.py +++ b/tests/unit/test_ticketed_features.py @@ -1195,3 +1195,19 @@ from package1.a.c import baz from package2 import bar """ ) + + +def test_combine_as_with_comments_and_vertical_hang_2316() -> None: + """Comments and `combine_as_imports` should still produce correct parentheses""" + assert ( + isort.code( + "from pythonosc.udp_client import SimpleUDPClient # type: ignore[import-untyped]", + combine_as_imports=True, + multi_line_output=3, + ) + == """\ +from pythonosc.udp_client import ( # type: ignore[import-untyped] + SimpleUDPClient, +) +""" + )
</comments>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: DanielNoord <13665637+DanielNoord@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix multi line output ignored when wrapping with comment Fix multi_line_output=3/5 ignored when wrapping single imports with inline comments Mar 10, 2026
@codecov

codecov Bot commented Mar 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.19%. Comparing base (888e507) to head (0e40458).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2474   +/-   ##
=======================================
  Coverage   99.18%   99.19%           
=======================================
  Files          40       40           
  Lines        3086     3088    +2     
  Branches      672      672           
=======================================
+ Hits         3061     3063    +2     
  Misses         14       14           
  Partials       11       11           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@DanielNoord DanielNoord marked this pull request as ready for review March 12, 2026 21:09
@DanielNoord DanielNoord enabled auto-merge March 12, 2026 21:09
auto-merge was automatically disabled March 12, 2026 22:34

Head branch was pushed to by a user without write access

Copilot AI requested a review from DanielNoord March 12, 2026 22:35
@DanielNoord DanielNoord enabled auto-merge March 12, 2026 22:49
@DanielNoord DanielNoord added this pull request to the merge queue Mar 12, 2026
Merged via the queue into main with commit 32dcb03 Mar 12, 2026
22 of 23 checks passed
@DanielNoord DanielNoord deleted the copilot/fix-multi-line-output-handling branch March 12, 2026 22:53
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.

multi_line_output ignored when wrapping with comment

2 participants