From 13f9f1aad3f13e68b4c1bac6da09b7bcb2dab38f Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Sun, 10 May 2020 12:23:10 +0100 Subject: [PATCH] Support resolving django.utils.translation.ungettext_lazy deprecation --- README.rst | 3 ++ django_codemod/commands/django_40.py | 8 +++++ tests/commands/test_django_40.py | 51 ++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/README.rst b/README.rst index 65f97c9f..105f06d6 100644 --- a/README.rst +++ b/README.rst @@ -48,6 +48,9 @@ Currently implemented: * ``django_codemod.commands.django_40.UNGetTextToNGetTextCommand``: migrate deprecated ``ungettext()`` function to ``ngettext()``. +* ``django_codemod.commands.django_40.UNGetTextLazyToNGetTextLazyCommand``: migrate deprecated + ``ungettext_lazy()`` function to ``ngettext_lazy()``. + Credits ------- diff --git a/django_codemod/commands/django_40.py b/django_codemod/commands/django_40.py index c2572fde..2391b5ee 100644 --- a/django_codemod/commands/django_40.py +++ b/django_codemod/commands/django_40.py @@ -115,3 +115,11 @@ class UNGetTextToNGetTextCommand(UGetTextToGetTextCommand): DESCRIPTION: str = "Replaces ungettext() by ngettext()." old_name = "ungettext" new_name = "ngettext" + + +class UNGetTextLazyToNGetTextLazyCommand(UGetTextToGetTextCommand): + """Help resolve deprecation of django.utils.translation.ungettext_lazy.""" + + DESCRIPTION: str = "Replaces ungettext_lazy() by ngettext_lazy()." + old_name = "ungettext_lazy" + new_name = "ngettext_lazy" diff --git a/tests/commands/test_django_40.py b/tests/commands/test_django_40.py index c2f169cf..71b877c3 100644 --- a/tests/commands/test_django_40.py +++ b/tests/commands/test_django_40.py @@ -7,6 +7,7 @@ UGetTextLazyToGetTextLazyCommand, UGetTextNoopToGetTextNoopCommand, UNGetTextToNGetTextCommand, + UNGetTextLazyToNGetTextLazyCommand, ) @@ -328,3 +329,53 @@ def test_already_imported_substitution(self) -> None: result = ngettext(content, plural_content, count) """ self.assertCodemod(before, after) + + +class TestUNGetTextLazyToNGetTextLazyCommand(CodemodTest): + + TRANSFORM = UNGetTextLazyToNGetTextLazyCommand + + def test_noop(self) -> None: + """Test when nothing should change.""" + before = """ + from django import conf + from django.utils import translation + + foo = ngettext_lazy("bar", "bars", count) + """ + after = """ + from django import conf + from django.utils import translation + + foo = ngettext_lazy("bar", "bars", count) + """ + + self.assertCodemod(before, after) + + def test_simple_substitution(self) -> None: + """Check simple use case.""" + before = """ + from django.utils.translation import ungettext_lazy + + result = ungettext_lazy(content, plural_content, count) + """ + after = """ + from django.utils.translation import ngettext_lazy + + result = ngettext_lazy(content, plural_content, count) + """ + self.assertCodemod(before, after) + + def test_already_imported_substitution(self) -> None: + """Test case where ngettext_lazy is already in the imports.""" + before = """ + from django.utils.translation import ungettext_lazy, ngettext_lazy + + result = ungettext_lazy(content, plural_content, count) + """ + after = """ + from django.utils.translation import ngettext_lazy + + result = ngettext_lazy(content, plural_content, count) + """ + self.assertCodemod(before, after)