diff --git a/CHANGELOG.md b/CHANGELOG.md index 2de5058ad..4d0a99b2c 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 01abbe27d..13f72a3d5 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 7d311f3c2..9f1755254 100644 --- a/tests/unit/test_regressions.py +++ b/tests/unit/test_regressions.py @@ -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, +) +""" + )