Skip to content

Commit

Permalink
Fix #127
Browse files Browse the repository at this point in the history
  • Loading branch information
hakancelikdev committed Oct 7, 2020
1 parent f657154 commit f78b92b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
12 changes: 12 additions & 0 deletions tests/test_refactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,18 @@ def test_get_star_imp_none(self):
self.session.refactor(action),
)

def test_initial_imports(self):
from unimport.constants import INITIAL_IMPORTS

actions = [
(f"import {imp_name}\n{imp_name.split('.')[0]}\n")
for imp_name in INITIAL_IMPORTS
]
self.assertListEqual(
actions,
[self.session.refactor(action) for action in actions],
)


class TestDuplicateUnusedRefactor(RefactorTestCase):
def test_full_unused(self):
Expand Down
14 changes: 14 additions & 0 deletions unimport/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,17 @@
SUBSCRIPT_TYPE_VARIABLE.extend(["Literal", "OrderedDict"])
elif PY37_PLUS:
SUBSCRIPT_TYPE_VARIABLE.append("Literal")

INITIAL_IMPORTS = frozenset(
{
"encodings.utf_8",
"encodings.aliases",
"encodings.latin_1",
"importlib._bootstrap",
"importlib.abc",
"importlib.machinery",
"importlib._bootstrap_external",
"importlib.util",
"os.path",
}
)
8 changes: 7 additions & 1 deletion unimport/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
)

from unimport import color
from unimport.constants import PY38_PLUS, SUBSCRIPT_TYPE_VARIABLE
from unimport.constants import (
INITIAL_IMPORTS,
PY38_PLUS,
SUBSCRIPT_TYPE_VARIABLE,
)
from unimport.relate import first_occurrence, get_parents, relate
from unimport.statement import Import, ImportFrom, Name
from unimport.utils import get_dir, get_source, is_std
Expand Down Expand Up @@ -103,6 +107,8 @@ def visit_Import(self, node: ast.Import) -> None:
return
for column, alias in enumerate(node.names):
name = alias.asname or alias.name
if name in INITIAL_IMPORTS:
name = name.split(".")[0]
self.imports.append(
Import(
lineno=node.lineno,
Expand Down

0 comments on commit f78b92b

Please sign in to comment.