browniebroke / django-codemod

A tool to automatically fix Django deprecations.

Home Page:https://django-codemod.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Local variable shadowing import from outer scope

browniebroke opened this issue · comments

Describe the bug

In case something is being renamed by a codemodder in the current file, and if a local variable shadows this name from the outer scope, the shadowed variable gets renamed as well.

The code to change should avoid these kind of shadowing, but ideally we should not be tricked by it.

To Reproduce

from django.conf.urls import url
from django.urls import reverse_lazy


class MyModelAdmin(admin.ModelAdmin):
    def get_urls(self):
        return [
            url(r'^some-route/$', some_view, name=self.store_value_view_name),
        ] + super().get_urls()

    def get_result_url(self):
        url = reverse_lazy('some-url-name')
        return url

When running URLTransformer on this code, the variable url inside get_result_url gets incorrectly renamed to re_path.

Additional context
Add any other context about the problem here.

More problematic, the file also conatined a form class with an attribute called url:

class MyFilter(BaseFilter):
    url = reverse_lazy('some-url-name')

The BaseFilter class is expecting this url attribute to be set in its subclasses, this name isn't shadowing the import.