Skip to content

Commit fee6e8e

Browse files
committed
Fix output of hanging indent for long lines with noqa
1 parent 0c8fc82 commit fee6e8e

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

isort/output.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,17 @@ def _with_from_imports(
454454
parsed.categorized_comments["nested"].get(module, {}).pop(from_import, None)
455455
)
456456
if comment:
457+
# If the comment is a noqa and hanging indent wrapping is used,
458+
# keep the name in the main list and hoist the comment to the statement.
459+
if (
460+
comment.lower().startswith("noqa")
461+
and config.multi_line_output
462+
== wrap.Modes.HANGING_INDENT # type: ignore[attr-defined]
463+
):
464+
comments = list(comments) if comments else []
465+
comments.append(comment)
466+
continue
467+
457468
from_imports.remove(from_import)
458469
if from_imports:
459470
use_comments = []

tests/unit/test_isort.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5741,3 +5741,16 @@ def test_reexport_multiline_long_rollback() -> None:
57415741
test
57425742
"""
57435743
assert isort.code(test_input, config=Config(sort_reexports=True)) == expd_output
5744+
5745+
5746+
def test_noqa_multiline_hanging_indent() -> None:
5747+
test_input = (
5748+
"from aaaaaaa import bbbbbbbbbbbbbbbbb, ccccccccccccccccccc, dddddddddddddddd"
5749+
"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee, \\\n"
5750+
" fffffffffffffffffffff # noqa: E402\n"
5751+
"\n"
5752+
"print(fffffffffffffffffffff)\n"
5753+
"print(dddddddddddddddd)\n"
5754+
)
5755+
output = isort.code(test_input, line_length=120, multi_line_output=WrapModes.HANGING_INDENT)
5756+
assert output == test_input

0 commit comments

Comments
 (0)