diff options
author | Timothy Crosley <timothy.crosley@gmail.com> | 2020-08-29 17:55:46 -0700 |
---|---|---|
committer | Timothy Crosley <timothy.crosley@gmail.com> | 2020-08-29 17:55:46 -0700 |
commit | ae10a3f7ce077795e5dcb6e65d8debaf978c126c (patch) | |
tree | 16e71c1a5ac8b5607cb30da9dd07c168c5985a8f | |
parent | d203bc34b04919fc30332c4f93fab23a14660719 (diff) | |
download | isort-issue/1427/isort-shouldnt-move-imports.tar.gz |
Resolve #1427: Fixed isort incorrectly moving import statementissue/1427/isort-shouldnt-move-imports
-rw-r--r-- | isort/output.py | 12 | ||||
-rw-r--r-- | isort/parse.py | 7 |
2 files changed, 11 insertions, 8 deletions
diff --git a/isort/output.py b/isort/output.py index 7d793ac1..1a6c504b 100644 --- a/isort/output.py +++ b/isort/output.py @@ -272,7 +272,7 @@ def _with_from_imports( if "*" in from_imports and config.combine_star: import_statement = wrap.line( with_comments( - comments, + _with_star_comments(parsed, module, list(comments or ())), f"{import_start}*", removed=config.ignore_comments, comment_prefix=config.comment_prefix, @@ -364,7 +364,7 @@ def _with_from_imports( if "*" in from_imports: output.append( with_comments( - comments, + _with_star_comments(parsed, module, list(comments or ())), f"{import_start}*", removed=config.ignore_comments, comment_prefix=config.comment_prefix, @@ -529,3 +529,11 @@ def _ensure_newline_before_comment(output): new_output.append("") new_output.append(line) return new_output + + +def _with_star_comments(parsed: parse.ParsedContent, module: str, comments: List[str]) -> List[str]: + star_comment = parsed.categorized_comments["nested"].get(module, {}).pop("*", None) + if star_comment: + return comments + [star_comment] + else: + return comments diff --git a/isort/parse.py b/isort/parse.py index 1e71c6d2..913cdbb1 100644 --- a/isort/parse.py +++ b/isort/parse.py @@ -222,12 +222,7 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte import_string, comment = parse_comments(line) comments = [comment] if comment else [] line_parts = [part for part in _strip_syntax(import_string).strip().split(" ") if part] - if ( - type_of_import == "from" - and len(line_parts) == 2 - and line_parts[1] != "*" - and comments - ): + if type_of_import == "from" and len(line_parts) == 2 and comments: nested_comments[line_parts[-1]] = comments[0] if "(" in line.split("#", 1)[0] and index < line_count: |