Skip to content

Commit

Permalink
fix: disable rename parent module imported
Browse files Browse the repository at this point in the history
As per #421, this is not working currently, so I'm putting this feature behind an
experimental feature flag until it's fixed properly.
  • Loading branch information
browniebroke committed Jun 17, 2021
1 parent eecc871 commit b717fbb
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pytest


@pytest.fixture()
def parent_module_import_enabled(mocker):
yield mocker.patch(
"django_codemod.visitors.base.REPLACE_PARENT_MODULE_IMPORTED", True
)
5 changes: 5 additions & 0 deletions django_codemod/feature_flags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import os

REPLACE_PARENT_MODULE_IMPORTED = (
os.getenv("EXPERIMENTAL_REPLACE_PARENT_MODULE_IMPORTED") == "1"
)
4 changes: 4 additions & 0 deletions django_codemod/visitors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from libcst.codemod.visitors import AddImportsVisitor
from libcst.metadata import ParentNodeProvider, Scope, ScopeProvider

from django_codemod.feature_flags import REPLACE_PARENT_MODULE_IMPORTED


class BaseDjCodemodTransformer(ContextAwareTransformer, ABC):
deprecated_in: Tuple[int, int]
Expand Down Expand Up @@ -142,6 +144,8 @@ def _check_import_from_parent(
from parent import module
"""
if not REPLACE_PARENT_MODULE_IMPORTED:
return None
# First, exit early if 'import *' is used
if isinstance(updated_node.names, ImportStar):
return None
Expand Down
5 changes: 5 additions & 0 deletions tests/visitors/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def test_simple_substitution(self) -> None:
"""
self.assertCodemod(before, after)

@pytest.mark.usefixtures("parent_module_import_enabled")
def test_parent_module(self) -> None:
before = """
from django.dummy import module
Expand Down Expand Up @@ -87,6 +88,7 @@ def test_reference_without_call(self) -> None:
"""
self.assertCodemod(before, after)

@pytest.mark.usefixtures("parent_module_import_enabled")
def test_parent_reference_without_call(self) -> None:
before = """
from django.dummy import module
Expand Down Expand Up @@ -300,6 +302,7 @@ def test_simple_substitution(self) -> None:
"""
self.assertCodemod(before, after)

@pytest.mark.usefixtures("parent_module_import_enabled")
def test_parent_module(self) -> None:
before = """
from django.dummy import module
Expand All @@ -313,6 +316,7 @@ def test_parent_module(self) -> None:
"""
self.assertCodemod(before, after)

@pytest.mark.usefixtures("parent_module_import_enabled")
def test_parent_module_with_other(self) -> None:
before = """
from django.dummy import other_mod, module
Expand All @@ -327,6 +331,7 @@ def test_parent_module_with_other(self) -> None:
"""
self.assertCodemod(before, after)

@pytest.mark.usefixtures("parent_module_import_enabled")
def test_parent_module_import_alias(self) -> None:
before = """
from django.dummy import module as django_module
Expand Down
2 changes: 2 additions & 0 deletions tests/visitors/test_models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
from parameterized import parameterized

from django_codemod.visitors.models import (
Expand Down Expand Up @@ -369,6 +370,7 @@ class MyThing(Model):
"""
self.assertCodemod(before, after)

@pytest.mark.usefixtures("parent_module_import_enabled")
def test_simple_substitution(self) -> None:
before = """
from django.db import models
Expand Down
8 changes: 8 additions & 0 deletions tests/visitors/test_timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,11 @@ def test_with_name_args(self) -> None:
"""

self.assertCodemod(before, after)

def test_issue_421(self) -> None:
before = after = """
from django.utils import timezone
now = timezone.now()
"""
self.assertCodemod(before, after)

0 comments on commit b717fbb

Please sign in to comment.