Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply the bracket fix from issue 471 only for use_parentheses=True #2184

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion isort/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,11 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte
if comments and attach_comments_to is not None:
attach_comments_to.extend(comments)

if "," in import_string.split(just_imports[-1])[-1]:
if (
just_imports
and just_imports[-1]
and "," in import_string.split(just_imports[-1])[-1]
):
trailing_commas.add(import_from)
else:
if comments and attach_comments_to is not None:
Expand Down
17 changes: 9 additions & 8 deletions isort/wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,21 @@ def line(content: str, line_separator: str, config: Config = DEFAULT_CONFIG) ->
_separator = line_separator
else:
_separator = ""
_comment = ""
noqa_comment = ""
if comment and "noqa" in comment:
_comment = f"{config.comment_prefix}{comment}"
noqa_comment = f"{config.comment_prefix}{comment}"
cont_line = cont_line.rstrip()
_comma = "," if config.include_trailing_comma else ""
output = (
f"{content}{splitter}({_comment}"
f"{content}{splitter}({noqa_comment}"
f"{line_separator}{cont_line}{_comma}{_separator})"
)
lines = output.split(line_separator)
if config.comment_prefix in lines[-1] and lines[-1].endswith(")"):
content, comment = lines[-1].split(config.comment_prefix, 1)
lines[-1] = content + ")" + config.comment_prefix + comment[:-1]
return line_separator.join(lines)
lines = output.split(line_separator)
if config.comment_prefix in lines[-1] and lines[-1].endswith(")"):
content, comment = lines[-1].split(config.comment_prefix, 1)
lines[-1] = content + ")" + config.comment_prefix + comment[:-1]
output = line_separator.join(lines)
return output
return f"{content}{splitter}\\{line_separator}{cont_line}"
elif len(content) > config.line_length and wrap_mode == Modes.NOQA and "# NOQA" not in content: # type: ignore
return f"{content}{config.comment_prefix} NOQA"
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/test_wrap.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import pytest

from isort import wrap
from isort.settings import Config
from isort.wrap_modes import WrapModes


def test_import_statement():
Expand All @@ -16,3 +19,32 @@ def test_import_statement():
assert wrap.import_statement("from x import ", ["y", "z"], [], explode=True) == (
"from x import (\n y,\n z,\n)"
)


@pytest.mark.parametrize(
"multi_line_output, expected",
(
(
WrapModes.VERTICAL_HANGING_INDENT, # type: ignore
"""from a import (
b as c # comment that is long enough that this import doesn't fit in one line (parens)
)""",
),
(
WrapModes.VERTICAL, # type: ignore
"""from a import (
b as c) # comment that is long enough that this import doesn't fit in one line (parens)""",
),
),
)
def test_line__comment_with_brackets__expects_unchanged_comment(multi_line_output, expected):
content = (
"from a import b as c "
"# comment that is long enough that this import doesn't fit in one line (parens)"
)
config = Config(
multi_line_output=multi_line_output,
use_parentheses=True,
)

assert wrap.line(content=content, line_separator="\n", config=config) == expected