Skip to content

Commit

Permalink
Refs #32365 -- Deprecated django.utils.timezone.utc.
Browse files Browse the repository at this point in the history
  • Loading branch information
carltongibson committed Mar 29, 2022
1 parent baf9604 commit 59ab3fd
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 4 deletions.
23 changes: 21 additions & 2 deletions django/utils/timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from django.conf import settings
from django.utils.deprecation import RemovedInDjango50Warning

__all__ = [
__all__ = [ # noqa for utc RemovedInDjango50Warning.
"utc",
"get_fixed_timezone",
"get_default_timezone",
Expand All @@ -41,7 +41,18 @@
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):
Expand Down Expand Up @@ -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"])
3 changes: 3 additions & 0 deletions docs/internals/deprecation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions docs/ref/utils.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions docs/releases/4.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
=======================

Expand Down
7 changes: 5 additions & 2 deletions tests/migrations/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
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
Expand Down Expand Up @@ -532,6 +533,8 @@ def test_serialize_datetime(self):
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(
Expand Down
13 changes: 13 additions & 0 deletions tests/timezones/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 59ab3fd

Please sign in to comment.