Skip to content

Commit

Permalink
Merge pull request #74 from browniebroke/feature/python2-compat
Browse files Browse the repository at this point in the history
  • Loading branch information
browniebroke authored Jun 2, 2020
2 parents 6e8537d + 8a8917d commit 3be7518
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 5 deletions.
11 changes: 10 additions & 1 deletion django_codemod/visitors/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from .admin import InlineHasAddPermissionsTransformer
from .encoding import ForceTextTransformer, SmartTextTransformer
from .encoding import (
ForceTextTransformer,
SmartTextTransformer,
UnicodeCompatibleTransformer,
)
from .html import UnescapeEntitiesTransformer
from .http import (
HttpUrlQuotePlusTransformer,
Expand All @@ -8,6 +12,8 @@
HttpUrlUnQuoteTransformer,
IsSafeUrlTransformer,
)
from .lru_cache import LRUCacheTransformer
from .os_utils import AbsPathTransformer
from .shortcuts import RenderToResponseTransformer
from .translations import (
UGetTextLazyTransformer,
Expand All @@ -19,13 +25,15 @@
from .urls import URLTransformer

__all__ = (
"AbsPathTransformer",
"ForceTextTransformer",
"HttpUrlQuotePlusTransformer",
"HttpUrlQuoteTransformer",
"HttpUrlUnQuotePlusTransformer",
"HttpUrlUnQuoteTransformer",
"InlineHasAddPermissionsTransformer",
"IsSafeUrlTransformer",
"LRUCacheTransformer",
"RenderToResponseTransformer",
"SmartTextTransformer",
"UGetTextLazyTransformer",
Expand All @@ -35,4 +43,5 @@
"UNGetTextTransformer",
"URLTransformer",
"UnescapeEntitiesTransformer",
"UnicodeCompatibleTransformer",
)
11 changes: 10 additions & 1 deletion django_codemod/visitors/encoding.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django_codemod.constants import DJANGO_30, DJANGO_40
from django_codemod.constants import DJANGO_20, DJANGO_30, DJANGO_40
from django_codemod.visitors.base import BaseSimpleFuncRenameTransformer


Expand All @@ -18,3 +18,12 @@ class SmartTextTransformer(BaseSimpleFuncRenameTransformer):
removed_in = DJANGO_40
rename_from = "django.utils.encoding.smart_text"
rename_to = "django.utils.encoding.smart_str"


class UnicodeCompatibleTransformer(BaseSimpleFuncRenameTransformer):
"""Resolve deprecation of ``django.utils.encoding.python_2_unicode_compatible``."""

deprecated_in = DJANGO_20
removed_in = DJANGO_30
rename_from = "django.utils.encoding.python_2_unicode_compatible"
rename_to = "six.python_2_unicode_compatible"
11 changes: 11 additions & 0 deletions django_codemod/visitors/lru_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django_codemod.constants import DJANGO_20, DJANGO_30
from django_codemod.visitors.base import BaseSimpleFuncRenameTransformer


class LRUCacheTransformer(BaseSimpleFuncRenameTransformer):
"""Resolve deprecation of ``django.utils.lru_cache.lru_cache``."""

deprecated_in = DJANGO_20
removed_in = DJANGO_30
rename_from = "django.utils.lru_cache.lru_cache"
rename_to = "functools.lru_cache"
11 changes: 11 additions & 0 deletions django_codemod/visitors/os_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django_codemod.constants import DJANGO_20, DJANGO_30
from django_codemod.visitors.base import BaseSimpleFuncRenameTransformer


class AbsPathTransformer(BaseSimpleFuncRenameTransformer):
"""Resolve deprecation of ``django.utils._os.abspathu``."""

deprecated_in = DJANGO_20
removed_in = DJANGO_30
rename_from = "django.utils._os.abspathu"
rename_to = "os.path.abspath"
3 changes: 3 additions & 0 deletions docs/codemods.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Applied by passing the ``--removed-in 3.0`` or ``--deprecated-in 2.0`` option:

- Replaces ``render_to_response()`` by ``render()`` and add ``request=None``
as the first argument of ``render()``.
- Replaces ``django.utils.lru_cache.lru_cache()`` by the function it's an alias of: ``functools.lru_cache()``.
- Replaces ``django.utils._os.abspathu()`` by the function it's an alias of: ``os.path.abspath()``.
- Replaces ``django.utils.encoding.python_2_unicode_compatible()`` by the function it's an alias of: ``six.python_2_unicode_compatible()``.

Applied by passing the ``--removed-in 3.0`` or ``--deprecated-in 2.1`` option:

Expand Down
15 changes: 13 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,12 @@ def test_deprecated_in_mapping():
"UnescapeEntitiesTransformer",
],
(2, 1): ["InlineHasAddPermissionsTransformer"],
(2, 0): ["RenderToResponseTransformer"],
(2, 0): [
"AbsPathTransformer",
"LRUCacheTransformer",
"RenderToResponseTransformer",
"UnicodeCompatibleTransformer",
],
}


Expand All @@ -147,5 +152,11 @@ def test_removed_in_mapping():
"URLTransformer",
"UnescapeEntitiesTransformer",
],
(3, 0): ["InlineHasAddPermissionsTransformer", "RenderToResponseTransformer"],
(3, 0): [
"AbsPathTransformer",
"InlineHasAddPermissionsTransformer",
"LRUCacheTransformer",
"RenderToResponseTransformer",
"UnicodeCompatibleTransformer",
],
}
29 changes: 28 additions & 1 deletion tests/visitors/test_encoding.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from django_codemod.visitors import ForceTextTransformer, SmartTextTransformer
from django_codemod.visitors import (
ForceTextTransformer,
SmartTextTransformer,
UnicodeCompatibleTransformer,
)
from tests.visitors.base import BaseVisitorTest


Expand Down Expand Up @@ -37,3 +41,26 @@ def test_simple_substitution(self) -> None:
result = smart_str(content)
"""
self.assertCodemod(before, after)


class TestUnicodeCompatibleTransformer(BaseVisitorTest):

transformer = UnicodeCompatibleTransformer

def test_simple_substitution(self) -> None:
"""Check simple use case."""
before = """
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
class MyModel:
pass
"""
after = """
from six import python_2_unicode_compatible
@python_2_unicode_compatible
class MyModel:
pass
"""
self.assertCodemod(before, after)
20 changes: 20 additions & 0 deletions tests/visitors/test_lru_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django_codemod.visitors import LRUCacheTransformer
from tests.visitors.base import BaseVisitorTest


class TestLRUCacheTransformer(BaseVisitorTest):

transformer = LRUCacheTransformer

def test_simple_substitution(self) -> None:
before = """
from django.utils.lru_cache import lru_cache
result = lru_cache(content)
"""
after = """
from functools import lru_cache
result = lru_cache(content)
"""
self.assertCodemod(before, after)
20 changes: 20 additions & 0 deletions tests/visitors/test_os_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django_codemod.visitors import AbsPathTransformer
from tests.visitors.base import BaseVisitorTest


class TestAbsPathTransformer(BaseVisitorTest):

transformer = AbsPathTransformer

def test_simple_substitution(self) -> None:
before = """
from django.utils._os import abspathu
result = abspathu(content)
"""
after = """
from os.path import abspath
result = abspath(content)
"""
self.assertCodemod(before, after)

0 comments on commit 3be7518

Please sign in to comment.