Skip to content

Commit

Permalink
fix: error with star import
Browse files Browse the repository at this point in the history
When the old module appears as star import, it crashes with:

for import_alias in old_names:
TypeError: 'ImportStar' object is not iterable

We could probably handle renaming in this case,
but let avoid a crash for now.
  • Loading branch information
Bruno Alla authored and browniebroke committed Jan 28, 2021
1 parent 12359a7 commit 56d09b7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 4 additions & 2 deletions django_codemod/visitors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ def leave_ImportFrom(
self, original_node: ImportFrom, updated_node: ImportFrom
) -> Union[BaseSmallStatement, RemovalSentinel]:
"""Update import statements for matching old module name."""
if not import_from_matches(updated_node, self.old_module_parts):
return super().leave_ImportFrom(original_node, updated_node)
if not import_from_matches(updated_node, self.old_module_parts) or isinstance(
updated_node.names, ImportStar
):
return updated_node
# This is a match
new_names = list(self.gen_new_imported_names(updated_node.names))
self.save_import_scope(original_node)
Expand Down
14 changes: 14 additions & 0 deletions tests/visitors/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ def test_import_with_alias(self) -> None:
"""
self.assertCodemod(before, after)

def test_import_star_ignored(self) -> None:
"""Should not change anything in case of a star import."""
before = """
from django.dummy.module import *
result = func()
"""
after = """
from django.dummy.module import *
result = func()
"""
self.assertCodemod(before, after)

def test_same_name_function(self) -> None:
"""Should not be fooled by a function bearing the same name."""
before = """
Expand Down

0 comments on commit 56d09b7

Please sign in to comment.