Skip to content

Commit

Permalink
Resolve #1427: Fixed isort incorrectly moving import statement
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Aug 30, 2020
1 parent d203bc3 commit ae10a3f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
12 changes: 10 additions & 2 deletions isort/output.py
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -529,3 +529,11 @@ def is_comment(line):
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
7 changes: 1 addition & 6 deletions isort/parse.py
Expand Up @@ -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:
Expand Down

0 comments on commit ae10a3f

Please sign in to comment.