From 853969c69c815be69513c2f63a41285858a45352 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Kul=C3=ADk?= Date: Thu, 24 Jul 2025 09:47:47 +0200 Subject: [PATCH] Fix test with Django 5 when pytz is available (#9715) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix test with Django 5 when pytz is available * fix formatting * remove original condition Co-authored-by: Ülgen Sarıkavak * remove trailing whitespace * further improvements * let's not skip the pytz test - it should always be executed when testing against Django 4 * add comment to test requirements Co-authored-by: Bruno Alla * simplify the pytz import as it should always be available * make isort happy --------- Co-authored-by: Ülgen Sarıkavak Co-authored-by: Bruno Alla --- requirements/requirements-testing.txt | 1 + tests/test_fields.py | 28 +++++++++++++-------------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/requirements/requirements-testing.txt b/requirements/requirements-testing.txt index 2b39316a0..b1e3c82ec 100644 --- a/requirements/requirements-testing.txt +++ b/requirements/requirements-testing.txt @@ -5,3 +5,4 @@ pytest-django>=4.5.2,<5.0 importlib-metadata<5.0 # temporary pin of attrs attrs==22.1.0 +pytz # Remove when dropping support for Django<5.0 diff --git a/tests/test_fields.py b/tests/test_fields.py index d574b07eb..56693ed7a 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -9,13 +9,9 @@ from enum import auto from unittest.mock import patch from zoneinfo import ZoneInfo +import django import pytest - -try: - import pytz -except ImportError: - pytz = None - +import pytz from django.core.exceptions import ValidationError as DjangoValidationError from django.db.models import IntegerChoices, TextChoices from django.http import QueryDict @@ -1624,7 +1620,10 @@ class TestCustomTimezoneForDateTimeField(TestCase): assert rendered_date == rendered_date_in_timezone -@pytest.mark.skipif(pytz is None, reason="Django 5.0 has removed pytz; this test should eventually be able to get removed.") +@pytest.mark.skipif( + condition=django.VERSION >= (5,), + reason="Django 5.0 has removed pytz; this test should eventually be able to get removed.", +) class TestPytzNaiveDayLightSavingTimeTimeZoneDateTimeField(FieldValues): """ Invalid values for `DateTimeField` with datetime in DST shift (non-existing or ambiguous) and timezone with DST. @@ -1638,16 +1637,15 @@ class TestPytzNaiveDayLightSavingTimeTimeZoneDateTimeField(FieldValues): } outputs = {} - if pytz: - class MockTimezone(pytz.BaseTzInfo): - @staticmethod - def localize(value, is_dst): - raise pytz.InvalidTimeError() + class MockTimezone(pytz.BaseTzInfo): + @staticmethod + def localize(value, is_dst): + raise pytz.InvalidTimeError() - def __str__(self): - return 'America/New_York' + def __str__(self): + return 'America/New_York' - field = serializers.DateTimeField(default_timezone=MockTimezone()) + field = serializers.DateTimeField(default_timezone=MockTimezone()) @patch('rest_framework.utils.timezone.datetime_ambiguous', return_value=True)