summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlton Gibson <carlton.gibson@noumenal.es>2022-03-24 11:09:55 +0100
committerCarlton Gibson <carlton@noumenal.es>2022-03-29 14:47:44 +0200
commit59ab3fd0e9e606d7f0f7ca26609c06ee679ece97 (patch)
tree9014f24e170c210492e65fe54bcbf236e7342c4a
parentbaf9604ed8fed3e6e7ddfaca2d83c377c81399ae (diff)
Refs #32365 -- Deprecated django.utils.timezone.utc.
-rw-r--r--django/utils/timezone.py23
-rw-r--r--docs/internals/deprecation.txt3
-rw-r--r--docs/ref/utils.txt5
-rw-r--r--docs/releases/4.1.txt3
-rw-r--r--tests/migrations/test_writer.py7
-rw-r--r--tests/timezones/tests.py13
6 files changed, 50 insertions, 4 deletions
diff --git a/django/utils/timezone.py b/django/utils/timezone.py
index 33c0440095..d22e358538 100644
--- a/django/utils/timezone.py
+++ b/django/utils/timezone.py
@@ -19,7 +19,7 @@ from asgiref.local import Local
from django.conf import settings
from django.utils.deprecation import RemovedInDjango50Warning
-__all__ = [
+__all__ = [ # noqa for utc RemovedInDjango50Warning.
"utc",
"get_fixed_timezone",
"get_default_timezone",
@@ -41,7 +41,18 @@ __all__ = [
NOT_PASSED = object()
-utc = timezone.utc
+def __getattr__(name):
+ if name != "utc":
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
+
+ warnings.warn(
+ "The django.utils.timezone.utc alias is deprecated. "
+ "Please update your code to use datetime.timezone.utc instead.",
+ RemovedInDjango50Warning,
+ stacklevel=2,
+ )
+
+ return timezone.utc
def get_fixed_timezone(offset):
@@ -339,3 +350,11 @@ def _datetime_ambiguous_or_imaginary(dt, tz):
return False
return tz.utcoffset(dt.replace(fold=not dt.fold)) != tz.utcoffset(dt)
+
+
+# RemovedInDjango50Warning.
+_DIR = dir()
+
+
+def __dir__():
+ return sorted([*_DIR, "utc"])
diff --git a/docs/internals/deprecation.txt b/docs/internals/deprecation.txt
index 31eb732297..ab147725c1 100644
--- a/docs/internals/deprecation.txt
+++ b/docs/internals/deprecation.txt
@@ -94,6 +94,9 @@ details on these changes.
``django.contrib.auth.views.LogoutView`` and
``django.contrib.auth.views.logout_then_login()`` will be removed.
+* The ``django.utils.timezone.utc`` alias to ``datetime.timezone.utc`` will be
+ removed.
+
.. _deprecation-removed-in-4.1:
4.1
diff --git a/docs/ref/utils.txt b/docs/ref/utils.txt
index 8c12294cd6..a2be25f168 100644
--- a/docs/ref/utils.txt
+++ b/docs/ref/utils.txt
@@ -843,6 +843,11 @@ appropriate entities.
:class:`~datetime.tzinfo` instance that represents UTC.
+ .. deprecated:: 4.1
+
+ This is an alias to :attr:`datetime.timezone.utc`. Use
+ :attr:`datetime.timezone.utc` directly.
+
.. function:: get_fixed_timezone(offset)
Returns a :class:`~datetime.tzinfo` instance that represents a time zone
diff --git a/docs/releases/4.1.txt b/docs/releases/4.1.txt
index eb4bde652b..8fc81e5ee7 100644
--- a/docs/releases/4.1.txt
+++ b/docs/releases/4.1.txt
@@ -545,6 +545,9 @@ Miscellaneous
:meth:`.RemoteUserBackend.configure_user`. Support for ``RemoteUserBackend``
subclasses that do not accept this argument is deprecated.
+* The :data:`django.utils.timezone.utc` alias to :attr:`datetime.timezone.utc`
+ is deprecated. Use :attr:`datetime.timezone.utc` directly.
+
Features removed in 4.1
=======================
diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py
index 3e95ca6c15..6ec6d938b9 100644
--- a/tests/migrations/test_writer.py
+++ b/tests/migrations/test_writer.py
@@ -29,10 +29,11 @@ from django.core.validators import EmailValidator, RegexValidator
from django.db import migrations, models
from django.db.migrations.serializer import BaseSerializer
from django.db.migrations.writer import MigrationWriter, OperationWriter
-from django.test import SimpleTestCase
+from django.test import SimpleTestCase, ignore_warnings
from django.utils.deconstruct import deconstructible
+from django.utils.deprecation import RemovedInDjango50Warning
from django.utils.functional import SimpleLazyObject
-from django.utils.timezone import get_default_timezone, get_fixed_timezone, utc
+from django.utils.timezone import get_default_timezone, get_fixed_timezone
from django.utils.translation import gettext_lazy as _
from .models import FoodManager, FoodQuerySet
@@ -532,6 +533,8 @@ class WriterTests(SimpleTestCase):
datetime.datetime(2014, 1, 1, 1, 1),
("datetime.datetime(2014, 1, 1, 1, 1)", {"import datetime"}),
)
+ with ignore_warnings(category=RemovedInDjango50Warning):
+ from django.utils.timezone import utc
for tzinfo in (utc, datetime.timezone.utc):
with self.subTest(tzinfo=tzinfo):
self.assertSerializedResultEqual(
diff --git a/tests/timezones/tests.py b/tests/timezones/tests.py
index 4e26516041..f2617f4d0f 100644
--- a/tests/timezones/tests.py
+++ b/tests/timezones/tests.py
@@ -88,6 +88,19 @@ def get_timezones(key):
return [constructor(key) for constructor in ZONE_CONSTRUCTORS]
+class UTCAliasTests(SimpleTestCase):
+ def test_alias_deprecation_warning(self):
+ msg = (
+ "The django.utils.timezone.utc alias is deprecated. "
+ "Please update your code to use datetime.timezone.utc instead."
+ )
+ with self.assertRaisesMessage(RemovedInDjango50Warning, msg):
+ timezone.utc
+
+ def test_timezone_module_dir_includes_utc(self):
+ self.assertIn("utc", dir(timezone))
+
+
@contextmanager
def override_database_connection_timezone(timezone):
try: