remove dependency on pytz (#8984)

* remove pytz

* Revert "remove pytz"

This reverts commit 393609dfaa.

* remove pytz, more subtly
This commit is contained in:
Mathieu Dupuy 2023-06-04 07:24:07 +02:00 committed by GitHub
parent 03e2ecc9a5
commit 376a5cbbba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 12 deletions

View File

@ -29,7 +29,11 @@ from django.utils.encoding import is_protected_type, smart_str
from django.utils.formats import localize_input, sanitize_separators from django.utils.formats import localize_input, sanitize_separators
from django.utils.ipv6 import clean_ipv6_address from django.utils.ipv6 import clean_ipv6_address
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from pytz.exceptions import InvalidTimeError
try:
import pytz
except ImportError:
pytz = None
from rest_framework import ISO_8601 from rest_framework import ISO_8601
from rest_framework.exceptions import ErrorDetail, ValidationError from rest_framework.exceptions import ErrorDetail, ValidationError
@ -1148,8 +1152,10 @@ class DateTimeField(Field):
if not valid_datetime(dt): if not valid_datetime(dt):
self.fail('make_aware', timezone=field_timezone) self.fail('make_aware', timezone=field_timezone)
return dt return dt
except InvalidTimeError: except Exception as e:
if pytz and isinstance(e, pytz.exceptions.InvalidTimeError):
self.fail('make_aware', timezone=field_timezone) self.fail('make_aware', timezone=field_timezone)
raise e
elif (field_timezone is None) and timezone.is_aware(value): elif (field_timezone is None) and timezone.is_aware(value):
return timezone.make_naive(value, datetime.timezone.utc) return timezone.make_naive(value, datetime.timezone.utc)
return value return value

View File

@ -83,7 +83,7 @@ setup(
author_email='tom@tomchristie.com', # SEE NOTE BELOW (*) author_email='tom@tomchristie.com', # SEE NOTE BELOW (*)
packages=find_packages(exclude=['tests*']), packages=find_packages(exclude=['tests*']),
include_package_data=True, include_package_data=True,
install_requires=["django>=3.0", "pytz", 'backports.zoneinfo;python_version<"3.9"'], install_requires=["django>=3.0", 'backports.zoneinfo;python_version<"3.9"'],
python_requires=">=3.6", python_requires=">=3.6",
zip_safe=False, zip_safe=False,
classifiers=[ classifiers=[

View File

@ -9,7 +9,12 @@ from enum import auto
from unittest.mock import patch from unittest.mock import patch
import pytest import pytest
import pytz
try:
import pytz
except ImportError:
pytz = None
from django.core.exceptions import ValidationError as DjangoValidationError from django.core.exceptions import ValidationError as DjangoValidationError
from django.db.models import IntegerChoices, TextChoices from django.db.models import IntegerChoices, TextChoices
from django.http import QueryDict from django.http import QueryDict
@ -1604,6 +1609,7 @@ class TestPytzNaiveDayLightSavingTimeTimeZoneDateTimeField(FieldValues):
} }
outputs = {} outputs = {}
if pytz:
class MockTimezone(pytz.BaseTzInfo): class MockTimezone(pytz.BaseTzInfo):
@staticmethod @staticmethod
def localize(value, is_dst): def localize(value, is_dst):