Skip to content

Commit c36e43c

Browse files
authored
Merge pull request #2184 from bp72/issue/2154
Apply the bracket fix from issue 471 only for use_parentheses=True
2 parents e38702f + ee8d87f commit c36e43c

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

isort/parse.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,11 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte
519519
if comments and attach_comments_to is not None:
520520
attach_comments_to.extend(comments)
521521

522-
if "," in import_string.split(just_imports[-1])[-1]:
522+
if (
523+
just_imports
524+
and just_imports[-1]
525+
and "," in import_string.split(just_imports[-1])[-1]
526+
):
523527
trailing_commas.add(import_from)
524528
else:
525529
if comments and attach_comments_to is not None:

isort/wrap.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -122,20 +122,21 @@ def line(content: str, line_separator: str, config: Config = DEFAULT_CONFIG) ->
122122
_separator = line_separator
123123
else:
124124
_separator = ""
125-
_comment = ""
125+
noqa_comment = ""
126126
if comment and "noqa" in comment:
127-
_comment = f"{config.comment_prefix}{comment}"
127+
noqa_comment = f"{config.comment_prefix}{comment}"
128128
cont_line = cont_line.rstrip()
129129
_comma = "," if config.include_trailing_comma else ""
130130
output = (
131-
f"{content}{splitter}({_comment}"
131+
f"{content}{splitter}({noqa_comment}"
132132
f"{line_separator}{cont_line}{_comma}{_separator})"
133133
)
134-
lines = output.split(line_separator)
135-
if config.comment_prefix in lines[-1] and lines[-1].endswith(")"):
136-
content, comment = lines[-1].split(config.comment_prefix, 1)
137-
lines[-1] = content + ")" + config.comment_prefix + comment[:-1]
138-
return line_separator.join(lines)
134+
lines = output.split(line_separator)
135+
if config.comment_prefix in lines[-1] and lines[-1].endswith(")"):
136+
content, comment = lines[-1].split(config.comment_prefix, 1)
137+
lines[-1] = content + ")" + config.comment_prefix + comment[:-1]
138+
output = line_separator.join(lines)
139+
return output
139140
return f"{content}{splitter}\\{line_separator}{cont_line}"
140141
elif len(content) > config.line_length and wrap_mode == Modes.NOQA and "# NOQA" not in content: # type: ignore
141142
return f"{content}{config.comment_prefix} NOQA"

tests/unit/test_wrap.py

+32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import pytest
2+
13
from isort import wrap
24
from isort.settings import Config
5+
from isort.wrap_modes import WrapModes
36

47

58
def test_import_statement():
@@ -16,3 +19,32 @@ def test_import_statement():
1619
assert wrap.import_statement("from x import ", ["y", "z"], [], explode=True) == (
1720
"from x import (\n y,\n z,\n)"
1821
)
22+
23+
24+
@pytest.mark.parametrize(
25+
"multi_line_output, expected",
26+
(
27+
(
28+
WrapModes.VERTICAL_HANGING_INDENT, # type: ignore
29+
"""from a import (
30+
b as c # comment that is long enough that this import doesn't fit in one line (parens)
31+
)""",
32+
),
33+
(
34+
WrapModes.VERTICAL, # type: ignore
35+
"""from a import (
36+
b as c) # comment that is long enough that this import doesn't fit in one line (parens)""",
37+
),
38+
),
39+
)
40+
def test_line__comment_with_brackets__expects_unchanged_comment(multi_line_output, expected):
41+
content = (
42+
"from a import b as c "
43+
"# comment that is long enough that this import doesn't fit in one line (parens)"
44+
)
45+
config = Config(
46+
multi_line_output=multi_line_output,
47+
use_parentheses=True,
48+
)
49+
50+
assert wrap.line(content=content, line_separator="\n", config=config) == expected

0 commit comments

Comments
 (0)