Skip to content

Commit

Permalink
fix: ignore unused import statements with noqa autoimport line
Browse files Browse the repository at this point in the history
  • Loading branch information
lyz-code committed Oct 22, 2021
1 parent 21eb2bd commit 5dd5970
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/autoimport/model.py
Expand Up @@ -381,6 +381,10 @@ def _remove_unused_imports(self, import_name: str) -> None:
object_name = import_name.split(".")[-1]

for line in self.imports:
# Ignore the lines containing # noqa: autoimport
if re.match(r".*?# ?noqa:.*?autoimport.*", line):
continue

# If it's the only line, remove it
if re.match(fr"(from {package_name} )?import {object_name}$", line):
self.imports.remove(line)
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_services.py
Expand Up @@ -491,6 +491,18 @@ def test_fix_doesnt_move_import_statements_with_noqa_to_the_top() -> None:
assert result == source


def test_fix_doesnt_fail_on_noqa_lines_on_unused_import() -> None:
"""Ignore lines that have # noqa: autoimport."""
source = dedent(
"""\
from os import getcwd # noqa: autoimport"""
)

result = fix_code(source)

assert result == source


def test_fix_respects_noqa_in_from_import_lines_in_multiple_lines() -> None:
"""
Given: Multiple from X import Y lines, some with multiline format with noqa
Expand Down

0 comments on commit 5dd5970

Please sign in to comment.