diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b090d416..37d0ebc38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ Changelog NOTE: isort follows the [semver](https://semver.org/) versioning standard. Find out more about isort's release policy [here](https://pycqa.github.io/isort/docs/major_releases/release_policy/). +### 5.6.3 October TBD, 2020 + - Fixed #1556: Empty line added between imports that should be skipped. + ### 5.6.2 October 10, 2020 - Fixed #1548: On rare occasions an unecessary empty line can be added when an import is marked as skipped. - Fixed #1542: Bug in VERTICAL_PREFIX_FROM_MODULE_IMPORT wrap mode. diff --git a/isort/parse.py b/isort/parse.py index 96468f095..2c152651a 100644 --- a/isort/parse.py +++ b/isort/parse.py @@ -218,17 +218,35 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte import_index = index - 1 while import_index and not in_lines[import_index - 1]: import_index -= 1 - elif "isort:skip" in line or "isort: skip" in line: - commentless = line.split("#", 1)[0] + else: + commentless = line.split("#", 1)[0].strip() if ( - "(" in commentless - and not commentless.rstrip().endswith(")") - and import_index < line_count + ("isort:skip" in line + or "isort: skip" in line) + and "(" in commentless + and ")" not in commentless ): import_index = index - while import_index < line_count and not commentless.rstrip().endswith(")"): - commentless = in_lines[import_index].split("#", 1)[0] - import_index += 1 + + starting_line = line + while "isort:skip" in starting_line or "isort: skip" in starting_line: + commentless = starting_line.split("#", 1)[0] + if ( + "(" in commentless + and not commentless.rstrip().endswith(")") + and import_index < line_count + ): + + while import_index < line_count and not commentless.rstrip().endswith(")"): + commentless = in_lines[import_index].split("#", 1)[0] + import_index += 1 + else: + import_index += 1 + + if import_index >= line_count: + break + else: + starting_line = in_lines[import_index] line, *end_of_line_comment = line.split("#", 1) if ";" in line: diff --git a/tests/unit/test_regressions.py b/tests/unit/test_regressions.py index 873cc2f72..c25ed19c7 100644 --- a/tests/unit/test_regressions.py +++ b/tests/unit/test_regressions.py @@ -1424,6 +1424,33 @@ def test_isort_shouldnt_split_skip_issue_1548(): ) +def test_isort_shouldnt_split_skip_issue_1556(): + assert isort.check_code( + """ +from tools.dependency_pruning.prune_dependencies import ( # isort:skip + prune_dependencies, +) +from tools.developer_pruning.prune_developers import ( # isort:skip + prune_developers, +) +""", + show_diff=True, + profile="black", + float_to_top=True, + ) + assert isort.check_code( + """ +from tools.dependency_pruning.prune_dependencies import ( # isort:skip + prune_dependencies, +) +from tools.developer_pruning.prune_developers import x # isort:skip +""", + show_diff=True, + profile="black", + float_to_top=True, + ) + + def test_isort_losing_imports_vertical_prefix_from_module_import_wrap_mode_issue_1542(): """Ensure isort doesnt lose imports when a comment is combined with an import and wrap mode VERTICAL_PREFIX_FROM_MODULE_IMPORT is used.