Skip to content

Commit

Permalink
fix: Skip star imports when the --include-star-import flag is not used.
Browse files Browse the repository at this point in the history
  • Loading branch information
hakancelikdev committed Jun 8, 2020
1 parent 297dabb commit 24b6b47
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.

## Unreleased

- [🐞fix: Skip star imports when the `--include-star-import` flag is not used](https://github.com/hakancelik96/unimport/pull/60)

- [🐞fix: finding functions during scanning](https://github.com/hakancelik96/unimport/pull/55)

- [💪 `#noqa` comment support to skip import (#48) by @hakancelik96](https://github.com/hakancelik96/unimport/pull/54)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_refactor.py
Expand Up @@ -193,6 +193,12 @@ def test_comment(self):
expected, self.session.refactor(action),
)

def test_star(self):
action = "from os import *\n" "walk\n"
self.assertEqual(
action, self.session.refactor(action),
)


class TestDuplicateUnusedRefactor(RefactorTestCase):
def test_full_unused(self):
Expand Down
40 changes: 21 additions & 19 deletions unimport/scan.py
Expand Up @@ -56,29 +56,31 @@ def visit_FunctionDef(self, node):
def visit_Import(self, node, module_name=None):
if self.skip_import(node):
return
star = False
module = None
for alias in node.names:
name = alias.asname or alias.name
package = module_name or alias.name
if (
package not in self.ignore_imports
and name not in self.ignore_import_names
alias_name = alias.asname or alias.name
star = True if alias_name == "*" else False
name = package if star else alias_name
is_package_or_name_ignore = (
package in self.ignore_imports
or name in self.ignore_import_names
)
if is_package_or_name_ignore or (
star and not self.include_star_import
):
if name == "*":
star = True
name = package
with contextlib.suppress(ImportError, ValueError):
module = importlib.import_module(package)
self.imports.append(
{
"lineno": node.lineno,
"name": name,
"star": star,
"module": module,
"modules": [],
}
)
return
with contextlib.suppress(ImportError, ValueError):
module = importlib.import_module(package)
self.imports.append(
{
"lineno": node.lineno,
"name": name,
"star": star,
"module": module,
"modules": [],
}
)

@recursive
def visit_ImportFrom(self, node):
Expand Down

0 comments on commit 24b6b47

Please sign in to comment.