77from typing import Dict
88
99import pytest
10- import pytz
10+
11+ try :
12+ import zoneinfo
13+ except ImportError :
14+ # TODO: can remove this once we drop support for python 3.8
15+ from backports import zoneinfo
1116
1217from pydantic_core import SchemaError , SchemaValidator , ValidationError , core_schema , validate_core_schema
1318
@@ -81,8 +86,8 @@ def test_datetime_strict(input_value, expected):
8186
8287
8388def test_keep_tz ():
84- tz = pytz . timezone ('Europe/London' )
85- dt = tz . localize ( datetime (2022 , 6 , 14 , 12 , 13 , 14 ) )
89+ tz = zoneinfo . ZoneInfo ('Europe/London' )
90+ dt = datetime (2022 , 6 , 14 , 12 , 13 , 14 , tzinfo = tz )
8691 v = SchemaValidator ({'type' : 'datetime' })
8792
8893 output = v .validate_python (dt )
@@ -94,8 +99,8 @@ def test_keep_tz():
9499
95100
96101def test_keep_tz_bound ():
97- tz = pytz . timezone ('Europe/London' )
98- dt = tz . localize ( datetime (2022 , 6 , 14 , 12 , 13 , 14 ) )
102+ tz = zoneinfo . ZoneInfo ('Europe/London' )
103+ dt = datetime (2022 , 6 , 14 , 12 , 13 , 14 , tzinfo = tz )
99104 v = SchemaValidator ({'type' : 'datetime' , 'gt' : datetime (2022 , 1 , 1 )})
100105
101106 output = v .validate_python (dt )
@@ -106,7 +111,7 @@ def test_keep_tz_bound():
106111 assert output .tzinfo .dst (datetime (2022 , 1 , 1 )) == timedelta (0 )
107112
108113 with pytest .raises (ValidationError , match = r'Input should be greater than 2022-01-01T00:00:00 \[type=greater_than' ):
109- v .validate_python (tz . localize ( datetime (2021 , 6 , 14 ) ))
114+ v .validate_python (datetime (2021 , 6 , 14 , tzinfo = tz ))
110115
111116
112117@pytest .mark .parametrize (
@@ -186,8 +191,8 @@ def test_custom_timezone_utc_repr():
186191
187192
188193def test_tz_comparison ():
189- tz = pytz . timezone ('Europe/London' )
190- uk_3pm = tz . localize ( datetime (2022 , 1 , 1 , 15 , 0 , 0 ) )
194+ tz = zoneinfo . ZoneInfo ('Europe/London' )
195+ uk_3pm = datetime (2022 , 1 , 1 , 15 , 0 , 0 , tzinfo = tz )
191196
192197 # two times are the same instant, therefore le and ge are both ok
193198 v = SchemaValidator ({'type' : 'datetime' , 'le' : uk_3pm }).validate_python ('2022-01-01T16:00:00+01:00' )
@@ -322,22 +327,22 @@ def test_datetime_past_timezone():
322327 now_utc = datetime .now (timezone .utc ) - timedelta (seconds = 1 )
323328 assert v .isinstance_python (now_utc )
324329 # "later" in the day
325- assert v .isinstance_python (now_utc .astimezone (pytz . timezone ('Europe/Istanbul' )))
330+ assert v .isinstance_python (now_utc .astimezone (zoneinfo . ZoneInfo ('Europe/Istanbul' )))
326331 # "earlier" in the day
327- assert v .isinstance_python (now_utc .astimezone (pytz . timezone ('America/Los_Angeles' )))
332+ assert v .isinstance_python (now_utc .astimezone (zoneinfo . ZoneInfo ('America/Los_Angeles' )))
328333
329334 soon_utc = now_utc + timedelta (minutes = 1 )
330335 assert not v .isinstance_python (soon_utc )
331336
332337 # "later" in the day
333- assert not v .isinstance_python (soon_utc .astimezone (pytz . timezone ('Europe/Istanbul' )))
338+ assert not v .isinstance_python (soon_utc .astimezone (zoneinfo . ZoneInfo ('Europe/Istanbul' )))
334339 # "earlier" in the day
335- assert not v .isinstance_python (soon_utc .astimezone (pytz . timezone ('America/Los_Angeles' )))
340+ assert not v .isinstance_python (soon_utc .astimezone (zoneinfo . ZoneInfo ('America/Los_Angeles' )))
336341
337342 # input value is timezone naive, so we do a dumb comparison in these terms the istanbul time is later so fails
338343 # wile the LA time is earlier so passes
339- assert not v .isinstance_python (soon_utc .astimezone (pytz . timezone ('Europe/Istanbul' )).replace (tzinfo = None ))
340- assert v .isinstance_python (soon_utc .astimezone (pytz . timezone ('America/Los_Angeles' )).replace (tzinfo = None ))
344+ assert not v .isinstance_python (soon_utc .astimezone (zoneinfo . ZoneInfo ('Europe/Istanbul' )).replace (tzinfo = None ))
345+ assert v .isinstance_python (soon_utc .astimezone (zoneinfo . ZoneInfo ('America/Los_Angeles' )).replace (tzinfo = None ))
341346
342347
343348@pytest .mark .parametrize (
@@ -368,17 +373,17 @@ def test_datetime_future_timezone():
368373 assert v .isinstance_python (soon_utc )
369374
370375 # "later" in the day
371- assert v .isinstance_python (soon_utc .astimezone (pytz . timezone ('Europe/Istanbul' )))
376+ assert v .isinstance_python (soon_utc .astimezone (zoneinfo . ZoneInfo ('Europe/Istanbul' )))
372377 # "earlier" in the day
373- assert v .isinstance_python (soon_utc .astimezone (pytz . timezone ('America/Los_Angeles' )))
378+ assert v .isinstance_python (soon_utc .astimezone (zoneinfo . ZoneInfo ('America/Los_Angeles' )))
374379
375380 past_utc = now_utc - timedelta (minutes = 1 )
376381 assert not v .isinstance_python (past_utc )
377382
378383 # "later" in the day
379- assert not v .isinstance_python (past_utc .astimezone (pytz . timezone ('Europe/Istanbul' )))
384+ assert not v .isinstance_python (past_utc .astimezone (zoneinfo . ZoneInfo ('Europe/Istanbul' )))
380385 # "earlier" in the day
381- assert not v .isinstance_python (past_utc .astimezone (pytz . timezone ('America/Los_Angeles' )))
386+ assert not v .isinstance_python (past_utc .astimezone (zoneinfo . ZoneInfo ('America/Los_Angeles' )))
382387
383388
384389def test_mock_utc_offset_8_hours (mocker ):
0 commit comments