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

Add gis_admin_deprecations fixer #484

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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 CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Changelog

`Issue #364 <https://github.com/adamchainz/django-upgrade/issues/364>`__.

* Add Django 4.0+ fixer to rewrite ``OSMGeoAdmin`` and ``GeoModelAdmin`` classes.

Thanks to Adolfo Fitoria in `PR #484 <https://github.com/adamchainz/django-upgrade/pull/484>`__.

* Drop Python 3.8 support.

* Support Python 3.13.
Expand Down
24 changes: 24 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,30 @@ Renames the undocumented ``django.contrib.admin.utils.lookup_needs_distinct`` to
+if lookup_spawns_duplicates(self.opts, search_spec):
...

Deprecated GIS ``ModelAdmin`` classes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

**Name:** ``gis_admin_deprecations``

Rewrites use of the deprecated classes ``OSMGeoAdmin`` and ``GeoModelAdmin`` to the new ``GISModelAdmin``.
The classes were deprecated in `Ticket #34609 <https://code.djangoproject.com/ticket/27674>`__.

.. code-block:: diff

-from django.contrib.gis.admin.options import OSMGeoAdmin
+from django.contrib.gis.admin.options import GISModelAdmin

-class MyAdminClass(OSMGeoAdmin):
+class MyAdminClass(GISModelAdmin):

.. code-block:: diff

-from django.contrib.gis.admin.options import GeoModelAdmin
+from django.contrib.gis.admin.options import GISModelAdmin

-class MyAdminClass(GeoModelAdmin):
+class MyAdminClass(GISModelAdmin):

Compatibility imports
~~~~~~~~~~~~~~~~~~~~~

Expand Down
65 changes: 65 additions & 0 deletions src/django_upgrade/fixers/gis_admin_deprecations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
Rename OSMGeoAdmin to GISModelAdmin in django.contrib.gis.admin.options
due to deprecation on Django 5.0:
https://docs.djangoproject.com/en/5.0/releases/5.0/#features-removed-in-5-0
"""

from __future__ import annotations

import ast
from collections.abc import Iterable
from functools import partial

from tokenize_rt import Offset

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

fixer = Fixer(
__name__,
min_version=(4, 0),
)

MODULE = "django.contrib.gis.admin.options"
RENAMES = {
"OSMGeoAdmin": "GISModelAdmin",
"GeoModelAdmin": "GISModelAdmin",
}


@fixer.register(ast.ImportFrom)
def visit_ImportFrom(
state: State,
node: ast.ImportFrom,
parents: tuple[ast.AST, ...],
) -> Iterable[tuple[Offset, TokenFunc]]:
if node.module == MODULE and is_rewritable_import_from(node):
name_map = {}
for alias in node.names:
if alias.name in RENAMES:
name_map[alias.name] = RENAMES[alias.name]

if name_map:
yield ast_start_offset(node), partial(
update_import_names,
node=node,
name_map=name_map,
)


@fixer.register(ast.Name)
def visit_Name(
state: State,
node: ast.Name,
parents: tuple[ast.AST, ...],
) -> Iterable[tuple[Offset, TokenFunc]]:
if (name := node.id) in RENAMES and name in state.from_imports[MODULE]:
new_name = RENAMES[name]
yield ast_start_offset(node), partial(
find_and_replace_name, name=name, new=new_name
)
110 changes: 110 additions & 0 deletions tests/fixers/test_gis_admin_deprecations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
from __future__ import annotations

from functools import partial

from django_upgrade.data import Settings
from tests.fixers import tools

settings = Settings(target_version=(4, 0))
check_noop = partial(tools.check_noop, settings=settings)
check_transformed = partial(tools.check_transformed, settings=settings)


def test_no_deprecated_alias():
check_noop(
"""\
from django.contrib.gis.admin.options import GeoAdmin

GeoAdmin
""",
)


def test_osm_geo_admin_multiple_inheritance():
check_noop(
"""\
from django.contrib.gis.admin.options import OSMGeoAdmin

class MyModelAdmin(OSMGeoAdmin, other):
pass
""",
)


def test_osm_geo_admin_overloaded_attribute():
check_noop(
"""\
from django.contrib.gis.admin.options import OSMGeoAdmin

class MyModelAdmin(OSMGeoAdmin):
default_lon = 1
""",
)


def test_osm_geo_admin_plain():
check_transformed(
"""\
from django.contrib.gis.admin.options import OSMGeoAdmin

class MyModelAdmin(OSMGeoAdmin):
pass
""",
"""\
from django.contrib.gis.admin.options import GISModelAdmin

class MyModelAdmin(GISModelAdmin):
pass
""",
)


def test_geo_model_admin_plain():
check_transformed(
"""\
from django.contrib.gis.admin.options import GeoModelAdmin

class MyModelAdmin(GeoModelAdmin):
pass
""",
"""\
from django.contrib.gis.admin.options import GISModelAdmin

class MyModelAdmin(GISModelAdmin):
pass
""",
)


def test_osm_geo_admin_aliased():
check_transformed(
"""\
from django.contrib.gis.admin.options import OSMGeoAdmin as GeoAdmin

class MyModelAdmin(GeoAdmin):
pass
""",
"""\
from django.contrib.gis.admin.options import GISModelAdmin as GeoAdmin

class MyModelAdmin(GeoAdmin):
pass
""",
)


def test_geo_model_admin_aliased():
check_transformed(
"""\
from django.contrib.gis.admin.options import GeoModelAdmin as GeoAdmin

class MyModelAdmin(GeoAdmin):
pass
""",
"""\
from django.contrib.gis.admin.options import GISModelAdmin as GeoAdmin

class MyModelAdmin(GeoAdmin):
pass
""",
)
Loading