7
7
from typing import Dict
8
8
9
9
import 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
11
16
12
17
from pydantic_core import SchemaError , SchemaValidator , ValidationError , core_schema , validate_core_schema
13
18
@@ -81,8 +86,8 @@ def test_datetime_strict(input_value, expected):
81
86
82
87
83
88
def 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 )
86
91
v = SchemaValidator ({'type' : 'datetime' })
87
92
88
93
output = v .validate_python (dt )
@@ -94,8 +99,8 @@ def test_keep_tz():
94
99
95
100
96
101
def 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 )
99
104
v = SchemaValidator ({'type' : 'datetime' , 'gt' : datetime (2022 , 1 , 1 )})
100
105
101
106
output = v .validate_python (dt )
@@ -106,7 +111,7 @@ def test_keep_tz_bound():
106
111
assert output .tzinfo .dst (datetime (2022 , 1 , 1 )) == timedelta (0 )
107
112
108
113
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 ))
110
115
111
116
112
117
@pytest .mark .parametrize (
@@ -186,8 +191,8 @@ def test_custom_timezone_utc_repr():
186
191
187
192
188
193
def 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 )
191
196
192
197
# two times are the same instant, therefore le and ge are both ok
193
198
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():
322
327
now_utc = datetime .now (timezone .utc ) - timedelta (seconds = 1 )
323
328
assert v .isinstance_python (now_utc )
324
329
# "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' )))
326
331
# "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' )))
328
333
329
334
soon_utc = now_utc + timedelta (minutes = 1 )
330
335
assert not v .isinstance_python (soon_utc )
331
336
332
337
# "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' )))
334
339
# "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' )))
336
341
337
342
# input value is timezone naive, so we do a dumb comparison in these terms the istanbul time is later so fails
338
343
# 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 ))
341
346
342
347
343
348
@pytest .mark .parametrize (
@@ -368,17 +373,17 @@ def test_datetime_future_timezone():
368
373
assert v .isinstance_python (soon_utc )
369
374
370
375
# "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' )))
372
377
# "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' )))
374
379
375
380
past_utc = now_utc - timedelta (minutes = 1 )
376
381
assert not v .isinstance_python (past_utc )
377
382
378
383
# "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' )))
380
385
# "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' )))
382
387
383
388
384
389
def test_mock_utc_offset_8_hours (mocker ):
0 commit comments