Skip to content

Commit

Permalink
Add some compatibility imports (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownPlatypus authored Sep 24, 2023
1 parent 4921670 commit 3ca1aac
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Changelog

Thanks to Thibaut Decombe in `PR #369 <https:/adamchainz/django-upgrade/pull/369>`__.

* Add some compatibility import replacements for Django 4.0 and below.

Thanks to Thibaut Decombe in `PR #368 <https:/adamchainz/django-upgrade/pull/368>`__.

1.14.1 (2023-08-16)
-------------------

Expand Down
60 changes: 60 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,22 @@ If a ``register()`` call is preceded by an ``unregister()`` call that includes t
admin.site.unregister(MyModel1)
admin.site.register(MyModel1, MyCustomAdmin)
Compatibility imports
~~~~~~~~~~~~~~~~~~~~~

Rewrites some compatibility imports:

* ``django.contrib.admin.helpers.ACTION_CHECKBOX_NAME`` in ``django.contrib.admin``
* ``django.template.context.BaseContext``, ``django.template.context.Context``, ``django.template.context.ContextPopException`` and ``django.template.context.RequestContext`` in ``django.template.base``

.. code-block:: diff
-from django.contrib.admin import ACTION_CHECKBOX_NAME
+from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
-from django.template.base import Context
+from django.template.context import Context
Django 1.9
-----------

Expand Down Expand Up @@ -277,6 +293,7 @@ Rewrites some compatibility imports:

* ``django.forms.utils.pretty_name`` in ``django.forms.forms``
* ``django.forms.boundfield.BoundField`` in ``django.forms.forms``
* ``django.forms.widgets.SelectDateWidget`` in ``django.forms.extras``

Whilst mentioned in the `Django 3.1 release notes <https://docs.djangoproject.com/en/3.1/releases/3.1/#id1>`_, these have been possible since Django 1.9.

Expand All @@ -285,6 +302,27 @@ Whilst mentioned in the `Django 3.1 release notes <https://docs.djangoproject.co
-from django.forms.forms import pretty_name
+from django.forms.utils import pretty_name
Django 1.10
-----------

`Release Notes <https://docs.djangoproject.com/en/1.10/releases/1.10/>`__

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

Rewrites some compatibility imports:

* ``django.templatetags.static.static`` in ``django.contrib.staticfiles.templatetags.staticfiles``

Whilst mentioned in the `Django 2.1 release notes <https://docs.djangoproject.com/en/2.1/releases/2.1/#features-deprecated-in-2-1>`_, these have been possible since Django 1.10.


.. code-block:: diff
-from django.contrib.staticfiles.templatetags.staticfiles import static
+from django.templatetags.static import static
Django 1.11
-----------

Expand Down Expand Up @@ -379,6 +417,16 @@ Rewrites imports of ``lru_cache`` from ``django.utils.functional`` to use ``func
-from django.utils.functional import lru_cache
+from functools import lru_cache
``ContextDecorator``
~~~~~~~~~~~~~~~~~~~~

Rewrites imports of ``ContextDecorator`` from ``django.utils.decorators`` to use ``contextlib``.

.. code-block:: diff
-from django.utils.decorators import ContextDecorator
+from contextlib import ContextDecorator
``<func>.allow_tags = True``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -780,6 +828,18 @@ Renames the undocumented ``django.contrib.admin.utils.lookup_needs_distinct`` to
+if lookup_spawns_duplicates(self.opts, search_spec):
...
Compatibility imports
~~~~~~~~~~~~~~~~~~~~~

Rewrites some compatibility imports:

* ``django.utils.translation.template.TRANSLATOR_COMMENT_MARK`` in ``django.template.base``

.. code-block:: diff
-from django.template.base import TRANSLATOR_COMMENT_MARK
+from django.utils.translation.template import TRANSLATOR_COMMENT_MARK
Django 4.1
----------

Expand Down
28 changes: 27 additions & 1 deletion src/django_upgrade/fixers/compatibility_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,28 @@
)

REPLACEMENTS_EXACT = {
(1, 7): {
"django.contrib.admin": {
"ACTION_CHECKBOX_NAME": "django.contrib.admin.helpers",
},
"django.template.base": {
"BaseContext": "django.template.context",
"Context": "django.template.context",
"ContextPopException": "django.template.context",
"RequestContext": "django.template.context",
},
},
(1, 9): {
"django.forms.forms": {
"pretty_name": "django.forms.utils",
"BoundField": "django.forms.boundfield",
},
"django.forms.extras": {"SelectDateWidget": "django.forms.widgets"},
},
(1, 10): {
"django.contrib.staticfiles.templatetags.staticfiles": {
"static": "django.templatetags.static",
},
},
(1, 11): {
"django.db.models.fields": {
Expand All @@ -55,8 +72,12 @@
"django.db.models.sql.datastructures": {
"EmptyResultSet": "django.core.exceptions",
},
"django.test.runner": {"setup_databases": "django.test.utils"},
},
(2, 0): {
"django.utils.functional": {"lru_cache": "functools"},
"django.utils.decorators": {"ContextDecorator": "contextlib"},
},
(2, 0): {"django.utils.functional": {"lru_cache": "functools"}},
(3, 1): {
"django.contrib.postgres.forms": {
"JSONField": "django.forms",
Expand All @@ -65,6 +86,11 @@
"JSONField": "django.forms",
},
},
(4, 0): {
"django.template.base": {
"TRANSLATOR_COMMENT_MARK": "django.utils.translation.template"
}
},
}
REPLACEMENTS_EXCEPT_MIGRATIONS = {
(3, 1): {
Expand Down

0 comments on commit 3ca1aac

Please sign in to comment.