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 for star imports #71

Merged
merged 1 commit into from
Sep 22, 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 HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
History
=======

* Fix import fixers to not crash on star imports (``from foo import *``).

Thanks to Mikhail for the report in `Issue #70 <https://github.com/adamchainz/django-upgrade/issues/70>`__.

1.3.0 (2021-09-22)
------------------

Expand Down
5 changes: 3 additions & 2 deletions src/django_upgrade/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ def ast_start_offset(node: Union[ast.expr, ast.keyword, ast.stmt]) -> Offset:
return Offset(node.lineno, node.col_offset)


# def ast_end_offset(node: Union[ast.expr, ast.keyword, ast.stmt]) -> Offset:
# return Offset(node.end_lineno, node.end_col_offset)
def is_rewritable_import_from(node: ast.ImportFrom) -> bool:
# Not relative import or import *
return node.level == 0 and not (len(node.names) == 1 and node.names[0].name == "*")
4 changes: 3 additions & 1 deletion src/django_upgrade/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ def visit(
)
):
state.from_imports[node.module].update(
name.name for name in node.names if not name.asname
name.name
for name in node.names
if name.asname is None and name.name != "*"
)

for name in reversed(node._fields):
Expand Down
6 changes: 3 additions & 3 deletions src/django_upgrade/fixers/compatibility_imports_1_11.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from tokenize_rt import Offset

from django_upgrade.ast import ast_start_offset
from django_upgrade.ast import ast_start_offset, is_rewritable_import_from
from django_upgrade.data import Fixer, State, TokenFunc
from django_upgrade.tokens import update_import_modules

Expand Down Expand Up @@ -40,8 +40,8 @@ def visit_ImportFrom(
parent: ast.AST,
) -> Iterable[Tuple[Offset, TokenFunc]]:
if (
node.level == 0
and node.module in REWRITES
node.module in REWRITES
and is_rewritable_import_from(node)
and any(alias.name in REWRITES[node.module] for alias in node.names)
):
yield ast_start_offset(node), partial(
Expand Down
6 changes: 3 additions & 3 deletions src/django_upgrade/fixers/compatibility_imports_1_9.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from tokenize_rt import Offset

from django_upgrade.ast import ast_start_offset
from django_upgrade.ast import ast_start_offset, is_rewritable_import_from
from django_upgrade.data import Fixer, State, TokenFunc
from django_upgrade.tokens import update_import_modules

Expand All @@ -32,8 +32,8 @@ def visit_ImportFrom(
parent: ast.AST,
) -> Iterable[Tuple[Offset, TokenFunc]]:
if (
node.level == 0
and node.module in REWRITES
node.module in REWRITES
and is_rewritable_import_from(node)
and any(alias.name in REWRITES[node.module] for alias in node.names)
):
yield ast_start_offset(node), partial(
Expand Down
6 changes: 3 additions & 3 deletions src/django_upgrade/fixers/django_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from tokenize_rt import Offset, Token

from django_upgrade.ast import ast_start_offset
from django_upgrade.ast import ast_start_offset, is_rewritable_import_from
from django_upgrade.compat import str_removeprefix, str_removesuffix
from django_upgrade.data import Fixer, State, TokenFunc
from django_upgrade.tokens import (
Expand All @@ -35,8 +35,8 @@ def visit_ImportFrom(
parent: ast.AST,
) -> Iterable[Tuple[Offset, TokenFunc]]:
if (
node.level == 0
and node.module == "django.conf.urls"
node.module == "django.conf.urls"
and is_rewritable_import_from(node)
and any(alias.name in ("include", "url") for alias in node.names)
):
yield ast_start_offset(node), partial(
Expand Down
6 changes: 3 additions & 3 deletions src/django_upgrade/fixers/jsonfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from tokenize_rt import Offset

from django_upgrade.ast import ast_start_offset
from django_upgrade.ast import ast_start_offset, is_rewritable_import_from
from django_upgrade.data import Fixer, State, TokenFunc
from django_upgrade.tokens import update_import_modules

Expand Down Expand Up @@ -44,8 +44,8 @@ def visit_ImportFrom(
parent: ast.AST,
) -> Iterable[Tuple[Offset, TokenFunc]]:
if (
node.level == 0
and node.module in REWRITES
node.module in REWRITES
and is_rewritable_import_from(node)
and any(alias.name in REWRITES[node.module] for alias in node.names)
):
yield ast_start_offset(node), partial(
Expand Down
4 changes: 2 additions & 2 deletions src/django_upgrade/fixers/null_boolean_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from tokenize_rt import Offset, Token

from django_upgrade.ast import ast_start_offset
from django_upgrade.ast import ast_start_offset, is_rewritable_import_from
from django_upgrade.data import Fixer, State, TokenFunc
from django_upgrade.tokens import (
CODE,
Expand All @@ -31,7 +31,7 @@ def visit_ImportFrom(
node: ast.ImportFrom,
parent: ast.AST,
) -> Iterable[Tuple[Offset, TokenFunc]]:
if node.level == 0 and node.module == "django.db.models":
if is_rewritable_import_from(node) and node.module == "django.db.models":
yield ast_start_offset(node), partial(
update_import_names,
node=node,
Expand Down
6 changes: 3 additions & 3 deletions src/django_upgrade/fixers/postgres_float_range_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from tokenize_rt import Offset

from django_upgrade.ast import ast_start_offset
from django_upgrade.ast import ast_start_offset, is_rewritable_import_from
from django_upgrade.data import Fixer, State, TokenFunc
from django_upgrade.tokens import find_and_replace_name, update_import_names

Expand All @@ -35,8 +35,8 @@ def visit_ImportFrom(
parent: ast.AST,
) -> Iterable[Tuple[Offset, TokenFunc]]:
if (
node.level == 0
and node.module in MODULES
node.module in MODULES
and is_rewritable_import_from(node)
and any(alias.name in NAME_MAP for alias in node.names)
):
yield ast_start_offset(node), partial(
Expand Down
4 changes: 2 additions & 2 deletions src/django_upgrade/fixers/queryset_paginator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from tokenize_rt import Offset

from django_upgrade.ast import ast_start_offset
from django_upgrade.ast import ast_start_offset, is_rewritable_import_from
from django_upgrade.data import Fixer, State, TokenFunc
from django_upgrade.tokens import find_and_replace_name, update_import_names

Expand All @@ -29,7 +29,7 @@ def visit_ImportFrom(
node: ast.ImportFrom,
parent: ast.AST,
) -> Iterable[Tuple[Offset, TokenFunc]]:
if node.level == 0 and node.module == MODULE:
if node.module == MODULE and is_rewritable_import_from(node):
yield ast_start_offset(node), partial(
update_import_names, node=node, name_map=NAMES
)
Expand Down
6 changes: 3 additions & 3 deletions src/django_upgrade/fixers/timezone_fixedoffset.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from tokenize_rt import Offset, Token

from django_upgrade.ast import ast_start_offset
from django_upgrade.ast import ast_start_offset, is_rewritable_import_from
from django_upgrade.data import Fixer, State, TokenFunc
from django_upgrade.tokens import (
OP,
Expand Down Expand Up @@ -36,8 +36,8 @@ def visit_ImportFrom(
parent: ast.AST,
) -> Iterable[Tuple[Offset, TokenFunc]]:
if (
node.level == 0
and node.module == MODULE
node.module == MODULE
and is_rewritable_import_from(node)
and any(alias.name == OLD_NAME for alias in node.names)
):
yield ast_start_offset(node), partial(fix_import_from, node=node)
Expand Down
4 changes: 2 additions & 2 deletions src/django_upgrade/fixers/utils_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from tokenize_rt import Offset

from django_upgrade.ast import ast_start_offset
from django_upgrade.ast import ast_start_offset, is_rewritable_import_from
from django_upgrade.data import Fixer, State, TokenFunc
from django_upgrade.tokens import find_and_replace_name, update_import_names

Expand All @@ -30,7 +30,7 @@ def visit_ImportFrom(
node: ast.ImportFrom,
parent: ast.AST,
) -> Iterable[Tuple[Offset, TokenFunc]]:
if node.level == 0 and node.module == MODULE:
if node.module == MODULE and is_rewritable_import_from(node):
yield ast_start_offset(node), partial(
update_import_names, node=node, name_map=NAMES
)
Expand Down
4 changes: 2 additions & 2 deletions src/django_upgrade/fixers/utils_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from tokenize_rt import Offset, Token

from django_upgrade.ast import ast_start_offset
from django_upgrade.ast import ast_start_offset, is_rewritable_import_from
from django_upgrade.data import Fixer, State, TokenFunc
from django_upgrade.tokens import (
extract_indent,
Expand Down Expand Up @@ -40,7 +40,7 @@ def visit_ImportFrom(
node: ast.ImportFrom,
parent: ast.AST,
) -> Iterable[Tuple[Offset, TokenFunc]]:
if node.level == 0 and node.module == MODULE:
if node.module == MODULE and is_rewritable_import_from(node):
name_map = {}
urllib_names = {}
for alias in node.names:
Expand Down
6 changes: 3 additions & 3 deletions src/django_upgrade/fixers/utils_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from tokenize_rt import Offset

from django_upgrade.ast import ast_start_offset
from django_upgrade.ast import ast_start_offset, is_rewritable_import_from
from django_upgrade.data import Fixer, State, TokenFunc
from django_upgrade.tokens import find_and_replace_name, update_import_names

Expand All @@ -34,8 +34,8 @@ def visit_ImportFrom(
parent: ast.AST,
) -> Iterable[Tuple[Offset, TokenFunc]]:
if (
node.level == 0
and node.module == MODULE
node.module == MODULE
and is_rewritable_import_from(node)
and any(alias.name in NAME_MAP for alias in node.names)
):
yield ast_start_offset(node), partial(
Expand Down
16 changes: 16 additions & 0 deletions tests/fixers/test_compatibility_imports_1_9.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ def test_unrecognized_import_format():
)


def test_import_star():
check_transformed(
"""\
from django.forms.forms import *

pretty_name()
""",
"""\
from django.forms.forms import *

pretty_name()
""",
settings,
)


def test_name_imported():
check_transformed(
"""\
Expand Down