Skip to content

Commit

Permalink
🐞 Fix: Initial imports from sys.modules #127 (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
hakancelikdev authored Oct 7, 2020
1 parent f657154 commit 9170498
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.

## [Unreleased] - ././2020

- [🐞 Fix: Initial imports from sys.modules by @hakancelik96](https://github.com/hakancelik96/unimport/pull/136)
- [🐞 Fix: import and name matcher by @hakancelik96](https://github.com/hakancelik96/unimport/pull/133)
- [🐞 Fix: type comment by @hakancelik96](https://github.com/hakancelik96/unimport/pull/130)
- [💪 Support type variable by @hakancelik96](https://github.com/hakancelik96/unimport/pull/128)
Expand Down
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
17 changes: 17 additions & 0 deletions unimport/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,20 @@
SUBSCRIPT_TYPE_VARIABLE.extend(["Literal", "OrderedDict"])
elif PY37_PLUS:
SUBSCRIPT_TYPE_VARIABLE.append("Literal")

INITIAL_IMPORTS = frozenset(
# https://docs.python.org/3/library/sys.html#sys.modules
# The first thing Python will do is look up the name of import in sys.modules.
# Initial modules are below.
{
"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 9170498

Please sign in to comment.