Skip to content

Commit 00ddb21

Browse files
Add SimpleCookie compatibility import fixer (#537)
Fixes #535 --------- Co-authored-by: Adam Johnson <[email protected]>
1 parent cf14cf2 commit 00ddb21

File tree

4 files changed

+17
-25
lines changed

4 files changed

+17
-25
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
Changelog
33
=========
44

5+
* Add Django 2.0+ compatibility import rewrite of `django.http.cookie.SimpleCookie` to `http.cookies.SimpleCookie`.
6+
7+
Thanks to Thibaut Decombe in `PR #537 <https://github.com/adamchainz/django-upgrade/pull/537>`__.
8+
59
1.23.1 (2025-02-07)
610
-------------------
711

README.rst

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -976,28 +976,28 @@ Thus, you should test affected paths after this fixer makes any changes.
976976
Note that ``[\w-]`` is sometimes used for slugs, but is not converted because it might be incompatible.
977977
That pattern matches all Unicode word characters, such as “α”, unlike Django's ``slug`` converter, which only matches Latin characters.
978978

979-
``lru_cache``
980-
~~~~~~~~~~~~~
979+
Compatibility imports
980+
~~~~~~~~~~~~~~~~~~~~~
981981

982982
**Name:** ``compatibility_imports``
983983

984-
Rewrites imports of ``lru_cache`` from ``django.utils.functional`` to use ``functools``.
984+
Rewrites some compatibility imports:
985+
986+
* ``lru_cache`` from ``django.utils.functional`` to use ``functools``.
987+
* ``ContextDecorator`` from ``django.utils.decorators`` to use ``contextlib``
988+
* ``SimpleCookie`` from ``django.http.cookie`` to use ``http.cookies``
985989

986990
.. code-block:: diff
987991
988992
-from django.utils.functional import lru_cache
989993
+from functools import lru_cache
990994
991-
``ContextDecorator``
992-
~~~~~~~~~~~~~~~~~~~~
993-
994-
Rewrites imports of ``ContextDecorator`` from ``django.utils.decorators`` to use ``contextlib``.
995-
996-
.. code-block:: diff
997-
998995
-from django.utils.decorators import ContextDecorator
999996
+from contextlib import ContextDecorator
1000997
998+
-from django.http.cookie import SimpleCookie
999+
+from http.cookies import SimpleCookie
1000+
10011001
``<func>.allow_tags = True``
10021002
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10031003

src/django_upgrade/fixers/compatibility_imports.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
"""
2-
- Replace compatibility imports for
3-
- `django.core.exceptions.EmptyResultSet`
4-
- `django.core.exceptions.FieldDoesNotExist`
5-
- `django.forms.utils.pretty_name`
6-
- `django.forms.boundfield.BoundField`
7-
See https://docs.djangoproject.com/en/3.1/releases/3.1/#id1
8-
9-
- Replace `JSONField` imports:
10-
See https://docs.djangoproject.com/en/3.1/releases/3.1/#features-deprecated-in-3-1
11-
12-
- Replace `django.utils.functional.lru_cache` with `functools.lru_cache`
13-
Undocumented change
14-
"""
15-
161
from __future__ import annotations
172

183
import ast
@@ -108,6 +93,7 @@
10893
(2, 0): {
10994
"django.utils.functional": {"lru_cache": "functools"},
11095
"django.utils.decorators": {"ContextDecorator": "contextlib"},
96+
"django.http.cookie": {"SimpleCookie": "http.cookies"},
11197
},
11298
(3, 1): {
11399
"django.contrib.postgres.forms": {

tests/fixers/test_compatibility_imports.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ def test_exception_class_imported_as_other_name(self):
330330
def test_all_transformed():
331331
check_transformed(
332332
"""\
333+
from django.http.cookie import SimpleCookie
333334
from django.forms.forms import pretty_name
334335
from django.forms.forms import BoundField
335336
from django.db.models.fields import FieldDoesNotExist
@@ -341,6 +342,7 @@ def test_all_transformed():
341342
from django.contrib.postgres.forms.jsonb import JSONField
342343
""",
343344
"""\
345+
from http.cookies import SimpleCookie
344346
from django.forms.utils import pretty_name
345347
from django.forms.boundfield import BoundField
346348
from django.core.exceptions import FieldDoesNotExist

0 commit comments

Comments
 (0)