summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Crosley <timothy.crosley@gmail.com>2021-03-18 23:23:02 -0700
committerTimothy Crosley <timothy.crosley@gmail.com>2021-03-18 23:23:02 -0700
commit5a0ebba5174d67210b6b23f8f644f1b0bc03de73 (patch)
tree7ea1c87b09104609b057458e3b618f500c84dc68
parent33b4cc3abb700cd89a52ef5fd27080bf32c991a4 (diff)
downloadisort-5a0ebba5174d67210b6b23f8f644f1b0bc03de73.tar.gz
Fixed #1594: incorrect placement of noqa comments with multiple from imports.
-rw-r--r--CHANGELOG.md3
-rw-r--r--isort/output.py12
-rw-r--r--tests/unit/test_regressions.py22
3 files changed, 32 insertions, 5 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2de5058a..4d0a99b2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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.
@@ -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
diff --git a/isort/output.py b/isort/output.py
index 01abbe27..13f72a3d 100644
--- a/isort/output.py
+++ b/isort/output.py
@@ -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 (
diff --git a/tests/unit/test_regressions.py b/tests/unit/test_regressions.py
index 7d311f3c..9f175525 100644
--- a/tests/unit/test_regressions.py
+++ b/tests/unit/test_regressions.py
@@ -1585,3 +1585,25 @@ SOME_CONSTANT = 4
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,
+)
+"""
+ )