Skip to content

Commit

Permalink
Fixed #1721: repeat noqa comments on separate from lines with force-s…
Browse files Browse the repository at this point in the history
…ingle-line set, sometimes get dropped.
  • Loading branch information
timothycrosley committed Jun 13, 2021
1 parent 6b10303 commit 19eb9ac
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@ Find out more about isort's release policy [here](https://pycqa.github.io/isort/
- Fixed (https://github.com/PyCQA/isort/pull/1726): isort ignores reverse_sort when force_sort_within_sections is true.
- Fixed #1741: comments in hanging indent modes can lead to invalid code.
- Fixed #1744: repeat noqa comments dropped when * import and non * imports exist from the same package.
- Fixed #1721: repeat noqa comments on separate from lines with force-single-line set, sometimes get dropped.
- Implemented #1697: Provisional support for PEP 582: skip `__pypackages__` directories by default.
- Implemented #1705: More intuitive handling of isort:skip_file comments on streams.

Expand Down
14 changes: 14 additions & 0 deletions isort/parse.py
Expand Up @@ -455,6 +455,20 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte
] = associated_comment
if associated_comment in comments:
comments.pop(comments.index(associated_comment))
if (
config.force_single_line
and comments
and attach_comments_to is None
and len(just_imports) == 1
):
nested_from_comments = categorized_comments["nested"].setdefault(
import_from, {}
)
existing_comment = nested_from_comments.get(just_imports[0], "")
nested_from_comments[
just_imports[0]
] = f"{existing_comment}{'; ' if existing_comment else ''}{'; '.join(comments)}"
comments = []

if comments and attach_comments_to is None:
attach_comments_to = categorized_comments["from"].setdefault(import_from, [])
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/test_regressions.py
Expand Up @@ -1780,3 +1780,19 @@ def test_isort_should_keep_multi_noqa_with_star_issue_1744():
from typing import * # noqa
"""
)


def test_isort_should_keep_multiple_noqa_comments_force_single_line_mode_issue_1721():
assert isort.check_code(
"""
from some_very_long_filename_to_import_from_that_causes_a_too_long_import_row import ( # noqa: E501
CONSTANT_1,
)
from some_very_long_filename_to_import_from_that_causes_a_too_long_import_row import ( # noqa: E501
CONSTANT_2,
)
""",
show_diff=True,
profile="black",
force_single_line=True,
)

0 comments on commit 19eb9ac

Please sign in to comment.