Skip to content

Commit

Permalink
Fixed #1594: incorrect placement of noqa comments with multiple from …
Browse files Browse the repository at this point in the history
…imports.
  • Loading branch information
timothycrosley committed Mar 19, 2021
1 parent 33b4cc3 commit 5a0ebba
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Find out more about isort's release policy [here](https://pycqa.github.io/isort/
### 5.8.0 TBD
- Fixed #1631: as import comments can in some cases be duplicated.
- Fixed #1667: extra newline added with float-to-top, after skip, in some cases.
- Fixed #1594: incorrect placement of noqa comments with multiple from imports.
- Implemented #1648: Export MyPY type hints.
- Implemented #1641: Identified import statements now return runnable code.
- Implemented #1661: Added "wemake" profile.
Expand All @@ -16,7 +17,7 @@ Find out more about isort's release policy [here](https://pycqa.github.io/isort/
- Implemented #1684: Added support for extending skips with `--extend-skip` and `--extend-skip-glob`.
- Implemented #1688: Auto identification and skipping of some invalid import statements.
- Implemented #1645: Ability to reverse the import sorting order.
- Implemented #1504: Ability to push star imports to the top to avoid overriding explicitly defined imports.
- Implemented #1504: Added ability to push star imports to the top to avoid overriding explicitly defined imports.
- Documented #1685: Skip doesn't support plain directory names, but skip_glob does.

### 5.7.0 December 30th 2020
Expand Down
12 changes: 8 additions & 4 deletions isort/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,18 +429,22 @@ def _with_from_imports(
parsed.categorized_comments["nested"].get(module, {}).pop(from_import, None)
)
if comment:
from_imports.remove(from_import)
if from_imports:
use_comments = []
else:
use_comments = comments
comments = None
single_import_line = with_comments(
comments,
use_comments,
import_start + from_import,
removed=config.ignore_comments,
comment_prefix=config.comment_prefix,
)
single_import_line += (
f"{comments and ';' or config.comment_prefix} " f"{comment}"
f"{use_comments and ';' or config.comment_prefix} " f"{comment}"
)
output.append(wrap.line(single_import_line, parsed.line_separator, config))
from_imports.remove(from_import)
comments = None

from_import_section = []
while from_imports and (
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1585,3 +1585,25 @@ def test_isort_shouldnt_add_extra_line_float_to_top_issue_1667():
show_diff=True,
float_to_top=True,
)


def test_isort_shouldnt_move_noqa_comment_issue_1594():
assert (
isort.code(
"""
from .test import TestTestTestTestTestTest1 # noqa: F401
from .test import TestTestTestTestTestTest2, TestTestTestTestTestTest3, """
"""TestTestTestTestTestTest4, TestTestTestTestTestTest5 # noqa: F401
""",
profile="black",
)
== """
from .test import TestTestTestTestTestTest1 # noqa: F401
from .test import ( # noqa: F401
TestTestTestTestTestTest2,
TestTestTestTestTestTest3,
TestTestTestTestTestTest4,
TestTestTestTestTestTest5,
)
"""
)

0 comments on commit 5a0ebba

Please sign in to comment.