Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #127 #161

Merged
merged 2 commits into from
Jan 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file.

## [Unreleased] - /././

- [Fix by @hakancelik96 #127](https://github.com/hakancelik96/unimport/pull/161)

## [0.7.0] - 26/December/2020

- [%15 performance increase & remove show-error flag by @hakancelik96](https://github.com/hakancelik96/unimport/pull/159)
Expand Down
12 changes: 0 additions & 12 deletions tests/test_refactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,18 +254,6 @@ def test_get_star_imp_none(self):
""",
)

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.refactor(action) for action in actions],
)


class TestDuplicateUnusedRefactor(RefactorTestCase):
def test_full_unused(self):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_unused_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def assertSourceAfterScanningEqualToExpected(


class TestBuiltin(UnusedTestCase):
# https://github.com/hakancelik96/unimport/issues/45

def test_ConnectionError(self):
self.assertSourceAfterScanningEqualToExpected(
"""\
Expand Down Expand Up @@ -1187,3 +1189,16 @@ def x(t=x):pass
),
],
)


class TestDealingImplicitImportsSubPackages(UnusedTestCase):
# https://github.com/hakancelik96/unimport/issues/127

def test(self):
self.assertSourceAfterScanningEqualToExpected(
"""\
import x.y
x
""",
)
18 changes: 0 additions & 18 deletions unimport/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"VERSION",
"PY38_PLUS",
"SUBSCRIPT_TYPE_VARIABLE",
"INITIAL_IMPORTS",
"IGNORE_IMPORT_NAMES",
"BUILTINS",
"Function",
Expand Down Expand Up @@ -85,23 +84,6 @@
}
)

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",
}
)

IGNORE_IMPORT_NAMES = frozenset({"__all__", "__doc__", "__name__"})
BUILTINS = frozenset(dir(builtins))

Expand Down
2 changes: 0 additions & 2 deletions unimport/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ 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 C.INITIAL_IMPORTS:
name = name.split(".")[0]
self.imports.append(
Import(
lineno=node.lineno,
Expand Down
30 changes: 15 additions & 15 deletions unimport/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class Import(NamedTuple):
def __len__(self) -> int:
return operator.length_hint(self.name.split("."))

def is_match_sub_packages(self, name_name) -> bool:
return self.name.split(".")[0] == name_name


class ImportFrom(NamedTuple):
lineno: int
Expand All @@ -25,6 +28,9 @@ class ImportFrom(NamedTuple):
def __len__(self) -> int:
return operator.length_hint(self.name.split("."))

def is_match_sub_packages(self, name_name):
return False


class Name(NamedTuple):
lineno: int
Expand All @@ -36,20 +42,14 @@ def is_attribute(self):
return "." in self.name

def match(self, imp: Union[Import, ImportFrom]) -> bool:
if self.is_attribute:
return self.__attribute_match(imp)
else:
return self.__name_match(imp)

def __attribute_match(self, imp: Union[Import, ImportFrom]) -> bool:
"""if the name is a attribute."""
match = ".".join(self.name.split(".")[: len(imp)]) == imp.name
return imp.lineno < self.lineno and match

def __name_match(self, imp: Union[Import, ImportFrom]) -> bool:
"""if the name is a normal name."""
match = self.name == imp.name
if self.is_all:
return match
return self.name == imp.name
elif self.is_attribute:
return imp.lineno < self.lineno and (
".".join(self.name.split(".")[: len(imp)]) == imp.name
or imp.is_match_sub_packages(self.name)
)
else:
return imp.lineno < self.lineno and match
return (imp.lineno < self.lineno) and (
self.name == imp.name or imp.is_match_sub_packages(self.name)
)
5 changes: 2 additions & 3 deletions unimport/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
import contextlib
import difflib
import functools
import importlib
import importlib.machinery # unimport: skip
import importlib.util # unimport: skip
import importlib.machinery
import importlib.util
import re
import tokenize
from distutils.util import strtobool
Expand Down