Skip to content

Commit 32dcb03

Browse files
authored
Merge pull request #2474 from PyCQA/copilot/fix-multi-line-output-handling
Fix multi_line_output=3/5 ignored when wrapping single imports with inline comments
2 parents 888e507 + 0e40458 commit 32dcb03

2 files changed

Lines changed: 49 additions & 7 deletions

File tree

isort/wrap.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,21 @@ def line(content: str, line_separator: str, config: Config = DEFAULT_CONFIG) ->
8282
splitter
8383
):
8484
line_parts = re.split(exp, line_without_comment)
85-
if comment and not (config.use_parentheses and "noqa" in comment):
85+
_is_vertical_mode = wrap_mode in (
86+
Modes.VERTICAL_HANGING_INDENT, # type: ignore
87+
Modes.VERTICAL_GRID_GROUPED, # type: ignore
88+
)
89+
# Determine whether the comment should be hoisted to the opening
90+
# parenthesis line rather than embedded in the import line.
91+
# This happens for noqa comments (when use_parentheses is True)
92+
# and for all comments in vertical hanging modes (when
93+
# use_parentheses is False), so that multi_line_output=3/5 is
94+
# respected even without an explicit use_parentheses=True setting.
95+
_hoist_comment_to_paren = comment and (
96+
(config.use_parentheses and "noqa" in comment)
97+
or (_is_vertical_mode and not config.use_parentheses)
98+
)
99+
if comment and not _hoist_comment_to_paren:
86100
_comma_maybe = (
87101
","
88102
if (
@@ -109,21 +123,18 @@ def line(content: str, line_separator: str, config: Config = DEFAULT_CONFIG) ->
109123
line_separator,
110124
config,
111125
)
112-
if config.use_parentheses:
126+
if config.use_parentheses or _is_vertical_mode:
113127
if splitter == "as ":
114128
output = f"{content}{splitter}{cont_line.lstrip()}"
115129
else:
116130
_comma = "," if config.include_trailing_comma and not comment else ""
117131

118-
if wrap_mode in (
119-
Modes.VERTICAL_HANGING_INDENT, # type: ignore
120-
Modes.VERTICAL_GRID_GROUPED, # type: ignore
121-
):
132+
if _is_vertical_mode:
122133
_separator = line_separator
123134
else:
124135
_separator = ""
125136
noqa_comment = ""
126-
if comment and "noqa" in comment:
137+
if _hoist_comment_to_paren:
127138
noqa_comment = f"{config.comment_prefix}{comment}"
128139
cont_line = cont_line.rstrip()
129140
_comma = "," if config.include_trailing_comma else ""

tests/unit/test_ticketed_features.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,37 @@ def test_sort_separate_packages_issue_2104():
11971197
)
11981198

11991199

1200+
def test_combine_as_with_comments_and_vertical_hang_2316() -> None:
1201+
"""Comments should still produce correct parentheses with multi_line_output=3.
1202+
See: https://github.com/PyCQA/isort/issues/2316
1203+
"""
1204+
assert (
1205+
isort.code(
1206+
"from pythonosc.udp_client import SimpleUDPClient # type: ignore[import-untyped]",
1207+
combine_as_imports=True,
1208+
multi_line_output=3,
1209+
)
1210+
== """\
1211+
from pythonosc.udp_client import ( # type: ignore[import-untyped]
1212+
SimpleUDPClient
1213+
)
1214+
"""
1215+
)
1216+
assert (
1217+
isort.code(
1218+
"from pythonosc.udp_client import SimpleUDPClient # type: ignore[import-untyped]",
1219+
combine_as_imports=True,
1220+
multi_line_output=3,
1221+
include_trailing_comma=True,
1222+
)
1223+
== """\
1224+
from pythonosc.udp_client import ( # type: ignore[import-untyped]
1225+
SimpleUDPClient,
1226+
)
1227+
"""
1228+
)
1229+
1230+
12001231
def test_isort_preserves_empty_inline_comments() -> None:
12011232
"""isort should not strip empty inline comments (bare `#`).
12021233
See: https://github.com/PyCQA/isort/issues/1913

0 commit comments

Comments
 (0)