Refs #32365 -- Removed django.utils.timezone.utc per deprecation timeline.

This commit is contained in:
Mariusz Felisiak 2023-01-13 08:22:41 +01:00
parent 9a01311d20
commit d6816bff73
7 changed files with 15 additions and 67 deletions

View File

@ -3,7 +3,6 @@ Timezone-related classes and functions.
"""
import functools
import warnings
try:
import zoneinfo
@ -16,10 +15,8 @@ from datetime import datetime, timedelta, timezone, tzinfo
from asgiref.local import Local
from django.conf import settings
from django.utils.deprecation import RemovedInDjango50Warning
__all__ = [ # noqa for utc RemovedInDjango50Warning.
"utc",
__all__ = [
"get_fixed_timezone",
"get_default_timezone",
"get_default_timezone_name",
@ -38,20 +35,6 @@ __all__ = [ # noqa for utc RemovedInDjango50Warning.
]
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):
"""Return a tzinfo instance with a fixed offset from UTC."""
if isinstance(offset, timedelta):
@ -279,11 +262,3 @@ def make_naive(value, timezone=None):
def _datetime_ambiguous_or_imaginary(dt, tz):
return tz.utcoffset(dt.replace(fold=not dt.fold)) != tz.utcoffset(dt)
# RemovedInDjango50Warning.
_DIR = dir()
def __dir__():
return sorted([*_DIR, "utc"])

View File

@ -862,15 +862,6 @@ appropriate entities.
.. module:: django.utils.timezone
:synopsis: Timezone support.
.. data:: utc
: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

View File

@ -590,7 +590,7 @@ Miscellaneous
:ref:`Localization section <use_l10n_deprecation>` above for more details.
* As part of the :ref:`move to zoneinfo <whats-new-4.0>`,
:attr:`django.utils.timezone.utc` is changed to alias
``django.utils.timezone.utc`` is changed to alias
:attr:`datetime.timezone.utc`.
* The minimum supported version of ``asgiref`` is increased from 3.3.2 to

View File

@ -695,8 +695,8 @@ 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.
* The ``django.utils.timezone.utc`` alias to :attr:`datetime.timezone.utc` is
deprecated. Use :attr:`datetime.timezone.utc` directly.
* Passing a response object and a form/formset name to
``SimpleTestCase.assertFormError()`` and ``assertFormsetError()`` is

View File

@ -331,3 +331,6 @@ to remove usage of these features.
* Support for logging out via ``GET`` requests in the
``django.contrib.auth.views.LogoutView`` and
``django.contrib.auth.views.logout_then_login()`` is removed.
* The ``django.utils.timezone.utc`` alias to ``datetime.timezone.utc`` is
removed.

View File

@ -24,9 +24,8 @@ 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, ignore_warnings
from django.test import SimpleTestCase
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
from django.utils.translation import gettext_lazy as _
@ -568,19 +567,13 @@ 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(
datetime.datetime(2012, 1, 1, 1, 1, tzinfo=tzinfo),
(
"datetime.datetime"
"(2012, 1, 1, 1, 1, tzinfo=datetime.timezone.utc)",
{"import datetime"},
),
)
self.assertSerializedResultEqual(
datetime.datetime(2012, 1, 1, 1, 1, tzinfo=datetime.timezone.utc),
(
"datetime.datetime(2012, 1, 1, 1, 1, tzinfo=datetime.timezone.utc)",
{"import datetime"},
),
)
self.assertSerializedResultEqual(
datetime.datetime(
2012, 1, 1, 2, 1, tzinfo=zoneinfo.ZoneInfo("Europe/Paris")

View File

@ -33,7 +33,6 @@ from django.test import (
from django.test.utils import requires_tz_support
from django.urls import reverse
from django.utils import timezone, translation
from django.utils.deprecation import RemovedInDjango50Warning
from django.utils.timezone import timedelta
from .forms import (
@ -74,19 +73,6 @@ EAT = timezone.get_fixed_timezone(180) # Africa/Nairobi
ICT = timezone.get_fixed_timezone(420) # Asia/Bangkok
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: